.Net Core implements the host program and interface implementation class library through dependency injection and dynamic loading of assemblies Complete deconstruction

There are many example codes of .Net Core dependency injection on the Internet. For example, in the host program, it should be written like this:

services.AddTransient<Interface1, Class1>();

Among them, Interface1 is the interface, and Class1 is the implementation class of the interface. Generally, we will separate the interface project and the implementation class project into two projects to achieve decoupling.

However, this code requires the host program to refer to the implementation class project, so the destructuring implementation here is not complete. To achieve complete decoupling is to realize that the host program does not refer to the implementation class project.
Or change the injected code to this:

 services.Add(new ServiceDescriptor(serviceType: typeof(Interface1),
                                          implementationType: Type.GetType("ClassLibrary1.Class1, ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"),
                                          lifetime: ServiceLifetime.Transient));

In fact, this code also requires the host class to reference the implementation class library project, otherwise the operation will go wrong. Only by dynamically loading the assembly can the host program not refer to the implementation class:

 var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"C:\Users\pesu\source\repos\DynamicLoadDependencyInjectionConsoleApp\ClassLibrary1\bin\Debug\netcoreapp2.0\ClassLibrary1.dll");

The above code will dynamically load the implementation class library assembly into the host program, and then change the injected code to this:

  services.Add(new ServiceDescriptor(serviceType: typeof(Interface1),
                                          implementationType: myAssembly.GetType("ClassLibrary1.Class1"),
                                          lifetime: ServiceLifetime.Transient));

Among them, ClassLibrary1.Class1 is the implementation class of Interface1. The complete code is as follows:

using InterfaceLibrary;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Runtime.Loader;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(@"C:\Users\pesu\source\repos\DynamicLoadDependencyInjectionConsoleApp\ClassLibrary1\bin\Debug\netcoreapp2.0\ClassLibrary1.dll");
         
            IServiceCollection services = new ServiceCollection();
            //注入
            services.AddTransient<ILoggerFactory, LoggerFactory>();
           
            services.Add(new ServiceDescriptor(serviceType: typeof(Interface1),
                                          implementationType: myAssembly.GetType("ClassLibrary1.Class1"),
                                          lifetime: ServiceLifetime.Transient));

            //构建容器
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            //解析
            serviceProvider.GetService<ILoggerFactory>().AddConsole(LogLevel.Debug);
            var cls = serviceProvider.GetService<Interface1>();
            cls.Say();
            Console.ReadKey();
        }
    }
}

output:


What's the use of this?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324524334&siteId=291194637