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 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 #3
0
        private static void SetupContainer()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType<Logger>().As<ILogger>();

            builder.RegisterType<AutofacServiceProvider>()
                .As<Xdore.Core.IServiceProvider>()
                .InstancePerLifetimeScope();

            #region Modules

            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterModule<DataAccessRegistrationModule>();
            builder.RegisterModule<ServicesRegistrationModule>();

            #endregion

            builder.RegisterControllers(Assembly.GetExecutingAssembly())
                .PropertiesAutowired();

            builder.RegisterFilterProvider();

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

            ServiceLocator.Initialize(() =>
            {
                return DependencyResolver.Current.GetService<IServiceProvider>();
            });

            IDbContextFactory dbFactory = DependencyResolver.Current.GetService<IDbContextFactory>();
            dbFactory.InitDbContext();
        }
        public static IContainer ConfigureContainerWebApi()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(AutofacConfig).Assembly).PropertiesAutowired();

            builder.RegisterSource(new ViewRegistrationSource());

            // Register dependencies in filter attributes
            builder.RegisterFilterProvider();

            // Register our Data dependencies
            builder.RegisterModule(new DataModule(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString));

            //builder.RegisterModule(new MvcSiteMapProviderModule());

            // Register Common module
            builder.RegisterModule(new CommonModule());

            builder.RegisterModule(new ServiceModule());

            // OPTIONAL: Register web abstractions like HttpContextBase.
            builder.RegisterModule<AutofacWebTypesModule>();

            // Set MVC DI resolver to use our Autofac container
            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            return container;
        }
        public void Configuration(IAppBuilder app)
        {
            var builder = new ContainerBuilder();
            var config = new HttpConfiguration();
            // Identity Dependencies
            builder.RegisterType<ApplicationDbContext>().AsSelf().InstancePerLifetimeScope();
            builder.RegisterType<ApplicationUserStore>().As<IUserStore<ApplicationUser>>().InstancePerLifetimeScope();
            builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerLifetimeScope();
            builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerLifetimeScope();
            builder.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication);

            // Web Application Dependencies
            builder.RegisterType<StoreContext>().AsSelf().InstancePerRequest();
            builder.RegisterType<StoreItemService>().AsSelf();
            builder.RegisterType<ReceivingItemService>().AsSelf();

            // Autofac MVC Boilerplate
            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterFilterProvider();
            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            //Autofac WebAPI2 Boilerplate
            builder.RegisterApiControllers(typeof(MvcApplication).Assembly);
            builder.RegisterWebApiFilterProvider(config);

            var container = builder.Build();
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            app.UseAutofacMiddleware(container);
            app.UseAutofacMvc();
            app.UseAutofacWebApi(config);
            ConfigureAuth(app);
        }
        private static void SetAutofacContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();

            builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();

            builder.RegisterAssemblyTypes(typeof(CompanyRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces().InstancePerHttpRequest();

            builder.RegisterAssemblyTypes(typeof(CompanyService).Assembly)
               .Where(t => t.Name.EndsWith("Service"))
               .AsImplementedInterfaces().InstancePerHttpRequest();

            builder.RegisterAssemblyTypes(typeof(DefaultFormsAuthentication).Assembly)
             .Where(t => t.Name.EndsWith("Authentication"))
             .AsImplementedInterfaces().InstancePerHttpRequest();

            builder.Register(c => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ArchitectureEntities())))
                .As<UserManager<ApplicationUser>>().InstancePerHttpRequest();

            builder.RegisterFilterProvider();

            IContainer container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
Beispiel #7
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));
        }
