【问题标题】:.NET Framework Class Library with Microsoft.Extensions.DependencyInjection带有 Microsoft.Extensions.DependencyInjection 的 .NET Framework 类库
【发布时间】:2017-02-14 17:26:18
【问题描述】:

我有一个 .NET Framework (4.5.2) 类库,我想使用 Microsoft.Extensions.DependencyInjection,因为我们正在单独开发一个 ASP.NET Core 应用程序。

我使用 nuget 安装它,当我使用 Visual Studio 构建时它工作正常,但尝试在 TeamCity 构建代理上使用 MSBuild 构建它给了我很多:

MyClass.cs(32, 26): error CS0012: The type 'IServiceProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
MyClass.cs(33, 31): error CS0012: 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'.
MyClass.cs(33, 31): error CS1061: 'IServiceScope' does not contain a definition for 'Dispose' and no extension method 'Dispose' accepting a first argument of type 'IServiceScope' could be found (are you missing a using directive or an assembly reference?)
MyClass.cs(34, 10): error CS0012: 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'.
MyClass.cs(34, 10): error CS0012: The type 'IServiceProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
MyClass.cs(35, 10): error CS0012: 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'.
MyClass.cs(35, 10): error CS0012: The type 'IServiceProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
MyClass.cs(36, 51): error CS0012: 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'.
MyClass.cs(36, 51): error CS0012: The type 'IServiceProvider' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
MyClass.cs(36, 30): error CS0012: The type 'Object' 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'.

某些 nuget 包(如 Microsoft.Extensions.DependencyInjection 本身)没有单独的 net452 程序集,只有 netstandard1.0 或 netstandard1.1。这可能是问题吗?我在构建服务器上安装了 .NET Core SDK 没有效果。

【问题讨论】:

  • 正如herehere所表达的,类库不应该使用任何DI库。
  • 库公开了一个抽象的 Topshelf 服务类,然后在控制台应用程序中实现,类似于 ASP.NET 在实践中是一个使用 DI 的库。我这样做是因为我们不需要将粘贴样板复制到我们制作的每项服务中。
  • 这种方法有很多缺点,尤其是当您使用 .NET Core DI 抽象时,即 Conforming Container
  • @JussiKosunen - “我正在这样做,所以我们不需要将粘贴样板复制到我们制作的每项服务中。” - 对于您所做的每项服务,您都有.config 文件吗?将组合根视为另一个(在代码中).config 文件。你不会在应用程序之间共享.config 文件,那你为什么要share a composition root
  • 组合根在服务中,没有任何内容被隐式添加到库中的容器中。该库仅提供一个框架和实用程序,以使体验尽可能接近 ASP.NET Core。我试图优先考虑使服务实现者的工作更容易,而不是担心 ioc 容器在哪个程序集中被实例化。

标签: c# .net dependency-injection msbuild


【解决方案1】:

我遇到了同样的问题,但在使用 NuGet 安装程序作为构建步骤后,我的所有问题都得到了解决。

在开始构建项目之前,您只需要做一件事 - 只需添加一个构建步骤并选择 Runner 类型作为 NuGet 安装程序

您可以从这个屏幕截图中查看:

【讨论】:

  • 在这种情况下,我已经这样做了。所需的程序集(System.ComponentModel 和 System.Runtime)不是通过 NuGet 作为依赖项提供的,而是需要安装 Visual Studio 或 Developer Pack。
【解决方案2】:

原来我错过了 .NET Framework 4.5.2 开发人员包,奇怪的是我的所有其他解决方案没有它都可以正常工作。

【讨论】:

    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2018-10-13
    相关资源
    最近更新 更多