当前位置: 首页>>代码示例>>C#>>正文


C# ServiceContainer.GetInstance方法代码示例

本文整理汇总了C#中ServiceContainer.GetInstance方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.GetInstance方法的具体用法?C# ServiceContainer.GetInstance怎么用?C# ServiceContainer.GetInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceContainer的用法示例。


在下文中一共展示了ServiceContainer.GetInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        public static void Main(string[] args)
        {
            var argumentParser = ApplicationArgumentsConfigurationSetup.CreateCommandLineParser();

            ICommandLineParserResult parseResult = argumentParser.Parse(args);

            if (!parseResult.HasErrors && !parseResult.EmptyArgs)
            {
                using (var serviceContainer = new ServiceContainer())
                {
                    serviceContainer.RegisterFrom<CompositionRoot>();
                    IFileSystemMonitorServiceFactory factory = serviceContainer.GetInstance<IFileSystemMonitorServiceFactory>(CompositionRoot.LoggingFileSystemMonitorServiceFactory);
                    FileSystemMonitorServiceConfiguration configuration = CreateConfiguration(argumentParser.Object);

                    var logger = serviceContainer.GetInstance<ILogger>();
                    logger.Log(LogLevel.Trace, "From: {0} To: {1}", configuration.FolderToMonitor, configuration.TargetFolder);

                    FileSystemMonitorService service = factory.Create(configuration);

                    ServiceBase.Run(service);
                }
            }
            else
            {
                argumentParser.HelpOption.ShowHelp(argumentParser.Options);
            }
        }
开发者ID:RamanBut-Husaim,项目名称:.NET-Practice,代码行数:27,代码来源:Program.cs

示例2: GetInstance_NoServices_CallsAssemblyScannerOnlyOnce

        public void GetInstance_NoServices_CallsAssemblyScannerOnlyOnce()
        {
            var scannerMock = new Mock<IAssemblyScanner>();
            var serviceContainer = new ServiceContainer();
            serviceContainer.AssemblyScanner = scannerMock.Object;
            try
            {
                serviceContainer.GetInstance<IFoo>();
            }
            catch
            {
                try
                {
                    serviceContainer.GetInstance<IFoo>();
                }
                catch
                {

                }
            }
            finally
            {
                scannerMock.Verify(a => a.Scan(typeof(IFoo).Assembly, It.IsAny<IServiceRegistry>(), LifeCycleType.Transient, It.IsAny<Func<Type, bool>>()), Times.Once());
            }
        }
开发者ID:stegenish,项目名称:LightInject,代码行数:25,代码来源:AssemblyScannerTests.cs

示例3: Main

        public static void Main(string[] args)
        {
            var argumentParser = ApplicationArgumentsConfiguration.CreateCommandLineParser();

            ICommandLineParserResult parseResult = argumentParser.Parse(args);

            if (!parseResult.HasErrors && !parseResult.EmptyArgs)
            {
                var configuration = CreateConfiguration(argumentParser.Object);
                ConfigurationProvider.Instance.Configuration = configuration;

                using (var serviceContainer = new ServiceContainer())
                {
                    serviceContainer.RegisterFrom<CompositionRoot>();
                    var factory = serviceContainer.GetInstance<CentralHostServiceFactory>();

                    CentralHostService service = factory.Create(configuration);

                    var logger = serviceContainer.GetInstance<ILogger>();

                    logger.Trace("[Before Service base run]");
                    ServiceBase.Run(service);
                    logger.Trace("[After Service base run]");
                }
            }
            else
            {
                argumentParser.HelpOption.ShowHelp(argumentParser.Options);
            }
        }
开发者ID:RamanBut-Husaim,项目名称:.NET-Practice,代码行数:30,代码来源:Program.cs

示例4: Install_With_ShouldNotBeNull

        public void Install_With_ShouldNotBeNull()
        {
            var container = new ServiceContainer();

            container.RegisterFrom<SettingsCompositionRoot>();

            var setting = container.GetInstance<ISettingsExtractor>();

            setting.ShouldNotBeNull();

            var section = container.GetInstance<ISectionExtractor>();

            section.ShouldNotBeNull();
        }