Beispiel #8
0
        public static void Run()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).OnActivated(e =>
            {
                var viewBag =
                    ((Controller)
                     e.Instance).ViewBag;
                viewBag.Version =
                    Assembly.
                        GetExecutingAssembly
                        ().GetName().
                        Version.ToString(3);
            });

            builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>();
            builder.RegisterType<ModelReader>().As<IModelReader>();
            builder.RegisterType<CryptoService>().As<ICryptoService>().InstancePerHttpRequest().InstancePerApiRequest();
            builder.RegisterType<Mapper>().As<IMapper>().InstancePerHttpRequest().InstancePerApiRequest();
            builder.RegisterType<LogManager>().As<ILogManager>().InstancePerHttpRequest().InstancePerApiRequest();

            builder.RegisterFilterProvider();
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
Beispiel #9
0
        public static void RegisterComponents() {
            var builder = new ContainerBuilder();

            AppStarter.CreateHostContainer(builder, BuildManager.GetReferencedAssemblies().Cast<Assembly>());

            #region register filter
            builder.Register(c => new MvcAuthorizationAttribute {
                Logger = c.Resolve<ILogger>(new TypedParameter(typeof(Type), typeof(MvcAuthorizationAttribute)))
            }).AsAuthorizationFilterFor<AdminController>().InstancePerRequest();

            builder.Register(c => new MvcResultFilterAttribute {
                Logger = c.Resolve<ILogger>(new TypedParameter(typeof(Type), typeof(MvcResultFilterAttribute))),
                UnitOfWork = c.Resolve<IUnitOfWork>()
            }).AsActionFilterFor<AdminController>().InstancePerRequest();

            builder.Register(c => new InitialConfigurationFilter("Initial", "Index") {
                Logger = c.Resolve<ILogger>(new TypedParameter(typeof(Type), typeof(InitialConfigurationFilter))),
                SettingManager = c.Resolve<ISettingManager>()
            }).AsActionFilterFor<Controller>().InstancePerRequest();

            #endregion

            builder.RegisterFilterProvider();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            builder.Register(ctx => RouteTable.Routes).SingleInstance();
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        public static void RegisterIoc()
        {
            var builder = new ContainerBuilder();

            // registrar autofac para resolver las dependencías de los controladores
              builder.RegisterControllers(typeof(Tarea.Web.Controllers.TareaController).Assembly);
            // registrar para IDatabase - 1 Instancia por request
            builder.Register(c => new Database("Default"))
                .As<IDatabase>()
                .SingleInstance();
            // registar el servicio

            builder.RegisterType<TareaServicio2>().
                As<ITareaServicio>().
                InstancePerRequest();

            builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerRequest();
            builder.RegisterType<Uow>().As<IUnitOfWork>().InstancePerRequest();

            // registar para los filters (aun no tenemos)
            builder.RegisterFilterProvider();

            builder.RegisterApiControllers(typeof(Tarea.Web.Controllers.TareaDataController).Assembly);
            // completar la config de autofac
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
        }
Beispiel #11
0
        public static void Start()
        {
            var builder = new ContainerBuilder();
              builder.RegisterFilterProvider();
              builder.RegisterControllers(Assembly.GetExecutingAssembly());

              builder.RegisterType<AppFormsAuthentication>().As<IFormsAuthentication>().InstancePerHttpRequest();
              builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Mailer")).
            AsImplementedInterfaces().InstancePerHttpRequest();

              builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
              builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();

              // Register Repositories
              builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces().InstancePerHttpRequest();

              // Register Services
              builder.RegisterAssemblyTypes(typeof(AccountTasks).Assembly)
            .Where(t => t.Name.EndsWith("Tasks"))
            .AsImplementedInterfaces().InstancePerHttpRequest();

              var container = builder.Build();
              DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
Beispiel #12
0
        /// <summary>
        /// Registers services into ioc container. 
        /// </summary>
        private static void RegisterTypes(ContainerBuilder builder, Assembly callingAssembly, IEnumerable<string> bootTaskAssemblies)
        {
            builder.RegisterControllers(callingAssembly);
            builder.RegisterFilterProvider();

            // Perform Registrations Here...
            // -----------------------------------------------------------------------
            // Core Utilities
            builder.RegisterType<NLogLogger>().As<ILogger>().InstancePerHttpRequest();
            builder.RegisterType<Settings>().As<ISettings>().InstancePerHttpRequest();

            // Localization
            builder.RegisterType<ResourceService>().As<IResourceService>().InstancePerHttpRequest();
            builder.RegisterType<ResourceRepository>().As<IResourceRepository>().InstancePerHttpRequest();
            builder.RegisterType<ResourceProviderFactory>().AsSelf();
            builder.RegisterType<DbResourceProvider>().As<IResourceProvider>().InstancePerHttpRequest();

            // Register Bootstrapper tasks based on the assemblies passed.
            if (bootTaskAssemblies != null)
            {
                foreach (string asm in bootTaskAssemblies)
                {
                    var assembly = Assembly.Load(asm);
                    RegisterFromAssembly(builder, assembly, typeof(IBootStrapperTask));
                }
            }

            // Register Plugins and override any previous registrations.
            var plugins = Assembly.Load("Sigma.Plugins");
            RegisterFromAssembly(builder, plugins, typeof(IPlugin));
        }
Beispiel #13
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 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;
        }
        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 #16
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();
        }
