03_NET6中使用Autofac

安装包:

Autofac

Autofac.Extensions.DependencyInjection

Autofac.Extras.DynamicProxy

中文文档:欢迎来到 Autofac 中文文档! — Autofac 4.0 documentation (autofac-.readthedocs.io)

 

1.新建一个web api项目

新建一个AutofacMamager类,用于注册

 public class AutofacMamager
    {
        public class AutoFacManager : Module
        {
            // 重写 Autofac 管道 Load方法, 在这里注入
            protected override void Load(ContainerBuilder builder)
            {
                /*
                1.瞬态
                 InstancePerDependency();

                2.单例
                SingleInstance();

                3.每个生命周期范围一个实例
                InstancePerLifetimeScope();
                 */

                /*载程序集注册*/
                // 加载程序集 
                Assembly ifirstGrade = Assembly.Load("IModels");
                // 添加程序集依赖
                builder.RegisterAssemblyTypes(ifirstGrade)
                    .Where(t => t.Name.EndsWith("Services"))//对象名后面有Services
                    .AsImplementedInterfaces();//.InstancePerDependency();

                
                Assembly firstGrade = Assembly.Load("Models");
                builder.RegisterAssemblyTypes(firstGrade)
                    .Where(t => t.Name.EndsWith("Services"))//对象名后面有Services
                    .AsImplementedInterfaces();//.InstancePerDependency();


                /*注册具体类*/
                //builder.RegisterType<Student>();
                //builder.RegisterType(typeof(Student));

                /*注册实例*/
                builder.RegisterType<Student>().As<IStudent>();

                base.Load(builder);
            }
        }
    }

 

2.创建一个AutofacExtend类,用于替换

 public static class AutofacExtend
    {
        public static void ReplaceAutofac(this WebApplicationBuilder builder)
        {
            // 替换Autofac
            builder.Host
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureContainer<ContainerBuilder>(build =>
                            {
                                build.RegisterModule(new AutoFacManager());
                            });
        }
    }

 

3.修改Program.cs

            //Autofac
            builder.ReplaceAutofac();
            var app = builder.Build();

 

 

4.创建一个控制器用户测试

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private IStudent _student;
        private ISchoolboyServices _schoolboy;

        public UserController(IStudent student, ISchoolboyServices schoolboy)
        {
            _student=student;
            _schoolboy=schoolboy;
        }

        /// <summary>
        /// 获取学生名称
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string GetStudentName()
        {
            return _student.GetName();
        }

        /// <summary>
        /// 一年级学生名称
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string GetSchoolboyName()
        {
            return _schoolboy.Show();
        }
    }

 

posted @ 2024-04-13 12:48  野码  阅读(90)  评论(0编辑  收藏  举报