本文整理汇总了C#中Autofac.ContainerBuilder.RegisterType方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.RegisterType方法的具体用法?C# ContainerBuilder.RegisterType怎么用?C# ContainerBuilder.RegisterType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Autofac.ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder.RegisterType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
public void Init() {
var builder = new ContainerBuilder();
builder.RegisterType<TokenManager>().As<ITokenManager>();
builder.RegisterType<TestTokenProvider>().As<ITokenProvider>();
_container = builder.Build();
_tokenManager = _container.Resolve<ITokenManager>();
}
示例2: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterModule(new RepositoryModule());
builder.RegisterType(typeof(SampleArchContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerRequest();
}
示例3: Main
static void Main(string[] args)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<Cpu>().As<ICpu>().SingleInstance();
containerBuilder.RegisterType<Emulator>().As<IEmulator>();
containerBuilder.RegisterType<Memory>().As<IMemory>().SingleInstance();
containerBuilder.RegisterType<Ppu>().As<IPpu>();
var instructionTypes = Assembly.GetExecutingAssembly().GetTypes().Where(f => typeof(IInstruction).IsAssignableFrom(f) && !f.IsAbstract).ToArray();
containerBuilder.RegisterTypes(instructionTypes)
.As<IInstruction>();
var container = containerBuilder.Build();
var emulator = container.Resolve<IEmulator>();
var memory = container.Resolve<IMemory>();
memory.SetCartridge(new Cartridge("../../roms/nestest.nes"));
emulator.Reset();
for (;;)
{
emulator.Step();
}
}
示例4: configureIoC
private void configureIoC()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.Register(ctx => LogManager.GetLogger("trace"))
.As<Logger>()
.InstancePerHttpRequest();
builder.RegisterType<ProcessRunner>()
.AsImplementedInterfaces()
.InstancePerDependency()
;
builder.RegisterType<DotProcessor>()
.AsImplementedInterfaces()
.SingleInstance()
;
builder.RegisterType<Ansi2HtmlConverter>()
.AsImplementedInterfaces()
.InstancePerDependency()
;
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
示例5: Application_Start
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// Register: create and configure the container
_container = BootstrapContainer();
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
var builder = new ContainerBuilder();
//var configurationRepository =
// new ConfigurationRepository.ConfigurationRepository() as IConfigurationRepository;
//builder.Register(c => configurationRepository).As<IConfigurationRepository>().SingleInstance();
builder.RegisterType<StashCoreApiConsumer>().As<IStashCoreApiConsumer>();
builder.RegisterType<PullTrackerRepository>().As<IPullTrackerRepository>();
builder.RegisterType<RequestProcessFacade>().As<IRequestProcessFacade>();
IContainer localContainer = builder.Build();
AutofacHelper.Scope = localContainer;
_containerProvider = new ContainerProvider(localContainer);
}
示例6: SetIocContainer
private static void SetIocContainer()
{
//Implement Autofac
var configuration = GlobalConfiguration.Configuration;
var builder = new ContainerBuilder();
// Register MVC controllers using assembly scanning.
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// Register MVC controller and API controller dependencies per request.
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerDependency();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerLifetimeScope();
// Register service
builder.RegisterAssemblyTypes(typeof(ProfileService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerDependency();
// Register repository
builder.RegisterAssemblyTypes(typeof(ProfileRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces().InstancePerDependency();
var container = builder.Build();
//for MVC Controller Set the dependency resolver implementation.
var resolverMvc = new AutofacDependencyResolver(container);
System.Web.Mvc.DependencyResolver.SetResolver(resolverMvc);
}
示例7: RegisterHandlersWithAutofac
public static void RegisterHandlersWithAutofac(ContainerBuilder containerBuilder, IReadOnlyCollection<MessageRegistration> registrations)
{
var handlers = registrations.Select(x => x.Handler).Distinct();
var piplelineHandlers = registrations.Select(x => x.Pipeline).Distinct().SelectMany(x => x).Distinct();
RegisterLeafHandlers(containerBuilder, handlers);
RegisterPipelineHandlers(containerBuilder, piplelineHandlers);
foreach (var registration in registrations)
{
if (registration.Dependancies.Any())
{
foreach (var dependancy in registration.Dependancies)
{
containerBuilder.RegisterType(dependancy).AsSelf();
}
}
if (registration.ScopedDependancies.Any())
{
foreach (var dependancy in registration.ScopedDependancies)
{
containerBuilder.RegisterType(dependancy).AsSelf().AsImplementedInterfaces().InstancePerLifetimeScope();
}
}
}
}
示例8: RegisterDefaultServices
public void RegisterDefaultServices()
{
var builder = new ContainerBuilder();
builder.RegisterInstance(container);
builder.RegisterType<AutofacServiceLocator>()
.As<IServiceLocator>()
.SingleInstance();
builder.RegisterAssemblyTypes(typeof(IServiceBus).Assembly)
.AssignableTo<IBusConfigurationAware>()
.As<IBusConfigurationAware>()
.SingleInstance();
builder.RegisterType<DefaultReflection>()
.As<IReflection>()
.SingleInstance();
builder.RegisterType(config.SerializerType)
.As<IMessageSerializer>()
.SingleInstance();
builder.RegisterType<EndpointRouter>()
.As<IEndpointRouter>()
.SingleInstance();
foreach(var module in config.MessageModules)
{
builder.RegisterType(module)
.Named<string>(module.FullName)
.As(typeof(IMessageModule))
.SingleInstance();
}
builder.Update(container);
foreach(var busConfigurationAware in container.Resolve<IEnumerable<IBusConfigurationAware>>())
busConfigurationAware.Configure(config, this);
}
示例9: RegisterServices
private void RegisterServices(ContainerBuilder builder)
{
// repositories
//override required repository with our custom context
builder.RegisterType<EfRepository<GroupRule>>()
.As<IRepository<GroupRule>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(TAX_GROUPS_CONTEXT_NAME))
.InstancePerLifetimeScope();
// repositories
//override required repository with our custom context
builder.RegisterType<EfRepository<TaxClass>>()
.As<IRepository<TaxClass>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(TAX_GROUPS_CONTEXT_NAME))
.InstancePerLifetimeScope();
// repositories
//override required repository with our custom context
builder.RegisterType<EfRepository<MemberGroup>>()
.As<IRepository<MemberGroup>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(TAX_GROUPS_CONTEXT_NAME))
.InstancePerLifetimeScope();
// repositories
//override required repository with our custom context
builder.RegisterType<EfRepository<TaxRule>>()
.As<IRepository<TaxRule>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(TAX_GROUPS_CONTEXT_NAME))
.InstancePerLifetimeScope();
///////////////////////////////////////////////////////////////////////////////////////////////
// services
//builder.RegisterType<GroupDealService>().As<IGroupDealService>().InstancePerLifetimeScope();
///////////////////////////////////////////////////////////////////////////////////////////////
}
示例10: Load
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder.RegisterType<ApplicationDatabase>()
.As<IApplicationDatabase>()
.SingleInstance();
builder
.RegisterType<DatabaseUpgradeDetector>()
.As<IDatabaseUpgradeDetector>()
.SingleInstance();
builder
.RegisterType<SqlDatabaseProvider>()
.As<IDatabaseProvider>()
.Named<IDatabaseProvider>("sql")
.WithMetadata<IDatabaseProviderMetadata>(c => c.For(m => m.Name, "Sql"));
builder
.RegisterType<SqlCeDatabaseProvider>()
.As<IDatabaseProvider>()
.Named<IDatabaseProvider>("sqlce")
.WithMetadata<IDatabaseProviderMetadata>(c => c.For(m => m.Name, "SqlCe"));
}
示例11: TripleTexFacade
public TripleTexFacade(int syncSystem, string syncPassword, string username, string password)
{
_syncSystem = syncSystem;
_syncPassword = syncPassword;
_username = username;
_password = password;
// Setup IoC and DI
var builder = new ContainerBuilder();
// The JsonService keeps the session cookie and has to have one instance per lifetime
builder.RegisterType<JsonService>().As<IJsonService>().InstancePerLifetimeScope();
//builder.RegisterAssemblyTypes()
// .Where(r => r.Name.EndsWith("Service"))
// .AsImplementedInterfaces();
builder.RegisterType<SyncService>().As<ISyncService>();
builder.RegisterType<CompanyService>().As<ICompanyService>();
builder.RegisterType<OrderService>().As<IOrderService>();
builder.RegisterType<ProductService>().As<IProductService>();
_container = builder.Build();
// Login
_container.Resolve<ISyncService>().Login(_syncSystem, _syncPassword, _username, _password);
}
示例12: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<VendingMachineDbContext>()
.As<DbContext>()
.AsSelf()
.InstancePerLifetimeScope();
builder.RegisterGeneric(typeof (EntityFrameworkUnitOfWork<>))
.As(typeof (UnitOfWork<>))
.InstancePerDependency();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(x => x.IsClosedTypeOf(typeof (IEntityFactory<,>)))
.AsImplementedInterfaces()
.InstancePerDependency();
builder.RegisterType<CoinsRefundedEventCommitter>()
.As(typeof (EntityFrameworkTrackedEventCommitter<UserVendingMachineAggregationEntity, CoinsRefundedEvent>))
.InstancePerDependency();
builder.RegisterType<GoodsBuyedEventCommitter>()
.As(typeof (EntityFrameworkTrackedEventCommitter<UserVendingMachineAggregationEntity, GoodsBuyedEvent>))
.InstancePerDependency();
//builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
// .Where(x => x.IsClosedTypeOf(typeof (ITrackedEventCommitter<,>)))
// .As(typeof(EntityFrameworkTrackedEventCommitter<,>))
// .AsImplementedInterfaces()
// .InstancePerDependency();
}
示例13: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<ConfigManager>().As<IConfigManager>()
.WithParameter(new NamedParameter("navigation", this.Navigation));
builder.RegisterType<SecurityManager>().As<ISecurityManager>();
}
示例14: SetAutofacContainer
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));
}
示例15: RegisterServices
private static IContainer RegisterServices(ContainerBuilder builder)
{
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// EF DbContext
builder.RegisterType<EntitiesContext>()
.As<DbContext>()
.InstancePerApiRequest();
// Register repositories by using Autofac's OpenGenerics feature
// More info: http://code.google.com/p/autofac/wiki/OpenGenerics
builder.RegisterGeneric(typeof(EntityRepository<>))
.As(typeof(IEntityRepository<>))
.InstancePerApiRequest();
// Services
builder.RegisterType<CryptoService>()
.As<ICryptoService>()
.InstancePerApiRequest();
builder.RegisterType<MembershipService>()
.As<IMembershipService>()
.InstancePerApiRequest();
builder.RegisterType<ShipmentService>()
.As<IShipmentService>()
.InstancePerApiRequest();
return builder.Build();
}