• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# ContainerBuilder类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中ContainerBuilder的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder类的具体用法?C# ContainerBuilder怎么用?C# ContainerBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ContainerBuilder类属于命名空间,在下文中一共展示了ContainerBuilder类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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>();
 }
开发者ID:kthompson,项目名称:gitty_old,代码行数:7,代码来源:GitCommandLine.cs


示例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);
        }
开发者ID:peterbucher,项目名称:LightCore,代码行数:32,代码来源:AliasTests.cs


示例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));
        }
开发者ID:AlexZeitler,项目名称:LightCore,代码行数:25,代码来源:LifecycleTests.cs


示例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"));
 }
开发者ID:arronchen,项目名称:Autofac,代码行数:7,代码来源:LazyRegistrationSourceTests.cs


示例5: SetUp

 public void SetUp()
 {
     var builder = new ContainerBuilder();
     builder.RegisterMetadataRegistrationSources();
     builder.RegisterType<object>();
     _container = builder.Build();
 }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:7,代码来源:LazyWithMetadataRegistrationSourceTests.cs


示例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();
        }
开发者ID:arronchen,项目名称:Autofac,代码行数:32,代码来源:ConcurrentTests.cs


示例7: DefaultLifetimeForMefComponentsIsSingleton

 public void DefaultLifetimeForMefComponentsIsSingleton()
 {
     var builder = new ContainerBuilder();
     var catalog = new TypeCatalog(typeof(HasDefaultCreationPolicy));
     builder.RegisterComposablePartCatalog(catalog);
     AssertDisposalTrackerIsSingleton(builder);
 }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:7,代码来源:DisposalRegistrationTests.cs


示例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);
 }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:7,代码来源:AutofacWebTypesModuleFixture.cs


示例9: Initialise

 public void Initialise()
 {
     var builder = new ContainerBuilder();
     builder.RegisterType<Logger>().As<ILogger>();
     builder.RegisterType<Repository>().As<IRepository>();
     builder.Build();
 }
开发者ID:darrenscrine,项目名称:AutofacDI,代码行数:7,代码来源:RepositoryTests.cs


示例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();
        }
开发者ID:peterbucher,项目名称:LightCore,代码行数:35,代码来源:LightCoreDelegateUseCase.cs


示例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>());
        }
开发者ID:peterbucher,项目名称:LightCore,代码行数:26,代码来源:ConfigurationTests.cs


示例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>());
 }
开发者ID:GitHuang,项目名称:Autofac,代码行数:7,代码来源:PreserveExistingDefaultsTests.cs


示例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();
        }
开发者ID:mokhan,项目名称:ping.pong,代码行数:28,代码来源:Client.cs


示例14: ContainerBuilder_can_initialize_container

        public void ContainerBuilder_can_initialize_container()
        {
            var builder = new ContainerBuilder();
            var container = builder.Build();

            Assert.IsNotNull(container);
        }
开发者ID:AlexZeitler,项目名称:LightCore,代码行数:7,代码来源:ContainerTests.cs


示例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.");
        }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:25,代码来源:CircularDependencyDetectorTests.cs


示例16: ProvidedLifetimeScopeExposed

        public void ProvidedLifetimeScopeExposed()
        {
            var container = new ContainerBuilder().Build();
            var dependencyResolver = new AutofacDependencyResolver(container);

            Assert.Equal(container, dependencyResolver.LifetimeScope);
        }
开发者ID:cybernexus,项目名称:Autofac.SignalR,代码行数:7,代码来源:AutofacDependencyResolverFixture.cs


示例17: 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());
                }
            }
        }
开发者ID:GitHuang,项目名称:Autofac,代码行数:28,代码来源:CollectionRegistrationSourceTests.cs


示例18: 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));
        }
开发者ID:tddold,项目名称:ASP.NET-MVC-Projects,代码行数:27,代码来源:AutofacConfig.cs


示例19: Register

 public void Register(ContainerBuilder builder, IEnumerable<Type> possibleTypes)
 {
     foreach (Type possibleType in this.DiscoverDependencies(possibleTypes))
     {
         this.InternalRegister(builder, possibleType);
     }
 }
开发者ID:pekkah,项目名称:Autofac.Conventions,代码行数:7,代码来源:ConventionModel.cs


示例20: SetUp

        public void SetUp()
        {
            var cb = new ContainerBuilder();
            cb.RegisterModule(new QuartzAutofacJobsModule(Assembly.GetExecutingAssembly()));

            _container = cb.Build();
        }
开发者ID:JustAGhosT,项目名称:Autofac.Extras.Quartz,代码行数:7,代码来源:QuartzAutofacModuleTests.cs



注:本文中的ContainerBuilder类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ContainerConfiguration类代码示例发布时间:2022-05-24
下一篇:
C# Container类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap