将解决方案移动到ASP.NET vNext - 无法引用程序集

时间:2014-12-30 11:01:06

标签: c# asp.net asp.net-core

我正在将现有解决方案移至ASP.NET vNext。我的解决方案是传统的N层应用程序。考虑到这一点,该解决方案有四个项目。这四个项目的组织方式如下:

/backend
  /library
    Sample.cs
    project.json
  /web
    project.json
/test
  /library
    SampleTest.cs
    project.json
  /web
    project.json
global.json

简而言之,web项目引用了library项目。 /test/library/项目旨在测试library项目。 /test/web/项目旨在测试web项目。我的问题是,我似乎无法从我的library项目中引用test项目。如果我尝试在library中引用某个类,则会收到错误消息:

System.IO.FileLoadException: Could not load file or assembly 'library' or one of
 its dependencies. General Exception (Exception from HRESULT: 0x80131500)
System.IO.FileLoadException: Could not load file or assembly 'library' or one of
 its dependencies. General Exception (Exception from HRESULT: 0x80131500)
File name: 'library' ---> Microsoft.Framework.Runtime.Roslyn.RoslynCompilationEx
ception: C:\Projects\MySolution\test\library\SampleTest.cs(21,7
): error CS0246: The type or namespace name 'Sample' could not be found (
are you missing a using directive or an assembly reference?)

以下是我相关文件的内容

/backend/library/project.json

{
  "version":"3.0.0.0",
  "description":"This is the app library",
  "compilationOptions": {
    "warningsAsErrors": true
  },
  "dependencies": {
    "Microsoft.Data.Common":"0.1.0.0-alpha-build-0137"
  },
  "code": [ "**\\*.cs", "..\\Shared\\*.cs" ],
  "frameworks": {
    "net45": {}
  }
}

/backend/library/Sample.cs

using System;
using System.Collections.Generic;

using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using System.Linq;
using System.Text;

namespace MySolution.Library
{
  public class Sample
  {
    public int Id { get; set; }
    public string Name { get; set; }

    public void Save() 
    {
    }
  }
}

/test/library/project.json

{
  "commands": {
    "test": "Xunit.KRunner"
  },
  "dependencies": {
    "Xunit.KRunner": "1.0.0-beta1"
  },
  "frameworks": {
    "aspnet50": { },
    "aspnetcore50": { }
  }
}

/test/library/SampleTest.cs

using System.IO;
using Microsoft.Framework.Runtime;
using Xunit;

namespace MySolution.Library
{
  public class SampleTest
  {
    [Fact]
    public void Save()
    {
      Sample sample = new Sample();
      sample.Save();
      Assert.NotNull(sample);
    }
  }
}

global.json

{
  "sources": [ "backend/library" ]
}

我做错了什么?为什么我似乎无法从我的library项目中引用我的test项目?

谢谢!

1 个答案:

答案 0 :(得分:0)

  1. global.json中将sources更改为["backend","test"]
  2. 您缺少测试项目中的library引用:

    "dependencies": {
        "library": "",
        "Xunit.KRunner": "1.0.0-beta1"
     },
    
相关问题