Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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.

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.

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?

like image 760
fantastischIdee Avatar asked Mar 30 '18 09:03

fantastischIdee


People also ask

Is .NET Core backwards compatible with .NET Framework?

NET Framework 4.5 and later versions are backward-compatible with apps that were built with earlier versions of the . NET Framework. In other words, apps and components built with previous versions will work without modification on the .

Is .NET Core compatible with .NET Framework?

Net Core 3.0 is NOT compatible with . Net Framework of any version.

Can .NET Core and .NET Framework coexist on same server?

NET Framework. In fact, you can actually install multiple version of . NET Core side-by-side on the same machine (unlike . NET Framework).

Does .NET Framework support Dependency injection?

. NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.


2 Answers

I'm already using the new Microsoft.Extensions libraries in .NET Framework 4.7.1, mainly the DependencyInjection and Configuration libraries.

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.

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.

You can't add the 2.0 Extension packages to 4.6 at all. You can add the older, 1.x versions which are use in .NET Core 1.x.

Configuring the extensions is done in the same way in .NET Core and Full Framework:

  1. You create a ConfigurationBuilder, add configuration providers and call Build() in the end to get an IConfigurationRoot object:

    IConfigurationRoot configRoot = new ConfigurationBuilder()
        .AddUserSecrets<Program>()
        .AddJsonFile($"appsettings.json")
        .Build();
    
  2. Create a ServiceCollection, register services and call BuildServiceProvider() to get a ServiceProvider. You'll use that ServiceProvider to create instances of classes that require injection.

ASP.NET Core, which also works on the Full framework provides extra helper libraries that hide the boilerplate code.

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. 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. For a new web application it may make sense to create an ASP.NET Core 2.0 project targeting 4.7.1

like image 103
Panagiotis Kanavos Avatar answered Oct 03 '22 10:10

Panagiotis Kanavos


The scenario you describe is what .NET Standard is meant to achieve. 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.

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.

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. 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.

like image 25
McGuireV10 Avatar answered Oct 03 '22 11:10

McGuireV10