简体   繁体   English

.NET Core依赖注入向后兼容.NET Framework?

[英].NET Core dependency injection backwards compatibility with .NET Framework?

I want to rebuild a .NET Framework library to .NET Core, and then use this library in the .NET Framework app. 我想将.NET Framework库重建为.NET Core,然后在.NET Framework应用程序中使用此库。

The library needs a database connectionstring. 该库需要一个数据库连接字符串。 In .NET Core I would use Dependency Injection to pass the configuration with the connectionstring to the library class. 在.NET Core中,我将使用依赖注入将带有连接字符串的配置传递给库类。

public MyRepository(IConfiguration config)
{
    _connectionString = config.GetConnectionString("MyDb");
}

But how can I use this class from the .NET Framework 4.6 library without introducing complex DI frameworks? 但是,如何在不引入复杂DI框架的情况下使用.NET Framework 4.6库中的此类?

I'm already using the new Microsoft.Extensions libraries in .NET Framework 4.7.1, mainly the DependencyInjection and Configuration libraries. 我已经在.NET Framework 4.7.1中使用新的Microsoft.Extensions库,主要是DependencyInjectionConfiguration库。

The 2.x libraries are compatible with .NET Standard 2.0, which means they can be added to applications that target any runtime that is compatible with .NET Standard 2.0, ie .NET Framework 4.7.1 and above or .NET Core 2.0 and above. 2.x库与.NET Standard 2.0兼容,这意味着它们可以添加到针对与.NET Standard 2.0兼容的任何运行时的应用程序,即.NET Framework 4.7.1及更高版本或.NET Core 2.0及更高版本。 。

In older runtimes (4.6.1 and later), NuGet may have to add some extra packages with newer versions of some system assemblies, eg System.Runtime. 在较旧的运行时(4.6.1及更高版本)中,NuGet可能必须使用某些系统程序集的较新版本添加一些额外的软件包,例如System.Runtime。

You can't add the 2.0 Extension packages to 4.6 at all. 您根本无法将2.0扩展包添加到4.6。 You can add the older, 1.x versions which are use in .NET Core 1.x. 您可以添加.NET Core 1.x中使用的旧版1.x版本。

Configuring the extensions is done in the same way in .NET Core and Full Framework: 在.NET Core和Full Framework中以相同的方式配置扩展:

  1. You create a ConfigurationBuilder, add configuration providers and call Build() in the end to get an IConfigurationRoot object: 您创建一个ConfigurationBuilder,添加配置提供程序并最后调用Build()以获取IConfigurationRoot对象:

     IConfigurationRoot configRoot = new ConfigurationBuilder() .AddUserSecrets<Program>() .AddJsonFile($"appsettings.json") .Build(); 
  2. Create a ServiceCollection, register services and call BuildServiceProvider() to get a ServiceProvider . 创建一个ServiceCollection,注册服务并调用BuildServiceProvider()以获取ServiceProvider You'll use that ServiceProvider to create instances of classes that require injection. 您将使用该ServiceProvider创建需要注入的类的实例。

ASP.NET Core, which also works on the Full framework provides extra helper libraries that hide the boilerplate code. ASP.NET Core也适用于Full框架,它提供了隐藏样板代码的额外帮助程序库。

IServiceCollection services = new ServiceCollection();
ServiceProvider provider= services.AddSingleton<MyService>()
                                  .AddTransient<AnotherService>()
                                  .AddTransient<ServiceThatNeedsTheOthers>()
                                  .BuildServiceProvider();

Assuming the third service is : 假设第三项服务是:

public class ServiceThatNeedsTheOthers
{
    public ServiceThatNeedsTheOthers(MyService s1,AnotherService s2){..}
}

You can create it with : 你可以创建它:

var service3=provider.GetRequiredService<ServiceThatNeedsTheOthers>();

All this is described in Mark Michaelis' Essential .NET column in MSDN Magazine, eg Dependency Injection with .NET Core and Configuration in .NET Core . 所有这些都在MSDN杂志中的Mark Michaelis的Essential .NET专栏中进行了描述,例如.NET Core中的依赖注入和.NET Core中的 配置 The articles show how to setup and use the Extensions in Console applications where you need to write all the boilerplate. 这些文章展示了如何在控制台应用程序中设置和使用扩展,您需要编写所有样板文件。

PS ASP.NET Core 2.0 can target the Full framework too. PS ASP.NET Core 2.0也可以针对Full框架。 For a new web application it may make sense to create an ASP.NET Core 2.0 project targeting 4.7.1 对于新的Web应用程序,创建面向4.7.1的ASP.NET Core 2.0项目可能是有意义的

The scenario you describe is what .NET Standard is meant to achieve. 您描述的场景是.NET Standard要实现的目标。 Only a class library project can target .netstandard and any such library is compatible with most recent .NET Framework applications as well as .NET Core applications. 只有类库项目可以以.netstandard目标,并且任何此类库都与最新的.NET Framework应用程序以及.NET Core应用程序兼容。

The .NET Core assemblies in Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Configuration are also .netstandard -compatible, which means you can use those in your library and in your applications. Microsoft.Extensions.DependencyInjection和Microsoft.Extensions.Configuration中的.NET Core程序集也是.netstandard -compatible,这意味着您可以在库和应用程序中使用它们。

So, yes, you can use .NET Core DI with .NET Framework, and in a library scenario just stick to .NET Standard APIs and it will also work with all .NET implementations. 所以,是的,您可以将.NET Core DI与.NET Framework一起使用,并且在库场景中只需坚持使用.NET标准API,它也适用于所有.NET实现。 I have some very large, very complicated .netstandard20 libraries which I use with applications targeting both frameworks, and I rarely need to ask myself whether a given API is compatible because the coverage is so good now, but you can find the complete list here . 我有一些非常大的,非常复杂的.netstandard20库,我使用它们同时针对两个框架的应用程序,我很少需要问自己一个给定的API是否兼容,因为现在覆盖范围很好,但你可以在这里找到完整的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM