【发布时间】: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 没有效果。
【问题讨论】:
-
库公开了一个抽象的 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