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;
        }
Esempio n. 2
0
        public void Configuration(IAppBuilder app)
        {
            var autoFacBuilder = new ContainerBuilder();
            HttpConfiguration config = new HttpConfiguration();

            ConfigureAutoFac(autoFacBuilder);
            autoFacBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            autoFacBuilder.RegisterWebApiFilterProvider(config);
            autoFacBuilder.RegisterWebApiModelBinders(Assembly.GetExecutingAssembly());
            var container = autoFacBuilder.Build();

            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            app.UseAutofacMiddleware(container);
            app.UseAutofacWebApi(config);
            ConfigureAuth(app);
            WebApiConfig.Register(config);


            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            app.UseWebApi(config);

            config.EnsureInitialized();

        }
Esempio n. 3
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register(x => new HttpContextWrapper(HttpContext.Current))
                   .InstancePerApiRequest()
                   .As<HttpContextBase>();

            builder.RegisterApiControllers(ThisAssembly);
            builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
            builder.RegisterWebApiModelBinderProvider();
            builder.RegisterWebApiModelBinders(ThisAssembly);
        }
Esempio n. 4
0
        private void SetDependencyResolver()
        {
            var builder = new ContainerBuilder();

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

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

            builder
                .RegisterAssemblyTypes(
                    Assembly.GetExecutingAssembly(),
                    Assembly.GetAssembly(typeof(DataService)),
                    Assembly.GetAssembly(typeof(WebApiDataService)),
                    Assembly.GetAssembly(typeof(OpdsDataService)),
                    Assembly.GetAssembly(typeof(ConvertService)),
                    Assembly.GetAssembly(typeof(SocialService)),
                    Assembly.GetAssembly(typeof(MailService)),
                    Assembly.GetAssembly(typeof(OAuth2Client)),
                    Assembly.GetAssembly(typeof(RestClient)),
                    Assembly.GetAssembly(typeof(SocialIdentityInjector))
                ).AsImplementedInterfaces().AsSelf();

            builder.RegisterType<SearchService>()
                    .AsImplementedInterfaces()
                    .OnActivating(e => e.Instance.Initialize(new SearchStorageModel
                    {
                        DirectoryName = HttpRuntime.AppDomainAppPath,
                        StorageType = SearchStorageTypeEnum.FileSystem
                    }));

            builder.RegisterType<AuthorizationRoot>()
                   .WithParameter(new NamedParameter("sectionName", "oauth2"));

            builder.RegisterType<SocialIdentityInjector>().PropertiesAutowired();

            builder.RegisterFilterProvider();

            IContainer container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
        public void RegisterDependencies(ContainerBuilder containerBuilder, Assembly[] assemblies)
        {
            containerBuilder.RegisterAutofacModule(new AutofacWebTypesModule());

            // Mvc:
            containerBuilder.RegisterControllers(assemblies);
            containerBuilder.RegisterFilterProvider();
            containerBuilder.RegisterModelBinderProvider();
            containerBuilder.RegisterModelBinders(assemblies);

            // Web Api:
            containerBuilder.RegisterApiControllers(assemblies);
            containerBuilder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
            containerBuilder.RegisterWebApiModelBinderProvider();
            containerBuilder.RegisterWebApiModelBinders(assemblies);

            // Default auto registrations
            containerBuilder.RegisterAssemblyTypes(assemblies).Where(t => t.IsAssignableTo<IRouteRegistrar>()).As<IRouteRegistrar>().SingleInstance();
            containerBuilder.RegisterAssemblyTypes(assemblies).Where(t => t.IsAssignableTo<IBootTask>()).As<IBootTask>().SingleInstance();
            containerBuilder.RegisterAssemblyTypes(assemblies).Where(t => t.IsAssignableTo<AreaRegistration>()).As<AreaRegistration>().SingleInstance();
        }
Esempio n. 6
0
        public IContainer ConfigureDI(IAppBuilder app, HttpConfiguration config)
        {
            var builder = new ContainerBuilder();

            //Repositories
            builder.RegisterType<ApplicationDbContext>().As<ApplicationDbContext>().InstancePerRequest();
            builder.RegisterType<ApplicationUserManager>().As<UserManager<ApplicationUser>>().InstancePerRequest();
            builder.RegisterType<ApplicationRepository<UploadingProgress>>().As<IRepository<UploadingProgress>>().InstancePerRequest();
            builder.RegisterType<ApplicationRepository<SearchQuery>>().As<IRepository<SearchQuery>>().InstancePerRequest();
            builder.RegisterType<CSVImport>().As<IFileImport>().InstancePerRequest();

            builder.RegisterType<ApplicationRepository<Person>>().As<IRepository<Person>>().InstancePerRequest();
            builder.RegisterType<ApplicationRepository<PersonFact>>().As<IRepository<PersonFact>>().InstancePerRequest();
            builder.RegisterType<ApplicationRepository<PersonParameter>>().As<IRepository<PersonParameter>>().InstancePerRequest();

            builder.RegisterType<ApplicationRepository<Document>>().As<IRepository<Document>>().InstancePerRequest();
            builder.RegisterType<ApplicationRepository<DocumentParameter>>().As<IRepository<DocumentParameter>>().InstancePerRequest();
            builder.RegisterType<ApplicationRepository<DocumentFact>>().As<IRepository<DocumentFact>>().InstancePerRequest();

            builder.RegisterType<ApplicationRepository<ParameterName>>().As<IRepository<ParameterName>>().InstancePerRequest();

            builder.RegisterType<ApplicationRepository<Misc>>().As<IRepository<Misc>>().InstancePerRequest();
            builder.RegisterType<ApplicationRepository<MiscName>>().As<IRepository<MiscName>>().InstancePerRequest();

            builder.RegisterType<ReportRepository<DocumentOnControl>>().As<IReportRepository<DocumentOnControl>>().InstancePerRequest();

            ////Authorization
            builder.RegisterType<ApplicationOAuthProvider>().As<IOAuthAuthorizationServerProvider>();
            builder.RegisterType<QueryStringOAuthBearerProvider>().As<IOAuthBearerAuthenticationProvider>();

            // Register your Web API controllers.
            builder.RegisterWebApiFilterProvider(config);
            builder.RegisterWebApiModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

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

            return container;
        }
Esempio n. 7
0
        internal static void Configure(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });

            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterInstance(HeroRepository.Current).ExternallyOwned();
            builder.RegisterType<CryptographyService>().As<ICryptographyService>();
            
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterWebApiModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterWebApiModelBinderProvider();

            builder.RegisterType<HeroPropertiesModelBinder>().AsModelBinderForTypes(typeof(HeroPropertiesModel));
            builder.RegisterType<FreeToPlayPeriodPropertiesModelBinder>().AsModelBinderForTypes(typeof(FreeToPlayPeriodPropertiesModel));

            config.ParameterBindingRules.Add(typeof(HeroPropertiesModel), p => p.BindWithModelBinding());
            config.ParameterBindingRules.Add(typeof(FreeToPlayPeriodPropertiesModel), p => p.BindWithModelBinding());

            IContainer container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
Esempio n. 8
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);
        }