本文整理汇总了C#中ContainerBuilder.RegisterGeneric方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.RegisterGeneric方法的具体用法?C# ContainerBuilder.RegisterGeneric怎么用?C# ContainerBuilder.RegisterGeneric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder.RegisterGeneric方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<InMemoryDatabase>().As<IDatabase>().InstancePerHttpRequest();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
base.Load(builder);
}
示例2: AsImplementedInterfaces_CanBeAppliedToOpenGenericRegistrations
public void AsImplementedInterfaces_CanBeAppliedToOpenGenericRegistrations()
{
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(SelfComponent<>)).AsImplementedInterfaces();
var context = builder.Build();
context.Resolve<IImplementedInterface<object>>();
}
示例3: RegisterServices
private static void RegisterServices(ContainerBuilder builder)
{
builder.Register(x => new AppDbContext())
.As<DbContext>()
.InstancePerRequest();
<<<<<<< HEAD
builder.Register(x => new HttpCacheService())
.As<ICacheService>()
.InstancePerRequest();
builder.Register(x => new IdentifierProvider())
.As<IIdentifierProvider>()
.InstancePerRequest();
var servicesAssembly = Assembly.GetAssembly(typeof(IJokesService));
builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();
builder.RegisterGeneric(typeof(DbRepository<>))
.As(typeof(IDbRepository<>))
.InstancePerRequest();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AssignableTo<BaseController>()
.PropertiesAutowired();
=======
builder.RegisterGeneric(typeof(DbRepository<>))
.As(typeof(IDbRepository<>))
.InstancePerRequest();
// builder.Register(x => new HttpCacheService())
// .As<ICacheService>()
// .InstancePerRequest();
// builder.Register(x => new IdentifierProvider())
// .As<IIdentifierProvider>()
// .InstancePerRequest();
// var servicesAssembly = Assembly.GetAssembly(typeof(IJokesService));
// builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();
// builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
// .AssignableTo<BaseController>().PropertiesAutowired();
>>>>>>> 6ca5c2744ff07e9ad93c0f5627d37f5deea149bf
}
示例4: RegisterGenericType
private static void RegisterGenericType(ContainerBuilder builder, Type type, IEnumerable<IAutofacRegistrationAttribute> attributes)
{
var registrationBuilder = builder.RegisterGeneric(type);
foreach (IAutofacRegistrationAttribute attribute in attributes)
{
registrationBuilder = attribute.Register(registrationBuilder);
}
}
开发者ID:dvabuzyarov,项目名称:Autofac.Extras.RegistrationAttributes,代码行数:9,代码来源:ContainerBuilderExtensions.cs
示例5: AggregateServiceGenericsFixture
public AggregateServiceGenericsFixture()
{
var builder = new ContainerBuilder();
builder.RegisterAggregateService<IOpenGenericAggregate>();
builder.RegisterGeneric(typeof(OpenGenericImpl<>))
.As(typeof(IOpenGeneric<>));
this._container = builder.Build();
}
示例6: BuildMediator
private static IMediator BuildMediator()
{
var builder = new ContainerBuilder();
builder.RegisterSource(new ContravariantRegistrationSource());
builder.RegisterAssemblyTypes(typeof (IMediator).Assembly).AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof (Ping).Assembly).AsImplementedInterfaces();
builder.RegisterGeneric(typeof (GenericPostRequestHandler<,>)).As(typeof (IPostRequestHandler<,>));
builder.RegisterInstance(Console.Out).As<TextWriter>();
var lazy = new Lazy<IServiceLocator>(() => new AutofacServiceLocator(builder.Build()));
var serviceLocatorProvider = new ServiceLocatorProvider(() => lazy.Value);
builder.RegisterInstance(serviceLocatorProvider);
var mediator = serviceLocatorProvider().GetInstance<IMediator>();
return mediator;
}
示例7: GenericsAreTaggable
public void GenericsAreTaggable()
{
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(List<>))
.InstancePerMatchingLifetimeScope("tag")
.As(typeof(IList<>));
var outer = builder.Build();
var inner = outer.BeginLifetimeScope("tag");
var coll = inner.Resolve<IList<object>>();
Assert.NotNull(coll);
Assert.Throws<DependencyResolutionException>(() => outer.Resolve<IList<object>>());
}
示例8: GenericsAreTaggable
public void GenericsAreTaggable()
{
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(List<>))
.InstancePerMatchingLifetimeScope("tag")
.As(typeof(IList<>));
var outer = builder.Build();
var inner = outer.BeginLifetimeScope("tag");
var coll = inner.Resolve<IList<object>>();
Assert.NotNull(coll);
var threw = false;
try
{
outer.Resolve<IList<object>>();
}
catch (Exception)
{
threw = true;
}
Assert.True(threw);
}
示例9: Load
protected override void Load(ContainerBuilder builder)
{
bool enableLocalization = true;
string absoluteFileName = HostingEnvironment.MapPath("~/Mvc.sitemap");
TimeSpan absoluteCacheExpiration = TimeSpan.FromMinutes(5);
bool visibilityAffectsDescendants = true;
bool useTitleIfDescriptionNotProvided = true;
bool securityTrimmingEnabled = false;
string[] includeAssembliesForScan = new string[] { "QuizProjectMvc.Web" };
var currentAssembly = this.GetType().Assembly;
var siteMapProviderAssembly = typeof(SiteMaps).Assembly;
var allAssemblies = new Assembly[] { currentAssembly, siteMapProviderAssembly };
var excludeTypes = new Type[] {
// Use this array to add types you wish to explicitly exclude from convention-based
// auto-registration. By default all types that either match I[TypeName] = [TypeName] or
// I[TypeName] = [TypeName]Adapter will be automatically wired up as long as they don't
// have the [ExcludeFromAutoRegistrationAttribute].
//
// If you want to override a type that follows the convention, you should add the name
// of either the implementation name or the interface that it inherits to this list and
// add your manual registration code below. This will prevent duplicate registrations
// of the types from occurring.
// Example:
// typeof(SiteMap),
// typeof(SiteMapNodeVisibilityProviderStrategy)
};
var multipleImplementationTypes = new Type[] {
typeof(ISiteMapNodeUrlResolver),
typeof(ISiteMapNodeVisibilityProvider),
typeof(IDynamicNodeProvider)
};
// Matching type name (I[TypeName] = [TypeName]) or matching type name + suffix Adapter (I[TypeName] = [TypeName]Adapter)
// and not decorated with the [ExcludeFromAutoRegistrationAttribute].
CommonConventions.RegisterDefaultConventions(
(interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).SingleInstance(),
new Assembly[] { siteMapProviderAssembly },
allAssemblies,
excludeTypes,
string.Empty);
// Multiple implementations of strategy based extension points (and not decorated with [ExcludeFromAutoRegistrationAttribute]).
CommonConventions.RegisterAllImplementationsOfInterface(
(interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).SingleInstance(),
multipleImplementationTypes,
allAssemblies,
excludeTypes,
string.Empty);
// Registration of internal controllers
CommonConventions.RegisterAllImplementationsOfInterface(
(interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).AsSelf().InstancePerDependency(),
new Type[] { typeof(IController) },
new Assembly[] { siteMapProviderAssembly },
new Type[0],
string.Empty);
// Visibility Providers
builder.RegisterType<SiteMapNodeVisibilityProviderStrategy>()
.As<ISiteMapNodeVisibilityProviderStrategy>()
.WithParameter("defaultProviderName", string.Empty);
// Pass in the global controllerBuilder reference
builder.RegisterInstance(ControllerBuilder.Current)
.As<ControllerBuilder>();
builder.RegisterType<ControllerTypeResolverFactory>()
.As<IControllerTypeResolverFactory>()
.WithParameter("areaNamespacesToIgnore", new string[0]);
// Configure Security
builder.RegisterType<AuthorizeAttributeAclModule>()
.Named<IAclModule>("authorizeAttributeAclModule");
builder.RegisterType<XmlRolesAclModule>()
.Named<IAclModule>("xmlRolesAclModule");
builder.RegisterType<CompositeAclModule>()
.As<IAclModule>()
.WithParameter(
(p, c) => p.Name == "aclModules",
(p, c) => new[]
{
c.ResolveNamed<IAclModule>("authorizeAttributeAclModule"),
c.ResolveNamed<IAclModule>("xmlRolesAclModule")
});
builder.RegisterInstance(System.Runtime.Caching.MemoryCache.Default)
.As<System.Runtime.Caching.ObjectCache>();
builder.RegisterGeneric(typeof(RuntimeCacheProvider<>))
.As(typeof(ICacheProvider<>));
builder.RegisterType<RuntimeFileCacheDependency>()
.Named<ICacheDependency>("cacheDependency1")
.WithParameter("fileName", absoluteFileName);
builder.RegisterType<CacheDetails>()
.Named<ICacheDetails>("cacheDetails1")
//.........这里部分代码省略.........