protected override void Load(ContainerBuilder builder) { // assemblies var assemblies = AutofacAssemblyStore.GetAssemblies(); // validators: types derived from AbstractValidator<> builder.RegisterAssemblyTypes(assemblies) .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(AbstractValidator <>)) .Keyed(type => type.BaseType?.GenericTypeArguments[0], typeof(IValidator)) .SingleInstance(); }
protected override void Load(ContainerBuilder builder) { // warming up the mapper to trigger PlatformAdapter loads extra things Mapper.Initialize(configuration => { }); // assemblies var assemblies = AutofacAssemblyStore.GetAssemblies(); // depend by ConfigurationStore builder.RegisterAssemblyTypes(assemblies) .Where(type => type.IsSubclassOf(typeof(Profile))) .As <Profile>() .SingleInstance(); // depend by ConfigurationStore builder.RegisterInstance(new TypeMapFactory()) .As <ITypeMapFactory>() .SingleInstance(); // depend by ConfigurationStore MapperRegistry.Mappers.Add(new PagedListMapper()); builder.RegisterInstance(MapperRegistry.Mappers) .As <IEnumerable <IObjectMapper> >(); // depend by MappingEngine builder.RegisterType <ConfigurationStore>() .AsImplementedInterfaces() .SingleInstance() .OnActivating(args => { // add profiles foreach (var profile in args.Context.Resolve <IEnumerable <Profile> >()) { args.Instance.AddProfile(profile); } }); // depdend by MappingEngineWrapper // Note: there is constructor specified for MappingEngine // Because it uses the most parameters constructor in unit test project by supplying mocking parameter objects which is unwanted builder.RegisterType <MappingEngine>() .UsingConstructor(typeof(IConfigurationProvider)) .AsSelf() .SingleInstance(); // MappingEngine wrapper builder.RegisterType <MappingEngineWrapper>() .As <IMappingEngine>() .InstancePerLifetimeScope(); }
public static IContainer CreateHostContainer() { var builder = new ContainerBuilder(); // assemblies var assemblies = AutofacAssemblyStore.GetAssemblies(); // startable builder.RegisterAssemblyTypes(assemblies) .Where(t => typeof(IStartable).IsAssignableFrom(t)) .As <IStartable>() .SingleInstance(); // modules builder.RegisterModule(new LoggingFactoryModule()); builder.RegisterModule(new LoggingModule()); builder.RegisterModule(new EventsModule()); builder.RegisterModule(new CacheModule()); builder.RegisterModule(new SettingModule()); // event bus builder.RegisterType <DefaultEventBus>().As <IEventBus>().SingleInstance(); builder.RegisterType <DefaultExceptionPolicy>().As <IExceptionPolicy>().SingleInstance(); // cache builder.RegisterType <DefaultCacheHolder>().As <ICacheHolder>().SingleInstance(); builder.RegisterType <DefaultCacheContextAccessor>().As <ICacheContextAccessor>().SingleInstance(); builder.RegisterType <DefaultParallelCacheContext>().As <IParallelCacheContext>().SingleInstance(); builder.RegisterType <DefaultAsyncTokenProvider>().As <IAsyncTokenProvider>().SingleInstance(); // volatile providers RegisterVolatileProvider <Clock, IClock>(builder); // assembly resolvers builder.RegisterType <DefaultAssemblyLoader>().As <IAssemblyLoader>().SingleInstance(); builder.RegisterType <AppDomainAssemblyNameResolver>().As <IAssemblyNameResolver>().SingleInstance(); builder.RegisterType <GacAssemblyNameResolver>().As <IAssemblyNameResolver>().SingleInstance(); builder.RegisterType <FrameworkAssemblyNameResolver>().As <IAssemblyNameResolver>().SingleInstance(); // builder.RegisterType <DefaultHost>().As <IHost>().SingleInstance().ExternallyOwned(); // manually dispose var container = builder.Build(); return(container); }
protected override void Load(ContainerBuilder builder) { // assemblies var assemblies = AutofacAssemblyStore.GetAssemblies(); builder.RegisterType <ServiceBus>() .As <IServiceBus>() .SingleInstance() .OnActivated(args => { var siteSettings = args.Context.Resolve <ISiteSettings>(); var messageTypes = assemblies.SelectMany(x => x.ExportedTypes).Where(x => typeof(IMessage).IsAssignableFrom(x) && !x.IsInterface); foreach (var messageType in messageTypes) { args.Instance.RegisterMessageEndpoint(messageType, siteSettings.QueueEndPoint); } }); }
protected override void Load(ContainerBuilder builder) { // assemblies var assemblies = AutofacAssemblyStore.GetAssemblies(); // ------------------WebApi------------------ // REF: http://docs.autofac.org/en/latest/integration/webapi.html // Get your HttpConfiguration. var config = Startup.HttpConfiguration; builder.RegisterInstance(config); // Register your Web API controllers. builder.RegisterApiControllers(assemblies).EnableClassInterceptors(); // OPTIONAL: Register the Autofac filter provider. builder.RegisterWebApiFilterProvider(config); // ------------------MVC------------------ // REF: http://docs.autofac.org/en/latest/integration/mvc.html if (HttpContext.Current == null) { return; // Note: only enable mvc when hosting in IIS } // Register your MVC controllers. builder.RegisterControllers(assemblies).EnableClassInterceptors(); // OPTIONAL: Register model binders that require DI. builder.RegisterModelBinders(assemblies); 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(); }
public DefaultHost(ILifetimeScope rootLifetimeScope) { _rootLifetimeScope = rootLifetimeScope; var intermediateScope = _rootLifetimeScope.BeginLifetimeScope(builder => { // assemblies var assemblies = AutofacAssemblyStore.GetAssemblies(); // all modules: internal class module will NOT be picked up foreach (var item in assemblies.SelectMany(a => a.ExportedTypes).Where(t => typeof(IModule).IsAssignableFrom(t))) { builder.RegisterType(item) .As <IModule>() .InstancePerDependency(); } }); _siteLifetimeScope = intermediateScope.BeginLifetimeScope(AutofacScope.Site, builder => { // assemblies var assemblies = AutofacAssemblyStore.GetAssemblies(); // modules foreach (var item in intermediateScope.Resolve <IEnumerable <IModule> >()) { builder.RegisterModule(item); } // dependencies builder.RegisterAssemblyTypes(assemblies) .Where(t => typeof(IDependency).IsAssignableFrom(t) && !typeof(ISingletonDependency).IsAssignableFrom(t) && !typeof(IUnitOfWorkDependency).IsAssignableFrom(t) && !typeof(ITransientDependency).IsAssignableFrom(t) && !typeof(IEventHandler).IsAssignableFrom(t)) .AsImplementedInterfaces() .InstancePerLifetimeScope() .EnableInterfaceInterceptors(); builder.RegisterAssemblyTypes(assemblies) .Where(t => typeof(ISingletonDependency).IsAssignableFrom(t) && !typeof(IEventHandler).IsAssignableFrom(t)) .AsImplementedInterfaces() .InstancePerMatchingLifetimeScope(AutofacScope.Site) .EnableInterfaceInterceptors(); builder.RegisterAssemblyTypes(assemblies) .Where(t => typeof(IUnitOfWorkDependency).IsAssignableFrom(t) && !typeof(IEventHandler).IsAssignableFrom(t)) .AsImplementedInterfaces() .InstancePerMatchingLifetimeScope(AutofacScope.Work) .EnableInterfaceInterceptors(); builder.RegisterAssemblyTypes(assemblies) .Where(t => typeof(ITransientDependency).IsAssignableFrom(t) && !typeof(IEventHandler).IsAssignableFrom(t)) .AsImplementedInterfaces() .InstancePerDependency() .EnableInterfaceInterceptors(); // event handlers builder.RegisterAssemblyTypes(assemblies) .Where(t => typeof(IEventHandler).IsAssignableFrom(t)) .ActivatorData.ConfigurationActions.Add((type, registrationBuilder) => { var interfaceTypes = type.GetInterfaces(); foreach (var interfaceType in interfaceTypes) { if (interfaceType.GetInterface(typeof(IEventHandler).Name) != null) { // register named instance for each interface, for efficient filtering inside event bus // IEventHandler derived classes only registrationBuilder.Named <IEventHandler>(interfaceType.Name); } } registrationBuilder.InstancePerLifetimeScope(); }); }); _hostEvents = _siteLifetimeScope.Resolve <Owned <IHostEvents> >(); Logger = NullLogger.Instance; }