开发者ID:raulnq,项目名称:Jal.Settings,代码行数:14,代码来源:SettingsCompositionRootTests.cs

示例5: SingletonRegister_Success

        public void SingletonRegister_Success()
        {
            ITestCase testCase = new TestCaseA();

            var c = new ServiceContainer();
            c = (ServiceContainer)testCase.SingletonRegister(c);

            var obj1 = c.GetInstance<ITestA>();
            var obj2 = c.GetInstance<ITestA>();

            Assert.AreEqual(obj1, obj2);
            Helper.Check(obj1, true);
            Helper.Check(obj2, true);
        }
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:14,代码来源:TestCaseATests.cs

示例6: TransientRegister_Success

        public void TransientRegister_Success()
        {
            ITestCase testCase = new TestCaseA();

            var c = new ServiceContainer();
            c = (ServiceContainer)testCase.TransientRegister(c);

            var obj1 = c.GetInstance<ITestA>();
            var obj2 = c.GetInstance<ITestA>();

            Assert.AreNotEqual(obj1, obj2);
            Helper.Check(obj1, false);
            Helper.Check(obj2, false);
        }
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:14,代码来源:TestCaseATests.cs

示例7: test

        public void test()
        {
            var container = new ServiceContainer();
            container.Register<IFoo, FooWithDependency>();
            container.Register<IBar, Bar>();
            try
            {
                container.GetInstance<IFoo>();
            }
            catch (Exception)
            {

            }
            container.GetInstance<IFoo>();
        }
开发者ID:stangelandcl,项目名称:LightInject,代码行数:15,代码来源:ILGeneratorTests.cs

示例8: GetInstance_SameExecutionContext_InstancesAreSame

        public void GetInstance_SameExecutionContext_InstancesAreSame()
        {
            var container = new ServiceContainer();
            container.ScopeManagerProvider = new PerLogicalCallContextScopeManagerProvider();
            container.Register<IBar, Bar>(new PerScopeLifetime());
            container.Register<IAsyncFoo, AsyncFoo>();

            using (container.BeginScope())
            {
                var firstBar = container.GetInstance<IBar>();
                var instance = container.GetInstance<IAsyncFoo>();
                var secondBar = instance.GetBar().Result;
                Assert.Same(firstBar, secondBar);
            }
        }
开发者ID:vitamink,项目名称:LightInject,代码行数:15,代码来源:AsyncTests.cs

示例9: Create_WithCustomerOlderThan25_ShouldBeNotEmpty

        public void Create_WithCustomerOlderThan25_ShouldBeNotEmpty()
        {
            var container = new ServiceContainer();

            var directory = AppDomain.CurrentDomain.BaseDirectory;

            var finder = AssemblyFinder.Builder.UsePath(directory).Create;

            container.RegisterFrom<ServiceLocatorCompositionRoot>();

            var assemblies = finder.GetAssembliesTagged<AssemblyTagAttribute>();

            container.RegisterFactory(assemblies);

            container.Register<IDoSomething, DoSomething>(typeof(DoSomething).FullName);

            var factory = container.GetInstance<IObjectFactory>();

            var customer = new Customer(){Age = 25};

            var services = factory.Create<Customer, IDoSomething>(customer);

            services.ShouldNotBeEmpty();

            services.Length.ShouldBe(1);

            services[0].ShouldBeAssignableTo<IDoSomething>();

            services[0].ShouldBeOfType<DoSomething>();
        }
开发者ID:raulnq,项目名称:Jal.Factory,代码行数:30,代码来源:Tests.cs

