6

我做了以下事情:

  • 创建了一个新的 Web.Api 项目:“WFW3”。我在ASP.Net 5下使用了“Web API”模板。
  • 我再次使用ASP.Net 5创建了一个新的类库“Foo.Domain” 。
  • 我从 API 项目中添加了对它的引用。
  • 我将来自 Nuget 的Neo4j.Driver(一个可移植类库)安装到 Foo.Domain 项目中。Neo4j-NuGet

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

在 Foo.Domain 中,我创建了一个类,该类的方法在“使用”语句中引用了 GraphDatabase 类。这就是它坏的地方。

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

'IDisposable' 类型是在未引用的程序集中定义的。您必须添加对程序集“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。我尝试了此处找到的解决方案(内森的回答),但随后它开始抱怨我试图导入两种不同类型的运行时库,我需要删除一个。但是我应该删除哪个,以及如何删除?

API 项目.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 项目.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;
        }
    }
}
4

2 回答 2

2

引用这个类似的问题:当 ASP.NET 5 (vNext) 无法重定向绑定时该怎么办?

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

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

并删除它解决了这个问题。

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

这适用于他们的情况,也应该适用于您。我相信您之所以会得到这个,是因为省略了 Core CLR (dnxcore50) 框架所需的依赖项,如下所示:

构架

此代码段将为 Desktop (dnx451) 或 Core CLR (dnxcore50) 构建。 核心 CLR 有许多额外的依赖项,因为构成 BCL 的包需要被引用......

{
  "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

你也可以看看这个以获得更好的理解......

特定于框架的依赖项

您还可以为特定框架添加依赖项,如下所示:

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

在上面的示例中,System.Runtimednxcore50目标需要依赖项,而dnx451 不需要。通常情况下,您会对 Core CLR 有额外的依赖,因为在 Core CLR 中您需要依赖一些包,这些包是 .NET 4.5.x 的一部分。

笔记

虽然从技术上讲,您在 .NET 4.5.1 上不需要该System.Runtime 包,但是否将其添加为顶级依赖项也没有关系。每个System.*包都将作为顶级依赖项工作。所以,你不必总是有这种分离。您可以添加System.Runtime为顶级依赖项,并且在 .NET 4.5.1 上它不会影响您的应用程序。

附加参考资料:

使用 ASP.NET 5 诊断依赖性问题

如何诊断 dnx 中缺少的依赖项(或其他加载程序故障)?

于 2016-05-12T01:14:06.403 回答
2

我找到了我的解决方案。除了按照@Nkosi 的建议从根 web.api 库中删除“dnxcore50”框架外,我还需要在我的类库中添加一个“ 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"
  }
}
于 2016-05-13T01:57:44.860 回答