• 3.用Autofac增强容器能力:引入面向切面编程(AOP)的能力


    一、核心组件包

    • Microsoft.Extensions.Configuration. Abstractions
    • Microsoft.Extensions.Configuration
    Autofac.Extensions.DependencyInjection
    Autofac.Extras.DynamicProxy
     
    核心扩展点
    public interface IServiceProviderFactory<TContainerBuilder>  //注册第三方容器的入口
     
    二、使用
    1.在Program.cs添加 UseServiceProviderFactory(new AutofacServiceProviderFactory())
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .UseServiceProviderFactory(new AutofacServiceProviderFactory()) //注册第三方容器的入口
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });

    2.在Startup.cs添加注册方法Autofac的注册方法, public void ConfigureContainer(ContainerBuilder builder)  

            public virtual void ConfigureContainer(ContainerBuilder builder)
            {
                builder.RegisterType<MyService>().As<IMyService>();
                #region 命名注册
                builder.RegisterType<MyServiceV2>().Named<IMyService>("service2"); //获取对象是通过service2获取MyServiceV2对象
                #endregion
    
                #region 属性注册
                builder.RegisterType<MyNameService>();
                builder.RegisterType<MyServiceV2>().As<IMyService>().PropertiesAutowired(); //设置可以属性方式获取注入对象
                #endregion
    
                #region AOP  
                builder.RegisterType<MyInterceptor>();
                builder.RegisterType<MyNameService>();
                builder.RegisterType<MyServiceV2>().As<IMyService>().PropertiesAutowired().InterceptedBy(typeof(MyInterceptor)).EnableInterfaceInterceptors();
                //InterceptedBy(typeof(MyInterceptor)):使用拦截器MyInterceptor并注入
                //EnableInterfaceInterceptors:允许接口拦截器
                //拦截器:接口拦截器,类拦截器。类拦截器需要把方法设置成virtual
    
                #endregion
    
                #region 子容器
                builder.RegisterType<MyNameService>().InstancePerMatchingLifetimeScope("myscope");
                #endregion
            }

    拦截器MyInterceptor定义

        public class MyInterceptor : IInterceptor
        {
            public void Intercept(IInvocation invocation)
            {
                Console.WriteLine($"Intercept before,Method:{invocation.Method.Name}");
                invocation.Proceed();
                Console.WriteLine($"Intercept after,Method:{invocation.Method.Name}");
            }
        }

    3.使用在Autofac注册的服务

            public ILifetimeScope AutofacContainer { get; private set; }
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
                var servicenamed = this.AutofacContainer.Resolve<IMyService>();
                servicenamed.ShowCode();
                var service = this.AutofacContainer.ResolveNamed<IMyService>("service2"); //通过命名方式获取注册服务
                service.ShowCode();
    
                #region 子容器
    
                using (var myscope = AutofacContainer.BeginLifetimeScope("myscope"))
                {
                    //MyNameService在子容器的生命周期中无论创建多少次都是同一个对象
                    //因为MyNameService被注册到子容器myscope中了
    
                    var service0 = myscope.Resolve<MyNameService>();
                    using (var scope = myscope.BeginLifetimeScope())
                    {
                        var service1 = scope.Resolve<MyNameService>();
                        var service2 = scope.Resolve<MyNameService>();
                        Console.WriteLine($"service1=service2:{service1 == service2}");
                        Console.WriteLine($"service1=service0:{service1 == service0}");
                    }
                }
                #endregion
            }
  • 相关阅读:
    最近几周的总结
    做游戏
    枚举 结构 .....
    抽象类
    面向对象过程
    构造函数 对象关系
    .net 第二周学习
    。net初学
    CSS基本知识点(01)
    C#之ADO.NET基本知识点(01)
  • 原文地址:https://www.cnblogs.com/Adoni/p/12651373.html
Copyright © 2020-2023  润新知