public class MvcApplication : System.Web.HttpApplication { protectedvoidApplication_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // 開始建立 Container var builder = new ContainerBuilder(); // Register your MVC controllers. (MvcApplication is the name of // the class in Global.asax.) builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterType<DateService>() .As<IDateService>() .InstancePerLifetimeScope(); // OPTIONAL: 註冊 web abstractions builder.RegisterModule<AutofacWebTypesModule>(); // OPTIONAL: 啟用 property injection into action filters. builder.RegisterFilterProvider(); // Set the dependency resolver to be Autofac. var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } }