解析 ASP.Net 5、"IDisposable is ... not referenced" 中的引用

Resolving references in ASP.Net 5, "IDisposable is ... not referenced"

我做了以下事情:

到目前为止一切似乎都很好。一切都编译了,尽管它什么也没做。

在 Foo.Domain 中,我创建了一个 class,其中包含一个在 'using' 语句中引用 GraphDatabase class 的方法。这就是它崩溃的地方。

我收到此错误消息(以及其他类似消息):

The type 'IDisposable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Foo.Domain..NET Framework 4.5.1 C:\dev\WFW3\src\Foo.Domain\FooRepository.cs

我的理解是绑定重定向在 ASP.Net 中不可用 5. 这是正确的吗?我如何解决这个没有引用正确版本的 System.Runtime 的问题? 在 System.Runtime 中找到的项目 可供我使用。它似乎在 Neo4j.Driver.V1 程序集中寻找 System.Runtime 的旧版本。我尝试了此处找到的解决方案 (Nathan's answer),但随后它开始抱怨我正在尝试导入两种不同类型的运行时库,而我需要删除一个。但是我应该删除哪一个,如何删除?

API project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Foo.Domain": "1.0.0-*",
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

Foo.Domain project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Foo.Domain": "1.0.0-*",
    "Microsoft.ApplicationInsights.AspNet": "1.0.0-rc1",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

FooRepository 代码(在Foo.Domain):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Neo4j.Driver.V1;

namespace Foo.Domain
{
   public class FooRepository: IFooRepository
    {

        public Foo GetById(string Id)
        {
            // The next lines inside the 'using' are getting the error.
            using (var driver = GraphDatabase.Driver("http://localhost:7474/"))
            using (var session = driver.Session())
            {
                var result = session.Run("CREATE (n) RETURN n");
            }
            return null;
        }
    }
}

参考这个类似的问题:

该问题中的问题似乎与项目中包含 dnxcore50 框架这一事实有关。

"frameworks": {
    "dnx451": { },
    "dnxcore50": { }
}

删除它解决了问题。

"frameworks": {
    "dnx451": { }
}

这适用于他们的情况,也应该适用于您。我相信您得到这个是因为省略了此处引用的核心 CLR (dnxcore50) 框架所需的依赖项:

Frameworks

This snippet will build for Desktop (dnx451) or Core CLR (dnxcore50). Core CLR has many extra dependencies as the packages that make up the BCL need to be referenced...

{
  "frameworks": {
    "dnx451": {},
    "dnxcore50": {
      "dependencies": {
        "System.Collections": "4.0.0.0",
        "System.Collections.Concurrent": "4.0.0.0",
        "System.ComponentModel": "4.0.0.0",
        "System.Linq": "4.0.0.0",
        "System.Reflection": "4.0.10.0",
        "System.Runtime": "4.0.20.0",
        "System.Runtime.InteropServices": "4.0.10.0",
        "System.Threading": "4.0.0.0",
        "System.Threading.Tasks": "4.0.0.0"
      }
    }
  }
}

这就是为什么当它从上一个问题中删除时项目有效。我看到的大多数与此问题相关的其他帖子也建议删除 dnxcore50

你也可以看看这个以便更好地理解...

Framework-specific dependencies

You can also add dependencies for a particular framework like this:

{
  "frameworks": {
    "dnxcore50":{
      "dependencies":{
        "System.Runtime": "4.0.0.0"
      }
    },
    "dnx451":{}
  }
}

In the above example, the System.Runtime dependency is only needed for the dnxcore50 target, not dnx451. It is often the case that you will have extra dependencies on Core CLR, because there are packages that you need to depend on in Core CLR that are part of .NET 4.5.x.

备注

While it is technically true that you do not need the System.Runtime package on .NET 4.5.1, it also doesn’t matter if you add it as a top level dependency. Each of the System.* packages will work as a top level dependency. So, you don’t always have to have this separation. You could add System.Runtime as a top level dependency and it will not impact your application when on .NET 4.5.1.

附加参考 material:

Diagnosing dependency issues with ASP.NET 5

我找到了我的解决方案。除了从根目录中删除 'dnxcore50' 框架,web.api 库如 @Nkosi 所建议的那样,我还需要在我的 [=18] 中添加一个 'frameworkAssemblies' 部分=] 图书馆。 对 'Foo.Domain' 库的以下更改使解决方案编译并按预期运行。

{
  "version": "1.0.0-*",
  "description": "Foo.Domain Class Library",
  "authors": [ "Malcolm" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "frameworks": {
    "net451": {
      "frameworkAssemblies": {
        "System.Runtime": "4.0.10.0"
      }
    }
  },
  "dependencies": {
    "Neo4j.Driver": "1.0.0"
  }
}