Beispiel #17
0
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);

            builder.RegisterType<Repositories>().InstancePerLifetimeScope();
            builder.RegisterFilterProvider();
        }
Beispiel #18
0
        private static void SetAutofacContainer()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<DefaultCommandBus>().As<ICommandBus>().InstancePerHttpRequest();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();

            builder.RegisterAssemblyTypes(typeof(IRepository<Expense>).Assembly).Where(t => t.Name.EndsWith("ExpenseRepository")).AsImplementedInterfaces().InstancePerLifetimeScope();
            builder.RegisterAssemblyTypes(typeof(IRepository<Category>).Assembly).Where(t => t.Name.EndsWith("CategoryRepository")).AsImplementedInterfaces().InstancePerLifetimeScope();
            builder.RegisterAssemblyTypes(typeof(IRepository<User>).Assembly).Where(t => t.Name.EndsWith("UserRepository")).AsImplementedInterfaces().InstancePerLifetimeScope();

            var services = Assembly.Load("FNHMVC.Domain");
            builder.RegisterAssemblyTypes(services).AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(services).AsClosedTypesOf(typeof(IValidationHandler<>)).InstancePerHttpRequest();
            builder.RegisterType<DefaultFormsAuthentication>().As<IFormsAuthentication>().InstancePerHttpRequest();
            builder.RegisterFilterProvider();

            builder.Register(c => FNHMVC.Data.Infrastructure.ConnectionHelper.BuildSessionFactory("FNHMVCContainer")).As<ISessionFactory>().SingleInstance();
            builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()).InstancePerLifetimeScope();

            IContainer container = builder.Build();

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

            // Register dependencies in controllers
            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            // Register dependencies in filter attributes
            builder.RegisterFilterProvider();

            // Register dependencies in custom views
            builder.RegisterSource(new ViewRegistrationSource());

            // Get your HttpConfiguration.
            var config = GlobalConfiguration.Configuration;

            // Register your Web API controllers.
            builder.RegisterApiControllers(typeof(ReservationController).Assembly);

            // OPTIONAL: Register the Autofac filter provider.
            builder.RegisterWebApiFilterProvider(config);

            builder.RegisterModule(new ManagerModule());

            var container = builder.Build();
            var webApiResolver = new AutofacWebApiDependencyResolver(container);
            // Mechanism for setting the dependency resolver for Web API and MVC is different.
            // Web API uses GlobalConfiguration.Configuration.DependencyResolver
            GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;

            var mvcResolver = new AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(mvcResolver);
        }
        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();
        }
        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();
        }
        public ConfigureContainer(Action<IContainer> registerResolver)
        {
            var builder = new ContainerBuilder();

            // load module in plugin folder (if have)
            foreach (var referencedPlugin in PreApplicationInit.ReferencedPlugins)
            {
                builder.RegisterAssemblyModules(referencedPlugin);
            }

            // load all modules in bin folder
            var assemblies = AssemblyHelper.GetDllsInPath(HostingEnvironment.MapPath("~/bin/"));
            foreach (var assembly in assemblies)
            {
                builder.RegisterAssemblyModules(assembly);
            }

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //builder.Register(c => new Logger()).As<ILogger>().InstancePerHttpRequest();
            builder.RegisterFilterProvider();

            _container = builder.Build();

            registerResolver(_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;
        }
Beispiel #24
0
        private void SetupDi()
        {
            var builder = new ContainerBuilder();

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

            builder.RegisterType<UserRepository>().As<IUserRepository>();
            builder.RegisterType<FoodRepository>().As<IFoodRepository>();
            builder.RegisterType<SmoothieRepository>().As<ISmoothieRepository>();
            builder.RegisterType<CategoryRepository>().As<ICategoryRepository>();

            builder.RegisterType<UserService>().As<IUserService>();
            builder.RegisterType<FoodService>().As<IFoodService>();
            builder.RegisterType<SmoothieService>().As<ISmoothieService>();
            builder.RegisterType<CategoryService>().As<ICategoryService>();

            builder.RegisterType<MappingService>().As<IMappingService>();
            builder.RegisterType<FormsAuthenticationService>().As<IAuthenticationService>();
            builder.RegisterType<FormsAuthorizationService>().As<IAuthorizationService>();

            builder.RegisterFilterProvider();

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

            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            builder.RegisterType<DefaultCommandBus>().As<ICommandBus>().InstancePerHttpRequest();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
            builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();

            builder.RegisterAssemblyTypes(typeof (UserRepository).Assembly)
                .Where(t => t.Name.EndsWith("Repository"))
                .AsImplementedInterfaces().InstancePerHttpRequest();

            //builder.RegisterAssemblyTypes(typeof (CategoryRepository).Assembly)
            //    .Where(t => t.Name.EndsWith("Repository"))
            //    .AsImplementedInterfaces().InstancePerHttpRequest();

            Assembly services = Assembly.Load("YiHe.Domain");

            builder.RegisterAssemblyTypes(services)
                .AsClosedTypesOf(typeof (ICommandHandler<>)).InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(services)
                .AsClosedTypesOf(typeof (IValidationHandler<>)).InstancePerHttpRequest();

            builder.RegisterType<DefaultFormsAuthentication>().As<IFormsAuthentication>().InstancePerHttpRequest();

            builder.RegisterFilterProvider();

            IContainer container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        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));
        }
Beispiel #27
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));
        }
        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 SystemRegister(ref ContainerBuilder builder)
 {
     //业务层注入
     builder.RegisterType<SysSampleBLL>().As<ISysSampleBLL>();
     builder.RegisterType<HomeBLL>().As<IHomeBLL>();
     builder.RegisterType<SysLogBLL>().As<ISysLogBLL>();
     builder.RegisterType<SysExceptionBLL>().As<ISysExceptionBLL>();
     builder.RegisterType<AccountBLL>().As<IAccountBLL>();
     builder.RegisterType<SysUserBLL>().As<ISysUserBLL>();
     builder.RegisterType<SysModuleBLL>().As<ISysModuleBLL>();
     builder.RegisterType<SysModuleOperateBLL>().As<ISysModuleOperateBLL>();
     builder.RegisterType<SysRoleBLL>().As<ISysRoleBLL>();
     builder.RegisterType<SysRightBLL>().As<ISysRightBLL>();
     //数据层注入
     builder.RegisterType<SysSampleRepository>().As<ISysSampleRepository>();
     builder.RegisterType<HomeRepository>().As<IHomeRepository>();
     builder.RegisterType<SysLogRepository>().As<ISysLogRepository>();
     builder.RegisterType<SysExceptionRepository>().As<ISysExceptionRepository>();
     builder.RegisterType<AccountRepository>().As<IAccountRepository>();
     builder.RegisterType<SysRightRepository>().As<ISysRightRepository>();
     builder.RegisterType<SysModuleRepository>().As<ISysModuleRepository>();
     builder.RegisterType<SysModuleOperateRepository>().As<ISysModuleOperateRepository>();
     builder.RegisterType<SysRoleRepository>().As<ISysRoleRepository>();
     builder.RegisterType<SysRightRepository>().As<ISysRightRepository>();
     //过滤器,属性注入
     builder.Register(c => new SysUserBLL(new SysUserRepository(),new SysRightRepository())).As<ISysUserBLL>().InstancePerHttpRequest();
     builder.RegisterFilterProvider();
 }
        private static void SetAutofacContainer(string dbConnectionString)
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<DefaultValidationBus>().As<IValidationBus>().InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(Assembly.Load("Utilities")).AsImplementedInterfaces().InstancePerHttpRequest();

            builder.RegisterType<ScrabbleClubContext>().WithParameter("connectionStringName", dbConnectionString).InstancePerHttpRequest();

            var infrastructureDataAccessLayerAssembly = Assembly.Load("DataAccess");
            builder.RegisterAssemblyTypes(infrastructureDataAccessLayerAssembly)
                   .Where(t => t.Name.EndsWith("Repository"))
                   .AsImplementedInterfaces().InstancePerHttpRequest();

            var domainLayerAssembly = Assembly.Load("Domain");
            builder.RegisterAssemblyTypes(domainLayerAssembly)
                   .Where(t => t.Name.EndsWith("Service"))
                   .AsImplementedInterfaces()
                   .InstancePerHttpRequest();

            var applicationLayerAssembly = Assembly.Load("Application");
            builder.RegisterAssemblyTypes(applicationLayerAssembly)
                   .AsClosedTypesOf(typeof (IValidationHandler<>)).InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(applicationLayerAssembly)
                   .Where(t => t.Name.EndsWith("Service"))
                   .AsImplementedInterfaces()
                   .InstancePerHttpRequest();

            builder.RegisterFilterProvider();
            IContainer container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        public static void Initialize(Assembly webAssembly, IEnumerable<Assembly> relatedAssemblies)
        {
            var builder = new ContainerBuilder();

            // register all of autofac modules
            builder.RegisterAssemblyModules(relatedAssemblies.ToArray());

            // register all controllers
            builder.RegisterControllers(webAssembly);

            // register all web api controllers
            builder.RegisterApiControllers(webAssembly);

            // register all filters
            builder.RegisterFilterProvider();

            // build up the container
            var container = builder.Build();

            // register it to ASP.NET MVC
            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;
        }
