[Asp.Net Core]Autofac整合.NET5 MVC

Posted 厦门德仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Asp.Net Core]Autofac整合.NET5 MVC相关的知识,希望对你有一定的参考价值。

1.指定Autofac工厂替换默认工厂,Program指定

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                //.ConfigureLogging(loggingBuilder=> 
                //  //配置log4Net配置文件的读取
                //    loggingBuilder.AddLog4Net("CfgFile/log4net.Config");
                //) 
                .ConfigureWebHostDefaults(webBuilder =>
                
                    webBuilder.UseStartup<Startup>();
                ).UseServiceProviderFactory(new AutofacServiceProviderFactory());
    

2.在Startup类增加ConfigureContainer 方法, 注册关系

        /// <summary>
        /// 整个方法被autofact自动调用
        /// 负责注册各种服务
        /// </summary>
        /// <param name="containerBuilder"></param>
        public void ConfigureContainer(ContainerBuilder containerBuilder)
        
            containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>();
            containerBuilder.RegisterType<TestServiceB>().As<ITestServiceB>();
            containerBuilder.RegisterType<TestServiceC>().As<ITestServiceC>();
            containerBuilder.RegisterType<TestServiceD>().As<ITestServiceD>().PropertiesAutowired();
            containerBuilder.RegisterType<TestServiceE>().As<ITestServiceE>();

        

3.通过控制器构造函数注入,获取实例

 public class SixThController : Controller
    
        private readonly ITestServiceA _iTestServiceA;
        private readonly ITestServiceB _iTestServiceB;
        private readonly ITestServiceC _iTestServiceC;
        private readonly ITestServiceD _iTestServiceD;
        private readonly ITestServiceE _iTestServiceE;
        public SixThController(
            ITestServiceA iTestServiceA,
            ITestServiceB iTestServiceB,
            ITestServiceC iTestServiceC,
            ITestServiceD iTestServiceD,
            ITestServiceE iTestServiceE) 
        
            _iTestServiceA = iTestServiceA;
            _iTestServiceB = iTestServiceB;
            _iTestServiceC =iTestServiceC;
            _iTestServiceD = iTestServiceD;
            _iTestServiceE = iTestServiceE;
            
        
        public IActionResult Index()
        
            _iTestServiceD.Show();
            return View();
        
    

4.ServiceCollection注册的服务也可以让Autofac使用,因为Autofac在自己注册服务之前;会先把ServiceCollection中注册的服务全部接管过来;

            #region ServiceCollection注册的服务也可以让Autofac使用,因为Autofac在自己注册服务之前;会先把ServiceCollection中注册的服务全部接管过来;
            //services.AddTransient<ITestServiceA, TestServiceA>();
            //services.AddTransient<ITestServiceB, TestServiceB>();
            //services.AddTransient<ITestServiceC, TestServiceC>();
            #endregion

以上是关于[Asp.Net Core]Autofac整合.NET5 MVC的主要内容,如果未能解决你的问题,请参考以下文章

[Asp.Net Core]Autofac多种注入

[Asp.Net Core]Autofac多种注入

[Asp.Net Core]Autofac支持配置文件

[Asp.Net Core]Autofac支持配置文件

ASP.NET Core Web 应用程序系列- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)

[Asp.Net Core]Autofac单抽象多实例属性注入