一:
var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest(); builder.RegisterType<Web_ExceptionLogManager>().As<IWeb_ExceptionLogManager>().InstancePerHttpRequest(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes);
二: var builder = new ContainerBuilder(); builder.RegisterType<NewsRepository>().As<INewsRepository>(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
三:
protected void Application_Start() { var builder = new ContainerBuilder(); builder.RegisterModule(new FrameworkModule()); builder.RegisterModule(new **Module()); Assembly[] assemblies = ModuleManager.GetAllModuleAssembly().ToArray(); builder.RegisterAssemblyTypes(assemblies); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterModelBinders(Assembly.GetExecutingAssembly()); builder.RegisterModelBinderProvider(); builder.RegisterModule(new ConfigurationSettingsReader("*****")); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
public class **Module : Module { public static string **Context { get { return "**Context"; } } protected override void Load(ContainerBuilder builder) { builder.RegisterType<**Repository>().As<I**Repository>();base.Load(builder); }