本文整理汇总了C#中ContainerBuilder.Build方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.Build方法的具体用法?C# ContainerBuilder.Build怎么用?C# ContainerBuilder.Build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder.Build方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetUp
public void SetUp()
{
var cb = new ContainerBuilder();
cb.RegisterModule(new QuartzAutofacJobsModule(Assembly.GetExecutingAssembly()));
_container = cb.Build();
}
示例2: WrapperResolvesAuthenticationFilterFromDependencyScope
public void WrapperResolvesAuthenticationFilterFromDependencyScope()
{
var builder = new ContainerBuilder();
builder.Register<ILogger>(c => new Logger()).InstancePerDependency();
var activationCount = 0;
builder.Register<IAutofacAuthenticationFilter>(c => new TestAuthenticationFilter(c.Resolve<ILogger>()))
.AsWebApiAuthenticationFilterFor<TestController>(c => c.Get())
.InstancePerRequest()
.OnActivated(e => activationCount++);
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
var configuration = new HttpConfiguration { DependencyResolver = resolver };
var requestMessage = new HttpRequestMessage();
requestMessage.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, configuration);
var contollerContext = new HttpControllerContext { Request = requestMessage };
var controllerDescriptor = new HttpControllerDescriptor { ControllerType = typeof(TestController) };
var methodInfo = typeof(TestController).GetMethod("Get");
var actionDescriptor = new ReflectedHttpActionDescriptor(controllerDescriptor, methodInfo);
var actionContext = new HttpActionContext(contollerContext, actionDescriptor);
var context = new HttpAuthenticationContext(actionContext, Thread.CurrentPrincipal);
var metadata = new FilterMetadata
{
ControllerType = typeof(TestController),
FilterScope = FilterScope.Action,
MethodInfo = methodInfo
};
var wrapper = new AuthenticationFilterWrapper(metadata);
wrapper.OnAuthenticate(context);
Assert.That(activationCount, Is.EqualTo(1));
}
示例3: Initialise
public void Initialise()
{
var builder = new ContainerBuilder();
builder.RegisterType<Logger>().As<ILogger>();
builder.RegisterType<Repository>().As<IRepository>();
builder.Build();
}
示例4: RegisterAutofac
public static void RegisterAutofac()
{
var builder = new ContainerBuilder();
// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// OPTIONAL: Register model binders that require DI.
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
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();
// Register services
RegisterServices(builder);
// Set the dependency resolver to be Autofac.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
示例5: WhenTIsRegisteredByName_CanResolveLazyTByName
public void WhenTIsRegisteredByName_CanResolveLazyTByName()
{
var builder = new ContainerBuilder();
builder.RegisterType<object>().Named<object>("foo");
var container = builder.Build();
Assert.True(container.IsRegisteredWithName<Lazy<object>>("foo"));
}
示例6: EnsureWebTypeIsRegistered
public void EnsureWebTypeIsRegistered(Type serviceType)
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule(new AutofacWebTypesModule());
IContainer container = builder.Build();
Assert.That(container.IsRegistered(serviceType), Is.True);
}
示例7: ContainerScope_DefaultServiceRegistrationUsingPreservation
public void ContainerScope_DefaultServiceRegistrationUsingPreservation()
{
var builder = new ContainerBuilder();
builder.RegisterInstance("s1").PreserveExistingDefaults();
var container = builder.Build();
Assert.Equal("s1", container.Resolve<string>());
}
示例8: CollectionInNestedLifetimeScope
public void CollectionInNestedLifetimeScope()
{
// Issue #711
// Note #711 was using named collections; this test is not.
// Named collections don't have different behavior than the standard
// auto-generated behavior from a resolve standpoint since you
// can't resolve a specifically named collection.
var cb = new ContainerBuilder();
cb.RegisterType<Foo1>().As<IFoo>();
cb.RegisterType<Foo2>().As<IFoo>();
using (var container = cb.Build())
{
var collection = container.Resolve<IEnumerable<IFoo>>();
Assert.Equal(2, collection.Count());
using (var scope = container.BeginLifetimeScope())
{
collection = container.Resolve<IEnumerable<IFoo>>();
Assert.Equal(2, collection.Count());
}
using (var scope = container.BeginLifetimeScope(b => { }))
{
collection = container.Resolve<IEnumerable<IFoo>>();
Assert.Equal(2, collection.Count());
}
}
}
示例9: CollectionsAreTaggable
public void CollectionsAreTaggable()
{
var builder = new ContainerBuilder();
builder.RegisterCollection<object>("o")
.InstancePerMatchingLifetimeScope("tag")
.As(typeof(IList<object>));
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);
}
示例10: ContainerBuilder_can_initialize_container
public void ContainerBuilder_can_initialize_container()
{
var builder = new ContainerBuilder();
var container = builder.Build();
Assert.IsNotNull(container);
}
示例11: Instance_is_not_reused_in_different_httprequests
public void Instance_is_not_reused_in_different_httprequests()
{
var builder = new ContainerBuilder();
var currentItems = new Dictionary<string, object>();
var currentContext = new Mock<HttpContextBase>();
currentContext
.Setup(c => c.Items)
.Returns(currentItems);
builder.DefaultControlledBy(() => new HttpRequestLifecycle
{
CurrentContext = currentContext.Object
});
builder.Register<IBar, Bar>().ControlledBy<TransientLifecycle>();
builder.Register<IFoo, Foo>();
var container = builder.Build();
var firstResolvedInstanceInContext = container.Resolve<IFoo>();
currentItems = new Dictionary<string, object>();
// Simulate new request scope.
currentContext
.Setup(c => c.Items)
.Returns(currentItems);
var secondResolvedInstanceInContext = container.Resolve<IFoo>();
Assert.AreNotSame(firstResolvedInstanceInContext, secondResolvedInstanceInContext);
}
示例12: CanCreateMixinWithClassInterceptors
public void CanCreateMixinWithClassInterceptors()
{
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new Dictionary<int, int>());
var builder = new ContainerBuilder();
builder.RegisterType<C>().EnableClassInterceptors(options);
builder.RegisterType<AddOneInterceptor>();
builder.RegisterType<AddTenInterceptor>();
var container = builder.Build();
var i = 10;
var cpt = container.Resolve<C>(TypedParameter.From(i));
var dict = cpt as IDictionary<int, int>;
Assert.NotNull(dict);
dict.Add(1, 2);
Assert.Equal(2, dict[1]);
dict.Clear();
Assert.Empty(dict);
}
开发者ID:jango2015,项目名称:Autofac.Extras.DynamicProxy,代码行数:25,代码来源:ClassInterceptorsWithOptionsFixture.cs
示例13: Can_configure_and_resolve_type_registration_with_alias
public void Can_configure_and_resolve_type_registration_with_alias()
{
var configuration = new LightCoreConfiguration();
configuration.TypeAliases.Add(new TypeAlias { Alias = "IBar", Type = typeof(IBar).AssemblyQualifiedName });
configuration.TypeAliases.Add(new TypeAlias { Alias = "Bar", Type = typeof(Bar).AssemblyQualifiedName });
var registrations = new List<Registration>
{
new Registration
{
ContractType = "IBar",
ImplementationType = "Bar",
}
};
configuration.RegistrationGroups.Add(new RegistrationGroup()
{
Registrations = registrations
});
var builder = new ContainerBuilder();
RegistrationLoader.Instance.Register(builder, configuration);
var container = builder.Build();
var bar = container.Resolve<IBar>();
Assert.NotNull(bar);
}
示例14: Main
/// <summary>
/// Main entry point for the program.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
var builder = new ContainerBuilder();
var module = new XamlRegistrationModule();
builder.Register("Test");
//builder.Register<IRepository<Foo>, FooRepository>();
builder.RegisterModule(module);
builder.Register<IBar, Bar>();
var container = builder.Build();
var fooRepository = container.Resolve<IRepository<Foo, Bar>>();
var fsdfsdfd = fooRepository;
var func = container.Resolve<Func<string>>();
var test = func();
var namedScreen = container.Resolve<WelcomeScreen>();
namedScreen.WriteText();
Console.Read();
}
示例15: LightCoreDelegateUseCase
public LightCoreDelegateUseCase()
{
var builder = new ContainerBuilder();
builder.Register<IWebApp>(
c => new WebApp(
c.Resolve<IAuthenticator>(),
c.Resolve<IStockQuote>()));
builder.Register<IAuthenticator>(
c => new Authenticator(
c.Resolve<ILogger>(),
c.Resolve<IErrorHandler>(),
c.Resolve<IDatabase>()));
builder.Register<IStockQuote>(
c => new StockQuote(
c.Resolve<ILogger>(),
c.Resolve<IErrorHandler>(),
c.Resolve<IDatabase>()));
builder.Register<ILogger>(
c => new Logger());
builder.Register<IErrorHandler>(
c => new ErrorHandler(
c.Resolve<ILogger>()));
builder.Register<IDatabase>(
c => new Database(
c.Resolve<ILogger>(),
c.Resolve<IErrorHandler>()));
this._container = builder.Build();
}