public static void RegisterAutofac()
        {
            var builder = new ContainerBuilder();

            // Register your MVC controllers.
            // If AutofacConfig's assembly doesn't reference the specific controllers assemblies, use these:
            // builder.RegisterControllers(typeof(MvcApplication).Assembly);
            // builder.RegisterControllers(typeof(BasePublicController).Assembly);
            // builder.RegisterControllers(typeof(BaseAdminController).Assembly);
            // ...
            builder.RegisterControllers(BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToArray());

            // 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();

            // Register services
            RegisterServices(builder);

            // Set the dependency resolver to be Autofac.
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            // Integration with Hangfire
            GlobalConfiguration.Configuration.UseAutofacActivator(container);
        }
Beispiel #2
0
 protected override void Load(ContainerBuilder builder)
 {
     builder.RegisterModelBinders(_mvcModule);
     builder.RegisterModelBinderProvider();
     builder.RegisterControllers(_mvcModule);
     builder.RegisterModule(new AutofacWebTypesModule());
 }
Beispiel #3
0
        protected void BuildContainer()
        {
            var builder = new Autofac.ContainerBuilder();

            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();

            builder.RegisterAssemblyTypes(typeof(ChangeNameCommandHandler).Assembly).Where(x => x.Name.EndsWith("Handler")).AsImplementedInterfaces();

            RegisterMediatr(builder);

            // Set the dependency resolver to be Autofac.
            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        /// <summary>
        /// 创建 MVC容器(包含Filter)
        /// </summary>
        public static void BuildMvcContainer()
        {
            var builder = new ContainerBuilder();
            //注册Module方法2 在Web.config中配制方式
            builder.RegisterModule(new ConfigurationSettingsReader("autofacMvc"));
            //加载 *.Controllers 层的控制器,否则无法在其他层控制器构造注入,只能在web层注入
            Assembly[] asm = GetAllAssembly("*.Controllers.dll").ToArray();
            builder.RegisterAssemblyTypes(asm);
            //注册仓储
            Assembly[] asmRepository = GetAllAssembly("*.Repository.dll").ToArray();
            builder.RegisterAssemblyTypes(asmRepository)
               .Where(t => t.Name.EndsWith("Repository"))
               .AsImplementedInterfaces();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();


            //注册过滤器 
            builder.RegisterFilterProvider();
            builder.RegisterType<OperateAttribute>().PropertiesAutowired();//非global filter  属性注入
            builder.RegisterType<GlobalFilterAttribute>().SingleInstance();//global filter 注入
            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleTable.EnableOptimizations = true;
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            //*********************************
            //Autofac specific code
            var builder = new ContainerBuilder();
            builder.RegisterType<MemoryCacheProvider>().As<ICacheProvider>().InstancePerLifetimeScope();
            builder.RegisterType<StateRepository>().As<IStateRepository>().InstancePerLifetimeScope();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            //Register model binders (if you use them)
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());

            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            //*********************************
            //ViewEngines.Engines.Clear();
            var webFormsViewEngine = ViewEngines.Engines.OfType<WebFormViewEngine>().FirstOrDefault();
            if (webFormsViewEngine != null)
            {
                ViewEngines.Engines.Remove(webFormsViewEngine);
            }

            MiniProfilerEF.Initialize();
        }
        public static void RegisterAutofac()
        {
            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();

            // Register services
            RegisterServices(builder);

            // Set the dependency resolver to be Autofac.
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        public static void Config()
        {
            var builder = new ContainerBuilder();

            // Register MVC controllers.
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterApiControllers(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();

            var container = Bootstrap.ServiceLocator.Instance.InitializeIOC(builder);

            // Set the dependency resolver to be Autofac.
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
Beispiel #8
0
        protected void Application_Start(object sender, EventArgs e)
        {
            var builder = new ContainerBuilder();

            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<AuthenticationService>().As<IAuthenticationService>();
            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterModule<NHibernateModule>();

            // Change controller action parameter injection by changing web.config.
            builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>().InstancePerHttpRequest();

            // MVC integration test items
            builder.RegisterType<InvokerDependency>().As<IInvokerDependency>();

            // DomainServices
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AssignableTo<DomainService>();
            builder.RegisterModule<AutofacDomainServiceModule>();

            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            DomainService.Factory = new AutofacDomainServiceFactory(new MvcContainerProvider());

            RegisterRoutes(RouteTable.Routes);
        }
Beispiel #9
0
        private void CreateContainer(HttpConfiguration configuration,IApplicationHost applicationHost,IConfigurationProvider configurationProvider)
        {
            var assemblies          = applicationHost.Assemblies.ToArray();
            var containerBuilder    = new ContainerBuilder();

            containerBuilder.RegisterModelBinderProvider();
            //containerBuilder.RegisterFilterProvider(); TODO: Seems to be an autofac MVC4 integration issue
            containerBuilder.RegisterModelBinders(assemblies);
            containerBuilder.RegisterApiControllers(assemblies);
            containerBuilder.RegisterControllers(assemblies);
            containerBuilder.RegisterWebApiFilterProvider(configuration);
            containerBuilder.RegisterWebApiModelBinders(assemblies);
            containerBuilder.RegisterModule(new AutofacWebTypesModule());

            containerBuilder.RegisterAssemblyModules(assemblies);
            containerBuilder.RegisterAssemblyTypes(assemblies).Where(t => t.IsAssignableTo<IRouteProvider>()).As<IRouteProvider>();
            containerBuilder.RegisterAssemblyTypes(assemblies).Where(t => t.IsAssignableTo<IBootTask>()).As<IBootTask>();
            containerBuilder.RegisterAssemblyTypes(assemblies).Where(t => t.IsAssignableTo<AreaRegistration>()).As<AreaRegistration>();

            containerBuilder.RegisterInstance(applicationHost).As<IApplicationHost>();
            containerBuilder.RegisterInstance(configurationProvider).As<IConfigurationProvider>();

            var container = containerBuilder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            SignalR.GlobalHost.DependencyResolver = new AutofacSignalRDependencyResolver(container);
        }
Beispiel #10
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //Autofac Configuration
            var builder = new Autofac.ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
            builder.RegisterModelBinderProvider();
            builder.RegisterModule(new RepositoryModule());

            builder.RegisterModule(new ServiceModule());
            builder.RegisterModule(new EFModule());
            builder.RegisterModule(new LoggingModule());
            builder.RegisterModule(new AutoMapperModule());
            //builder.RegisterModule(new RedisModule());

            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
        }
        public static void Configure()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterModelBinders(typeof(MvcApplication).Assembly);

            builder
                .Register(c => NHibernateConfig.SessionFactory.OpenSession())
                .As<ISession>().InstancePerHttpRequest();

            builder.RegisterType<CsvFileTextService<ForwardTextCandidate>>().As<ITextService<ForwardTextCandidate>>();
            builder.RegisterType<CsvFileTextService<ReplyTextCandidate>>().As<ITextService<ReplyTextCandidate>>();

            builder.RegisterType<Repository<ForwardTextCandidate>>().As<IBaseRepository<ForwardTextCandidate>>();
            builder.RegisterType<Repository<ReplyTextCandidate>>().As<IBaseRepository<ReplyTextCandidate>>();

            builder.Register<ServerStatusService>(c=>new ServerStatusService(NHibernateConfig.SessionFactory));
            builder.RegisterType<UserService>();

            builder.RegisterType<ServerProvidersAdminSettingsController>();
            builder.RegisterType<UsersController>();

            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            // Create the dependency resolver.
            var resolver = new AutofacWebApiDependencyResolver(container);

            // Configure Web API with the dependency resolver.
            GlobalConfiguration.Configuration.DependencyResolver = resolver;

        }
        public static void Setup()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            // Register for API Controllers
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterFilterProvider();
            builder.RegisterModelBinderProvider();
            builder.RegisterModule<AutofacWebTypesModule>();

            // Find all autofac modules and include them.
            builder.RegisterModule<IdentityModule>();

            // Find all IStartable tasks and register them.
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            builder.RegisterAssemblyTypes(assemblies)
                .AssignableTo<IStartable>()
                .As<IStartable>()
                .SingleInstance();

            // Enable property injection into action filters (including authorize attribute).
            builder.RegisterFilterProvider();

            IContainer container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        public static IAppBuilder UseAutofacInjection(this IAppBuilder app, HttpConfiguration httpConfiguration)
        {
            var currentAssembly = Assembly.GetExecutingAssembly();

            var builder = new ContainerBuilder();

            // Web API
            builder.RegisterApiControllers(currentAssembly);
            builder.RegisterWebApiFilterProvider(httpConfiguration);

            // MVC
            builder.RegisterControllers(currentAssembly).PropertiesAutowired();

            builder.RegisterModelBinders(currentAssembly);
            builder.RegisterModelBinderProvider();

            builder.RegisterModule<AutofacWebTypesModule>();
            //builder.RegisterSource(new ViewRegistrationSource());

            builder.RegisterFilterProvider();

            builder.RegisterAssemblyModules(currentAssembly);
            
            // Hook it up
            var container = builder.Build();
            httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            // Register the Autofac middleware FIRST, then the Autofac MVC middleware.
            app.UseAutofacMiddleware(container);
            app.UseAutofacMvc();

            return app;
        }
        public static void RegisterDependencies()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<Config>().As<IConfig>();
            builder.RegisterType<JsonConverter>().As<IConverter>().InstancePerRequest();
            builder.RegisterType<CustomWebClient>().As<IWebClient>().InstancePerRequest();
            builder.RegisterType<GitHubService>().As<IVcsService<GitHubUser, GitHubRepository>>().InstancePerRequest();

            // 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));
        }
Beispiel #15
0
        protected void Application_Start()
        {

            var builder = new ContainerBuilder();

            BuildMappings(builder);

            // 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));

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            ConfigureLogging();
        }
        public static IContainer ConfigureContainer()
        {
            var _builder = new ContainerBuilder();

            //Call into our data project and let it register it's common services into the same builder
            TypeScript.Demo.Data.AutofacConfig.RegisterCommonServices(_builder);


            //This code is fairly stock code demonstrating how to tightly integrate Autofac with MVC and WebAPI
            _builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            _builder.RegisterModelBinders(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            _builder.RegisterModelBinderProvider();
            _builder.RegisterModule(new AutofacWebTypesModule());
            _builder.RegisterSource(new ViewRegistrationSource());
            _builder.RegisterFilterProvider();
            _builder.RegisterWebApiModelBinders(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            _builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();

            var _container = _builder.Build();

            // Tell ASP.NET MVC to use Autofac to resolve components
            DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));

            // Create the depenedency resolver for web API
            var resolver = new AutofacWebApiDependencyResolver(_container);

            // Configure Web API with the dependency resolver.
            GlobalConfiguration.Configuration.DependencyResolver = resolver;

            return _container;
        }
Beispiel #17
0
        public static void RegisterTypes()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(ContainerConfig).Assembly);

            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();
            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterSource(new ViewRegistrationSource());
            builder.RegisterFilterProvider();

            builder.Register(x => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
            builder.RegisterType<WorkaroundEmailService>().As<IEmailService>().SingleInstance();

            RegisterServices(builder);
            RegisterBackgroundTasks(builder);

            builder.RegisterType<AuctioneerDbContext>().InstancePerRequest();

            builder.RegisterType<BreadcrumbBuilder>().As<IBreadcrumbBuilder>().InstancePerDependency();
            builder.RegisterType<AuthenticationManager>().As<IAuthenticationManager>().InstancePerRequest();
            builder.RegisterType<EmailUserNotifier>().As<IUserNotifier>().SingleInstance();
            builder.RegisterType<LanguageService>().As<ILanguageService>().SingleInstance();
            builder.RegisterType<AuctionService>().As<IAuctionService>().InstancePerRequest().WithParameters(new Parameter[]
            {
                new NamedParameter("photoDirectoryPath",     HostingEnvironment.MapPath("~/Content/UserContent/Auctions/Photos")),
                new NamedParameter("thumbnailDirectoryPath", HostingEnvironment.MapPath("~/Content/UserContent/Auctions/Thumbnails"))
            });

            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        public static void Setup()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();
            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterSource(new ViewRegistrationSource());
            builder.RegisterFilterProvider();
            builder.RegisterType<PremiumCalculationService>().As<IPremiumCalculationService>();
            builder.Register(context => new MapperConfiguration(configuration =>
            {
                configuration.AddProfile(new AutoMapperProfile());
            }))
               .AsSelf()
               .SingleInstance();

            builder.Register(context => context.Resolve<MapperConfiguration>()
                .CreateMapper(context.Resolve))
                .As<IMapper>()
                .InstancePerLifetimeScope();

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        public static void RegisterAutoFac()
        {
            var builder = new ContainerBuilder();

            // MVC
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());

            // WebApi
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
            builder.RegisterModule<AutofacWebTypesModule>();

            // Register services
            AddRegistrations(builder);
            var container = builder.Build();

            // Set the dependency resolver for Web API.
            var webApiResolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;

            // Set the dependency resolver for MVC.
            var mvcResolver = new AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(mvcResolver);
        }
        protected void RegisterServices()
        {
            var tenantIdStrategy = new RouteDataItemTenantIdentificationStrategy("tenant");

            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();

            // Create the multitenant container and the tenant overrides.
            var mtc = new MultitenantContainer(tenantIdStrategy, builder.Build());
            AddTenant(mtc, "a");
            AddTenant(mtc, "b");

            DependencyResolver.SetResolver(new AutofacDependencyResolver(mtc));
        }
Beispiel #21
0
        public static void BuildContainer()
        {
            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();

            // Add our own components
            builder.RegisterType<NameResolver>().As<INameResolver>();

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .Where(x=>x.Name.EndsWith("Service"))
                .AsImplementedInterfaces();

            // Set the dependency resolver to be Autofac.
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        public static IContainer Initialize(ContainerBuilder builder)
        {
            // Register all controllers
            builder.RegisterControllers(typeof(Startup).Assembly)
                .OnActivating(e =>
                {
                    var controller = (Controller)e.Instance;
                    controller.TempDataProvider = new CookieTempDataProvider();
                });

            // Register model binders
            builder.RegisterModelBinders(typeof(Startup).Assembly);
            builder.RegisterModelBinderProvider();

            // Register all HTTP abstractions
            builder.RegisterModule<AutofacWebTypesModule>();

            //Register mapper module
            builder.RegisterModule(new MappingModule());

            // Allow property injection in views
            builder.RegisterSource(new ViewRegistrationSource());

            // Allow property injection in action filters
            builder.RegisterFilterProvider();

            // Register all Autofac specific IModule implementations
            builder.RegisterAssemblyModules(typeof(Startup).Assembly);

            return builder.Build();
        }
Beispiel #23
0
        public ContainerBuilder BuildAutofac(bool isTestLoad = false)
        {
            var builder = new ContainerBuilder();
            Assembly appAssembly = typeof(MvcApplication).Assembly;
            builder.RegisterAssemblyTypes(appAssembly);
            builder.RegisterControllers(appAssembly);
            builder.RegisterModelBinders(appAssembly);
            builder.RegisterModelBinderProvider();
            builder.RegisterModule(new AutofacWebTypesModule());

            string serverMapPath = string.Empty;
            if (!isTestLoad)
            {
                serverMapPath = Server.MapPath("~");
            }

            builder.Register<DemoConfig>(ctx =>
            {
                var env = ConfigurationManager.AppSettings["Env"];
                var cll = new Services.ConfigLoader(env, serverMapPath);
                return cll.DemoConfig;
            }).SingleInstance();

            builder.Register<LoggingDBDataContext>(ctx => new LoggingDBDataContext(ctx.Resolve<DemoConfig>().LoggingDBConnectionString));
            builder.Register<ProductEF>(ctx => new ProductEF(ctx.Resolve<DemoConfig>().ProductEFConnectionString));

            return builder;
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            var builder = new ContainerBuilder();
            builder.RegisterModelBinderProvider();
            builder.RegisterModule(new AutofacWebTypesModule());
            builder.RegisterSource(new ViewRegistrationSource());
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());

            builder.RegisterType<InMemoryPersonRepository>().As<IPersonRepository>();

            // Use application-scoped variable to mimic in-memory database
            var personDatabase = new List<Person>();
            builder.Register(c => personDatabase).As<List<Person>>();

            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            ModelMetadataProviders.Current = new MetadataProvider();
        }
        private static IContainer BuildAutofacContainer(IAppBuilder app)
        {
            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();

            //Register Dependencies
            RegisterIdentityParts(app, builder);
            RegisterImageParts(app, builder);
            RegisterContentParts(app, builder);

            return builder.Build();
        }
Beispiel #26
0
        public static void RegisterBindings()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            builder.RegisterModule(new Vintage.Rabbit.CQRS.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Carts.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Common.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Products.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Caching.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Messaging.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Orders.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Membership.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Inventory.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Blogs.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Themes.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Search.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Logging.Ioc.Bindings());
            builder.RegisterModule(new Vintage.Rabbit.Parties.Ioc.Bindings());

            builder.RegisterType<LoginProvider>().As<ILoginProvider>();

            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinderProvider();

            builder.RegisterFilterProvider();

            builder.RegisterType<HandleExceptionAttribute>().AsExceptionFilterFor<Controller>().InstancePerRequest().PropertiesAutowired();

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
Beispiel #27
0
        private void SetDependencyResolver()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            DependencyResolver.SetResolver(new AutofacDependencyResolver(builder.Build()));
        }
 protected override void Load(ContainerBuilder builder)
 {
     builder.RegisterType<ExchangeRatesVm>().AsSelf();
     builder.RegisterType<ModelBinder>().AsImplementedInterfaces();
     builder.RegisterControllers(GetType().Assembly);
     builder.RegisterModelBinders(GetType().Assembly);
     builder.RegisterModelBinderProvider();
 }
		public void Compose(ContainerBuilder builder)
		{
			var assembly = typeof(WebContainerBuilderComposition).Assembly;
			builder.RegisterControllers(assembly).PropertiesAutowired();
			builder.RegisterModelBinders(assembly);
			builder.RegisterModelBinderProvider();
			builder.RegisterFilterProvider();
			builder.RegisterInstance<ObjectCache>(MemoryCache.Default);
		}
Beispiel #30
0
 private static void RegisterInfrastructure(ContainerBuilder builder)
 {
     builder.RegisterControllers(Assembly.GetExecutingAssembly());
     builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
     builder.RegisterModelBinderProvider();
     builder.RegisterModule<AutofacWebTypesModule>();
     builder.RegisterSource(new ViewRegistrationSource());
     builder.RegisterFilterProvider();
 }
Beispiel #31
0
 protected override void Load(ContainerBuilder builder)
 {
     var assembly = typeof(Application).Assembly;
     builder.RegisterControllers(assembly);
     builder.RegisterModelBinders(assembly);
     builder.RegisterModelBinderProvider();
     builder.RegisterModule(new AutofacWebTypesModule());
     builder.RegisterFilterProvider();
 }
 private static IContainer CreateContainer()
 {
     var builder = new ContainerBuilder();
     builder.RegisterControllers(typeof (MvcApplication).Assembly);
     builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
     builder.RegisterModelBinderProvider();
     builder.RegisterFilterProvider();
     builder.RegisterModule<AzureModule>();
     return builder.Build();
 }
Beispiel #33
0
        protected override void Load(Autofac.ContainerBuilder builder)
        {
            try
            {
                //注册无效的类型
                RegisterInvalidTypes();

                Assembly[] assemblies = SystemHelper.LoadAppAssemblies();
                builder.RegisterAssemblyTypesWithLiftTimeScope(assemblies).AsImplementedInterfaces().AsSelf();

                //注册WCF的契约
                //builder.RegisterWCFContract(assemblies, m => m.Location.EndsWith(".Service.Interface.dll", StringComparison.CurrentCultureIgnoreCase));

                //注册ModelBinders
                builder.RegisterModelBinders(typeof(Int32ListModelBinder).Assembly);
                builder.RegisterModelBinderProvider();
                //注册Controllers
                builder.RegisterControllers(assemblies, m => m.Location.EndsWith(".Web.dll", StringComparison.CurrentCultureIgnoreCase));
                //注册路由
                builder.RegisterInstance(RouteTable.Routes).As <RouteCollection>();
                //注册GlobalFilters
                builder.RegisterInstance(GlobalFilters.Filters).As <GlobalFilterCollection>();
                //注册ModelBinders
                builder.RegisterInstance(ModelBinders.Binders);
                #region
                ////注册缓存
                //builder.RegisterType<DefaultCacheProvider>().Named<ICacheProvider>("aspnet").AsSelf().SingleInstance();
                //builder.RegisterType<AppFabricCacheProvider>().Named<ICacheProvider>("appfabric").AsSelf().SingleInstance();

                ////注册日志
                //builder.RegisterLogger();
                #endregion
            }
            catch (Exception ex)
            {
                throw;
            }
        }