ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Autofac 整合asp.net core6

2022-06-19 15:00:39  阅读:182  来源: 互联网

标签:Autofac asp option Business RegisterType builder using core6


1、Nuget引入

Autofac、Autofac.Extensions.DependencyInjection

2、Program.cs里面添加注入配置

{
    //第一种方式注入
    builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());  //通过工厂替换,把Autofac整合进来
    builder.Host.ConfigureContainer<ContainerBuilder>(option =>
    {
        option.RegisterType<Microphone>().As<IMicrophone>();
        option.RegisterType<Power>().As<IPower>();
        option.RegisterType<Headphone>().As<Headphone>();
        option.RegisterType<Headphone>().As<IHeadphone>();
    });

    ////第二种方式
    //builder.Host
    //.UseServiceProviderFactory(new AutofacServiceProviderFactory())
    //.ConfigureContainer<ContainerBuilder>(builder =>
    //{
    //    builder.RegisterModule(new AutofacModuleRegister());
    //});
}

第二种方式,是新建一个AutofacModuleRegister类

 1 using Autofac;
 2 using Business.IServices;
 3 using Business.Services;
 4 using System.Reflection;
 5 
 6 namespace ProjectIOC.Extensions
 7 {
 8     public class AutofacModuleRegister : Autofac.Module
 9     {
10         /// <summary>
11         /// 重新Autofac管道load方法,在这里注册注入
12         /// </summary>
13         /// <param name="builder"></param>
14         protected override void Load(ContainerBuilder builder)
15         {
16             //程序集
17             var IAppServices = Assembly.Load("Business.IServices");
18             var AppServices = Assembly.Load("Business.Services");
19 
20             //根据名称约定(服务层的接口和实现均以Service结尾),实现服务接口和服务实现的依赖
21             builder.RegisterAssemblyTypes(IAppServices, AppServices)
22                 .Where(t => t.Name.EndsWith("Service"))
23                 .AsImplementedInterfaces();
24 
25             builder.RegisterType<Microphone>().As<IMicrophone>().SingleInstance();
26         }
27     }
28 }
View Code

在类里面添加注入

3、接下来就可以在控制器的构造函数里面添加依赖注入

 1 using Autofac;
 2 using Business.IServices;
 3 using Microsoft.AspNetCore.Mvc;
 4 
 5 namespace ProjectIOC.Controllers
 6 {
 7     public class HomeController : Controller
 8     {
 9         private readonly IMicrophone _microphone;
10         private readonly IMicrophone _microphone2;
11         private readonly IMicrophone _microphone3;
12 
13         public HomeController(IMicrophone microphone,IServiceProvider serviceProvider,IComponentContext componentContext)
14         {
15             this._microphone = microphone;
16             this._microphone2 = serviceProvider.GetService<IMicrophone>();
17             this._microphone3 = componentContext.Resolve<IMicrophone>();
18         }
19 
20         public IActionResult Index()
21         {
22             return View();
23         }
24     }
25 }
View Code

 

标签:Autofac,asp,option,Business,RegisterType,builder,using,core6
来源: https://www.cnblogs.com/handsomeziff/p/16390502.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有