本文整理汇总了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);
}
}
示例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());
}
}
示例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);
}
}
示例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();
}
示例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);
}
示例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);
}
示例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>();
}
示例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);
}
}
示例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>();
}
示例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();
}
示例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>();
}
示例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);
}
示例13: GetInstance_UnknownService_ExecutesCompositionRootInSourceAssembly
public void GetInstance_UnknownService_ExecutesCompositionRootInSourceAssembly()
{
var container = new ServiceContainer();
container.GetInstance<IFoo>();
Assert.AreEqual(1, CompositionRoot.CallCount);
}
示例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);
}
示例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));
}