本文整理汇总了C#中ContainerBuilder类的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder类的具体用法?C# ContainerBuilder怎么用?C# ContainerBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ContainerBuilder类属于命名空间,在下文中一共展示了ContainerBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Load
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new WorkingDirectory(c.Resolve<DirectoryInfo>())).As<IWorkingDirectory>().ContainerScoped();
builder.Register(c => new Repository(c.Resolve<IWorkingDirectory>())).As<IRepository>().ContainerScoped();
builder.Register(c => new Index(c.Resolve<IRepository>())).As<IIndex>();
builder.Register(c => new GitLib()).As<IGit>();
}
示例2: Can_register_and_use_generics_with_configuration_api
public void Can_register_and_use_generics_with_configuration_api()
{
var configuration = new LightCoreConfiguration();
var registrations = new List<Registration>
{
new Registration
{
ContractType = typeof(IFoo).AssemblyQualifiedName,
ImplementationType = typeof(Foo).AssemblyQualifiedName,
Arguments = new List<Argument>
{
new Argument
{
Type = "Guid",
Value = "354c11f1-94e5-41b8-9a13-122e2df2b0c7"
}
}
}
};
configuration.Registrations = registrations;
var builder = new ContainerBuilder();
RegistrationLoader.Instance.Register(builder, configuration);
var container = builder.Build();
Guid expected = new Guid("354c11f1-94e5-41b8-9a13-122e2df2b0c7");
Assert.AreEqual(expected, ((Foo)container.Resolve<IFoo>()).Arg4);
}
示例3: Instance_is_reused_on_same_thread_when_controlled_by_threadsingleton_lifecycle
public void Instance_is_reused_on_same_thread_when_controlled_by_threadsingleton_lifecycle()
{
var builder = new ContainerBuilder();
builder.DefaultControlledBy<ThreadSingletonLifecycle>();
builder.Register<IFoo, Foo>();
builder.Register<IBar, Bar>();
var container = builder.Build();
var threadData = new ThreadData(container);
var thread = new Thread(threadData.ResolveFoosWithContainer);
var threadDataTwo = new ThreadData(container);
var threadTwo = new Thread(threadDataTwo.ResolveFoosWithContainer);
thread.Start();
threadTwo.Start();
thread.Join();
threadTwo.Join();
Assert.IsTrue(ReferenceEquals(threadData.FooOne, threadData.FooTwo));
Assert.IsFalse(ReferenceEquals(threadData.FooOne, threadDataTwo.FooOne));
}
示例4: 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"));
}
示例5: SetUp
public void SetUp()
{
var builder = new ContainerBuilder();
builder.RegisterMetadataRegistrationSources();
builder.RegisterType<object>();
_container = builder.Build();
}
示例6: ConcurrentResolveOperationsForNonSharedInstancesFromDifferentLifetimes_DoNotBlock
public void ConcurrentResolveOperationsForNonSharedInstancesFromDifferentLifetimes_DoNotBlock()
{
var evt = new ManualResetEvent(false);
var builder = new ContainerBuilder();
builder.Register((c, p) =>
{
if (p.TypedAs<bool>())
evt.WaitOne();
return new object();
});
var container = builder.Build();
var unblocked = 0;
var blockedScope = container.BeginLifetimeScope();
var blockedThread = new Thread(() =>
{
blockedScope.Resolve<object>(TypedParameter.From(true));
Interlocked.Increment(ref unblocked);
});
blockedThread.Start();
Thread.Sleep(500);
container.Resolve<object>(TypedParameter.From(false));
container.BeginLifetimeScope().Resolve<object>(TypedParameter.From(false));
Thread.MemoryBarrier();
Assert.Equal(0, unblocked);
evt.Set();
blockedThread.Join();
}
示例7: DefaultLifetimeForMefComponentsIsSingleton
public void DefaultLifetimeForMefComponentsIsSingleton()
{
var builder = new ContainerBuilder();
var catalog = new TypeCatalog(typeof(HasDefaultCreationPolicy));
builder.RegisterComposablePartCatalog(catalog);
AssertDisposalTrackerIsSingleton(builder);
}
示例8: 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);
}
示例9: Initialise
public void Initialise()
{
var builder = new ContainerBuilder();
builder.RegisterType<Logger>().As<ILogger>();
builder.RegisterType<Repository>().As<IRepository>();
builder.Build();
}
示例10: 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();
}
示例11: Can_register_and_use_closed_to_closed_generic_with_configuration_api
public void Can_register_and_use_closed_to_closed_generic_with_configuration_api()
{
var configuration = new LightCoreConfiguration();
var registrations = new List<Registration>
{
new Registration
{
// LightCore.TestTypes.IRepository`1, LightCore.TestTypes
ContractType = typeof (IRepository<Bar>).AssemblyQualifiedName,
ImplementationType = typeof (BarRepository).AssemblyQualifiedName
}
};
configuration.Registrations = registrations;
var builder = new ContainerBuilder();
RegistrationLoader.Instance.Register(builder, configuration);
var container = builder.Build();
var repository = container.Resolve<IRepository<Bar>>();
Assert.That(repository, Is.Not.Null);
Assert.That(repository, Is.InstanceOf<BarRepository>());
}
示例12: ContainerScope_DefaultServiceRegistrationUsingPreservation
public void ContainerScope_DefaultServiceRegistrationUsingPreservation()
{
var builder = new ContainerBuilder();
builder.RegisterInstance("s1").PreserveExistingDefaults();
var container = builder.Build();
Assert.Equal("s1", container.Resolve<string>());
}
示例13: run
static void run()
{
var builder = new ContainerBuilder();
var registry = new AutofacDependencyRegistryBuilder(builder).build();
Resolve.initialize_with(registry);
builder.Register(x => registry).As<DependencyRegistry>().SingletonScoped();
//needs startups
builder.Register<StartServiceBus>().As<NeedStartup>();
// infrastructure
var manager = new QueueManager(new IPEndPoint(IPAddress.Loopback, 2201), "client.esent");
manager.CreateQueues("client");
builder.Register(x => new RhinoPublisher("server", 2200, manager)).As<ServiceBus>().SingletonScoped();
builder.Register(x => new RhinoReceiver(manager.GetQueue("client"), x.Resolve<CommandProcessor>())).As<RhinoReceiver>().As<Receiver>().SingletonScoped();
// commanding
//builder.Register<AsynchronousCommandProcessor>().As<CommandProcessor>().SingletonScoped();
builder.Register<SynchronousCommandProcessor>().As<CommandProcessor>().SingletonScoped();
builder.Register<RequestHandler>().As<Handler>();
Resolve.the<IEnumerable<NeedStartup>>().each(x => x.run());
Resolve.the<CommandProcessor>().run();
"started".log();
}
示例14: ContainerBuilder_can_initialize_container
public void ContainerBuilder_can_initialize_container()
{
var builder = new ContainerBuilder();
var container = builder.Build();
Assert.IsNotNull(container);
}
示例15: OnCircularDependency_MessageDescribesCycle
public void OnCircularDependency_MessageDescribesCycle()
{
try
{
var builder = new ContainerBuilder();
builder.Register(c => c.Resolve<object>());
var target = builder.Build();
target.Resolve<object>();
}
catch (DependencyResolutionException de)
{
Assert.IsNull(de.InnerException);
Assert.IsTrue(de.Message.Contains("System.Object -> System.Object"));
Assert.IsFalse(de.Message.Contains("System.Object -> System.Object -> System.Object"));
return;
}
catch (Exception ex)
{
Assert.Fail("Expected a DependencyResolutionException, got {0}.", ex);
return;
}
Assert.Fail("Expected a DependencyResolutionException.");
}