惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
I
InfoQ
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
B
Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
雷峰网
雷峰网
Recent Announcements
Recent Announcements
量子位
B
Blog RSS Feed

博客园 - 小庄

如何正确的 DDD AI时代的领域驱动设计:DAD 我开发了个人工智能应用, 请大家给点意见 通过代理使用人工智能服务保姆级教程 OnionArch 2.0 - 基于DDD的洋葱架构改进版开源 根据MediatR的Contract Messages自动生成Minimal WebApi接口 MediatRPC - 基于MediatR和Quic通讯实现的RPC示例,新增Server Stream功能,发布开源地址。 MediatRPC - 基于MediatR和Quic通讯实现的RPC框架,比GRPC更简洁更低耦合,开源发布第一版 Dapr实现.Net Grpc服务之间的发布和订阅,并采用WebApi类似的事件订阅方式 关于.Net 7.0 RC gRPC JSON 转码为 Swagger/OpenAPI文档的注意事项 OnionArch - 如何实现更新指定字段的通用Handler OnionArch - 采用DDD+CQRS+.Net 7.0实现的洋葱架构 Blazor+Dapr+K8s微服务之基于WSL安装K8s集群并部署微服务 Blazor+Dapr+K8s微服务之事件发布订阅 Blazor+Dapr+K8s微服务之状态管理 Blazor+Dapr+K8s微服务之开发环境调试 Blazor+Dapr+K8s微服务之服务调用 面向对象之"三段论" 使用WIX打包客户端程序
.Net Core 通过依赖注入和动态加载程序集实现宿主程序和接口...
小庄 · 2018-04-17 · via 博客园 - 小庄

网上很多.Net Core依赖注入的例子代码,例如再宿主程序中要这样写:

services.AddTransient<Interface1, Class1>();

其中Interface1是接口,Class1是接口的实现类,一般我们会将接口项目和实现类项目分开成两个项目以实现解耦。

但这段代码却要求宿主程序要引用实现类项目,所以这里的解构实现的并不彻底,要完全解耦就是要实现宿主程序不引用实现类项目。
或者把注入的代码改成这样:

 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));

其实这段代码也要求宿主类引用实现类库项目,不然运行会出错,只有采用动态加载程序集的方式才能实现宿主程序不引用实现类:

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

上面代码将实现类库程序集动态加载到宿主程序,然后将注入的代码改成这样:

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

其中ClassLibrary1.Class1是Interface1的实现类,完整代码如下:

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();
        }
    }
}

输出:


这有什么用呢?