Example #1
0
        public static void Run()
        {
            var builder = new Autofac.ContainerBuilder();
            var config = GlobalConfiguration.Configuration;
            var service = builder.RegisterType<FormularioService>()
                .As<IFormularioService>();

            var factory = builder.RegisterType<DatabaseFactoryFormulario>()
                .As<IDatabaseFactory>();

            var unit = builder.RegisterType<UnitOfWorkGeneric>()
                .As<IUnitOfWOrkGeneric<IDatabaseFactory>>();

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            builder.RegisterWebApiFilterProvider(config);

            builder.RegisterWebApiModelBinderProvider();

            service.InstancePerRequest();
            factory.InstancePerRequest();
            unit.InstancePerRequest();


            var container = builder.Build();

            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            config.Formatters.Remove(config.Formatters.XmlFormatter);

            var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
            json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        }
        public static IContainer Initialize(ContainerBuilder builder, HttpConfiguration config)
        {
            // Register all controllers
            builder.RegisterApiControllers(typeof(Startup).Assembly);

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

            // Register model binders
            builder.RegisterWebApiModelBinderProvider();

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

            builder.RegisterModule(new RequestHandlerModule());
            builder.RegisterModule(new EntityFrameworkModule());
            builder.RegisterModule(new DocumentGeneratorModule());

            // http://www.talksharp.com/configuring-autofac-to-work-with-the-aspnet-identity-framework-in-mvc-5
            builder.RegisterType<IwsIdentityContext>().AsSelf().InstancePerRequest();
            builder.RegisterType<ApplicationUserStore>().As<IUserStore<ApplicationUser>>().InstancePerRequest();
            builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
            builder.RegisterType<ApplicationUserManager>().As<UserManager<ApplicationUser>>().InstancePerRequest();

            return builder.Build();
        }
        internal static IContainer BuildRegistrations()
        {
            var builder = new ContainerBuilder();

            // Register our custom dependencies
            builder.RegisterModule(new ServicesModule());

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

            // Register dependencies in filter attributes
            builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

            // Register autofac's provider for webapi's modelbinders.
            builder.RegisterWebApiModelBinderProvider();

            //Obsolete
            //builder.RegisterWebApiModelBinders(typeof(MvcApplication).Assembly);

            builder.RegisterType<WebApiRequestContextModelBinder>().AsModelBinderForTypes(typeof(RequestContext));

            var container = builder.Build();

            // Set WebApi DI resolver to use our Autofac container
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            return container;
        }
        public override void RegisterComponents(ContainerBuilder builder)
        {
            base.RegisterComponents(builder);
            
            builder.RegisterInstance(this).As<NuGetApiService>();

            var config = ConfigureWebApi();
            builder.RegisterInstance(config).AsSelf();

            builder
                .RegisterApiControllers(GetControllerAssemblies().ToArray())
                .OnActivated(e =>
                {
                    var nugetController = e.Instance as NuGetApiController;
                    if (nugetController != null)
                    {
                        nugetController.Host = Host;
                        nugetController.Service = this;
                        nugetController.Container = Container;
                    }
                })
                .InstancePerApiRequest();

            builder.RegisterWebApiFilterProvider(config);
            builder.RegisterWebApiModelBinderProvider();
        }
Example #5
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);
        }
Example #6
0
 /// <summary>
 /// 构建服务并设置WebApi平台的Resolver
 /// </summary>
 /// <param name="services">服务映射信息集合</param>
 /// <param name="assemblies">要检索的程序集集合</param>
 /// <returns>服务提供者</returns>
 protected override IServiceProvider BuildAndSetResolver(IServiceCollection services, Assembly[] assemblies)
 {
     ContainerBuilder builder = new ContainerBuilder();
     builder.RegisterApiControllers(assemblies).AsSelf().PropertiesAutowired();
     builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
     builder.RegisterWebApiModelBinderProvider();
     builder.Populate(services);
     IContainer container = builder.Build();
     IDependencyResolver resolver = new AutofacWebApiDependencyResolver(container);
     GlobalConfiguration.Configuration.DependencyResolver = resolver;
     return (IServiceProvider)resolver.GetService(typeof(IServiceProvider));
 }
