Skip to content

wenjieObject/AutofacDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutofacDemo

.net core AutoFac实现依赖注入和批量注入 https://blog.csdn.net/huanghuangtongxue/article/details/78914306 1.在Startup文件中注册autofac容器自动替换系统自带容器 //注册变量 public static IContainer AutofacContainer;

在ConfigureServices方法下创建容器,返回值修改为IServiceProvider ContainerBuilder builder = new ContainerBuilder(); //将services中的服务填充到Autofac中. builder.Populate(services); //新模块组件注册 builder.RegisterModule(); //创建容器. AutofacContainer = builder.Build(); //使用容器创建 AutofacServiceProvider return new AutofacServiceProvider(AutofacContainer); 2.新建DefaultModuleRegister类,继承autofac的Module

注册当前程序集中以“User”结尾的类,暴漏类实现的所有接口,生命周期为PerLifetimeScope var baseType = typeof(IUser);

        builder.RegisterAssemblyTypes(typeof(User).Assembly)
             .Where(t => t.Name.EndsWith("User"))
             .Where(m => baseType.IsAssignableFrom(m) && m != baseType)
             .AsImplementedInterfaces();

3.通过构造函数注入

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages