【问题标题】:Moving solution to ASP.NET vNext - Unable to reference assembly将解决方案移动到 ASP.NET vNext - 无法引用程序集
【发布时间】:2015-02-26 13:06:46
【问题描述】:

我正在将现有解决方案迁移到 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 项目。我的问题是,我似乎无法从我的test 项目中引用library 项目。如果我尝试在 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" ]
}

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

谢谢!

【问题讨论】:

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


    【解决方案1】:
    1. global.json 中将sources 更改为["backend","test"]
    2. 您在测试项目中缺少library 引用:

      "dependencies": {
          "library": "",
          "Xunit.KRunner": "1.0.0-beta1"
       },
      

    【讨论】:

    • 如果我将library 添加到test\library\project.json' file, as shown in #2, I get an error. If I run kpm restore, the error I get is: TODO:不支持循环依赖引用。包 'library'.. If I run the test, I get an error that says: Process 由于 StackOverflowException 而终止。`
    • 只是为了排除一些其他的问题,可以尝试将测试项目重命名为其他名称吗?喜欢library.test
    • 太棒了!我将 /test/library 目录重命名为 test/library-tests 并且它起作用了。感觉就像一个黑客。但是它有效。感谢您的帮助!
    • 这对我来说就像一个错误。我会打开一个
    • 另外,请记住,NuGet 包是为每个项目生成的,因此您不希望项目名称相同,因为您会遇到冲突
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多