示例10: Main

		static void Main(string[] args)
		{
			Console.WriteLine("Start");

			var container = new ServiceContainer();
			container.Register<IDataBaseController, DataBaseController>();

			IDataBaseController _dbController = container.GetInstance<IDataBaseController>();

			Product p1 = new Product()
			{
				Id = 1
			};

			Product p2 = new Product()
			{
				Id = 2
			};

			Product p3 = null;

			_dbController.InsertProduct(p1);
			_dbController.InsertProduct(p3);
			_dbController.InsertProduct(p2);

			Console.WriteLine("End");

			Console.ReadLine();
		}
开发者ID:AlehSkamarokhau,项目名称:PrototypeApps,代码行数:29,代码来源:Program.cs

示例11: RegisterFactory_WithCompositionRoot_ShouldBeNotNull

        public void RegisterFactory_WithCompositionRoot_ShouldBeNotNull()
        {
            var container = new ServiceContainer();

            var directory = AppDomain.CurrentDomain.BaseDirectory;

            var finder = AssemblyFinder.Builder.UsePath(directory).Create;

            container.RegisterFrom<ServiceLocatorCompositionRoot>();

            var assemblies = finder.GetAssembliesTagged<AssemblyTagAttribute>();

            container.RegisterFactory(assemblies);

            container.Register<IDoSomething, DoSomething>(typeof(DoSomething).FullName);

            var instance = container.GetInstance<IObjectFactory>();

            instance.ShouldNotBeNull();

            instance.ConfigurationProvider.Configuration.Items.ShouldNotBeEmpty();

            instance.ConfigurationProvider.Configuration.Items.Count.ShouldBe(1);

            instance.ConfigurationProvider.Sources.ShouldNotBeEmpty();

            instance.ConfigurationProvider.Sources.Length.ShouldBe(2);

            instance.ShouldBeAssignableTo<IObjectFactory>();

            instance.ShouldBeOfType<ObjectFactory>();
        }
开发者ID:raulnq,项目名称:Jal.Factory,代码行数:32,代码来源:ServiceContainerExtensionTests.cs

示例12: GetInstance_ClassWithObjectPropertyCanGetInstance

 public void GetInstance_ClassWithObjectPropertyCanGetInstance()
 {
     var container = new ServiceContainer();
     container.Register<object, Foo>();
     container.Register<FooWithObjectProperty>();
     var instance = container.GetInstance<FooWithObjectProperty>();
     Assert.NotNull(instance.Property);
 }
开发者ID:joelbourbon,项目名称:LightInject,代码行数:8,代码来源:PropertyInjectionTests.cs

示例13: GetInstance_UnknownService_ExecutesCompositionRootInSourceAssembly

        public void GetInstance_UnknownService_ExecutesCompositionRootInSourceAssembly()
        {
            var container = new ServiceContainer();

            container.GetInstance<IFoo>();

            Assert.AreEqual(1, CompositionRoot.CallCount);
        }
开发者ID:stangelandcl,项目名称:LightInject,代码行数:8,代码来源:LazyCompositionRootTests.cs

示例14: GetInstance_ClassWithIndexer_CanGetInstance

 public void GetInstance_ClassWithIndexer_CanGetInstance()
 {
     var container = new ServiceContainer();
     container.Register<object, Foo>();
     container.Register<FooWithIndexer>();
     var instance = container.GetInstance<FooWithIndexer>();
     Assert.NotNull(instance);
 }
开发者ID:joelbourbon,项目名称:LightInject,代码行数:8,代码来源:PropertyInjectionTests.cs

示例15: Intercept_Interceptor_DoesNotReturnProxyInstance

        public void Intercept_Interceptor_DoesNotReturnProxyInstance()
        {
            var container = new ServiceContainer();            
            container.Register<IInterceptor, SampleInterceptor>();
            container.Intercept(sr => sr.ServiceType == typeof(IInterceptor), factory => factory.GetInstance<IInterceptor>());
            var instance = container.GetInstance<IInterceptor>();

            Assert.IsNotInstanceOfType(instance, typeof(IProxy));
        }               
开发者ID:vitamink,项目名称:LightInject,代码行数:9,代码来源:ContainerInterceptionTests.cs


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