标签:
var builder = new ContainerBuilder();
var container = builder.Build();
var assemblies = new DirectoryInfo(
AppDomain.CurrentDomain.BaseDirectory)
.GetFiles("ServiceLeyer.dll")
.Select(r => Assembly.LoadFrom(r.FullName)).ToArray();
builder.RegisterAssemblyTypes(assemblies);
//有拦截器
builder1.RegisterAssemblyTypes(assemblies).As<ICategoryService>().EnableInterfaceInterceptors().InterceptedBy(typeof(CallLog));
builder1.RegisterAssemblyTypes(assemblies).EnableClassInterceptors().InterceptedBy(typeof(CallLog));
//无拦截器
builder1.RegisterAssemblyTypes(assemblies).As<ICategoryService>();
builder1.RegisterAssemblyTypes(assemblies);
builder1.RegisterType<CallLog>();
builder1.Update(container);
//SingleInstance()单例使用。
//动态更新容器里的对象。
var builder1 = new ContainerBuilder();
builder1.RegisterType<CategoryService>().As <ICategoryService>()
.EnableClassInterceptors()
.InterceptedBy(typeof(CallLog));
builder1.Update(container);
//MVC注入
var builder = new ContainerBuilder();
// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// OPTIONAL: Register model binders that require DI.
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();
// OPTIONAL: Register web abstractions like HttpContextBase.
builder.RegisterModule<AutofacWebTypesModule>();
// OPTIONAL: Enable property injection in view pages.
builder.RegisterSource(new ViewRegistrationSource());
// OPTIONAL: Enable property injection into action filters.
builder.RegisterFilterProvider();
// Set the dependency resolver to be Autofac.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
标签:
原文地址:http://www.cnblogs.com/wolf12/p/5521001.html