Example #7
0
        protected void Application_Start()
        {
            var config = GlobalConfiguration.Configuration;


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

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());


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

            // Register your Web API controllers.

            builder.RegisterApiControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
            //OPTIONAL: Register the Autofac filter provider.
            builder.RegisterWebApiFilterProvider(config);

            //OPTIONAL: Register the Autofac model binder provider.
            builder.RegisterWebApiModelBinderProvider();

            //Set the dependency resolver to be Autofac.


            var container = builder.Build();

            //  DependencyResolver.SetResolver(new AutofacWebApiDependencyResolver(container));

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            config.DependencyResolver =
                new AutofacWebApiDependencyResolver(container);



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

            GlobalConfiguration.Configuration.EnsureInitialized();
        }
Example #8
0
        private static IContainer BuildContainer(Options options)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var builder = new ContainerBuilder();

            var settings = new SettingsRepository(options.MplayerExecutablePath, options.LibraryPath);

            builder.RegisterInstance<ISettingsRepository>(settings);
            builder.RegisterType<SongRepository>().As<ISongRepository>().SingleInstance();

            builder.RegisterType<SongRunner>().SingleInstance();

            builder.RegisterWebApiModelBinderProvider();

            builder.RegisterApiControllers(assembly);

            return builder.Build();
        }
        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();
        }
        public static IContainer Initialize(ContainerBuilder builder, HttpConfiguration config)
        {
            // Register all controllers
            builder.RegisterApiControllers(typeof(Startup).Assembly);

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

            // Register model binders
            builder.RegisterWebApiModelBinderProvider();

            // Register all Autofac specific IModule implementations
            builder.RegisterAssemblyModules(typeof(Startup).Assembly);
            builder.RegisterAssemblyModules(typeof(AutofacMediator).Assembly);
            builder.RegisterModule(new RequestHandlerModule());
            builder.RegisterModule(new CoreModule());
            builder.RegisterModule(new EntityFrameworkModule());
            builder.RegisterModule(new EmailModule());
            builder.RegisterModule(new XmlValidationModule());
            builder.RegisterModule(new EventDispatcherModule());
            builder.RegisterModule(new XmlModule());
            builder.RegisterModule(new SecurityModule());

            // http://www.talksharp.com/configuring-autofac-to-work-with-the-aspnet-identity-framework-in-mvc-5
            builder.RegisterType<WeeeIdentityContext>().AsSelf().InstancePerRequest();
            builder.RegisterType<ApplicationUserStore>().As<IUserStore<ApplicationUser>>().InstancePerRequest();
            builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
            builder.RegisterType<ApplicationUserManager>().As<UserManager<ApplicationUser>>().InstancePerRequest();

            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Weee.DefaultConnection"].ConnectionString;

            builder.RegisterType<SecurityEventDatabaseAuditor>()
                .WithParameter(new NamedParameter("connectionString", connectionString))
                .As<ISecurityEventAuditor>()
                .InstancePerRequest();

            return builder.Build();
        }
Example #11
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);
        }
Example #12
0
        public static void Register()
        {
            var builder = new Autofac.ContainerBuilder();

            var config = GlobalConfiguration.Configuration;

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

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

            // OPTIONAL: Register the Autofac model binder provider.
            builder.RegisterWebApiModelBinderProvider();


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

            var container = builder.Build();

            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
Example #13
0
 private static void RegisterModelBinders(ContainerBuilder builder)
 {
     builder.RegisterWebApiModelBinderProvider();
     builder.RegisterType<ObjectIdModelBinder>().AsModelBinderForTypes(typeof(ObjectId));
 }