Beispiel #32
0
        protected override void Load(Autofac.ContainerBuilder builder)
        {
            // Register MVC controllers using assembly scanning.
            builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly());

            // Dependency injection module that registers abstractions for common web application properties.
            // Ref: http://autofac.org/apidoc/html/DA6737B.htm
            builder.RegisterModule(new AutofacWebTypesModule());
            builder.RegisterFilterProvider();

            //Injecting the Redis client service
            builder.RegisterType <NewtonsoftSerializer>().As <ISerializer>().SingleInstance();
            builder.RegisterType <StackExchangeRedisCacheClient>().As <ICacheClient>().SingleInstance();

            builder.Register(c => CacheProviderManager.Default).As <ICacheProvider>().SingleInstance();
        }
Beispiel #33
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            var builder = new Autofac.ContainerBuilder();

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

            builder.RegisterModule(new RepositoryModule());
            builder.RegisterModule(new EFModule());
            builder.RegisterFilterProvider();
            var container = builder.Build();

            //Start SqlDependency with application initialization

            AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
            {
                Debug.WriteLine(eventArgs.Exception.ToString());
            };
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);


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

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

            builder.RegisterModule(new RepositoryModule());
            builder.RegisterModule(new ServiceModule());
            builder.RegisterModule(new EFModule());

            builder.RegisterType <AuthorizeUserAttribute>().PropertiesAutowired();
            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }