IOC框架-AutoFac
Extensions层通过Nuget安装Autofac.Extras.DynamicProxy,Autofac.Extensions.DependencyInjection,
添加项目引用
新建ServiceExtensions文件夹,完成AutoFacModuleRegister
public class AutofacModuleRegister : Autofac.Module { protected override void Load(ContainerBuilder builder) { //builder.RegisterType<ArticleService>().As<IArticleService>(); var cacheType = new List<Type>(); if (AppSettings.app(new string[] { "Appsettings", "LogAOP", "Enabled" }).ObjToBool()) { builder.RegisterType<BlogLogAop>(); cacheType.Add(typeof(BlogLogAop)); } if (AppSettings.app(new string[] { "Appsettings", "MemoryCachingAOP", "Enabled" }).ObjToBool()) { builder.RegisterType<BlogCacheAop>(); cacheType.Add(typeof(BlogCacheAop)); } var assemblyServices = Assembly.Load("SwiftCode.BBS.Services"); builder.RegisterAssemblyTypes(assemblyServices).AsImplementedInterfaces().EnableInterfaceInterceptors().InterceptedBy(cacheType.ToArray()); var assemblyRepositories = Assembly.Load("SwiftCode.BBS.Repositories"); builder.RegisterAssemblyTypes(assemblyRepositories).AsImplementedInterfaces().InstancePerLifetimeScope(); } }
test在Program.cs中新增
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
注册服务
builder.Host.ConfigureContainer<ContainerBuilder>(builder => { builder.RegisterModule<AutofacModuleRegister>(); });