本文整理汇总了C#中ServiceContainer.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.Dispose方法的具体用法?C# ServiceContainer.Dispose怎么用?C# ServiceContainer.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceContainer
的用法示例。
在下文中一共展示了ServiceContainer.Dispose方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ChildContainerShouldBeDisposed
public void ChildContainerShouldBeDisposed()
{
var ioc = new ServiceContainer();
var childContainer = ioc.CreateChildContainer();
ioc.Dispose();
Assert.That(() => childContainer.Clear(), Throws.TypeOf(typeof(ObjectDisposedException)));
}
示例2: DoTest
public override TestResult DoTest(ITestCase testCase, int testCasesNumber, bool singleton)
{
var result = new TestResult { Singleton = singleton, TestCasesNumber = testCasesNumber };
var sw = new Stopwatch();
var c = new ServiceContainer();
if (singleton)
{
sw.Start();
c = (ServiceContainer)testCase.SingletonRegister(c);
sw.Stop();
}
else
{
sw.Start();
c = (ServiceContainer)testCase.TransientRegister(c);
sw.Stop();
}
result.RegisterTime = sw.ElapsedMilliseconds;
sw.Reset();
result.ResolveTime = DoResolve(sw, testCase, c, testCasesNumber, singleton);
c.Dispose();
return result;
}
示例3: ComplexHierarchy
//.........这里部分代码省略.........
// Register root services after child1 is created to see whether new services
// are properly inherited.
root.Register(typeof(IService1), null, typeof(Service1), CreationPolicy.Shared);
root.Register(typeof(IService2), null, typeof(Service2), CreationPolicy.LocalShared);
root.Register(typeof(IService3), null, typeof(Service3), CreationPolicy.NonShared);
var child2 = child1.CreateChildContainer();
child2.Register(typeof(IService1), null, typeof(Service1), CreationPolicy.Shared);
child2.Register(typeof(IService1), "token2", typeof(DisposableService), CreationPolicy.Shared);
// Resolve services.
var service1InChild2 = child2.GetInstance<IService1>();
var service2InChild2 = child2.GetInstance<IService2>();
var service3InChild2 = child2.GetInstance<IService3>();
var disposableServiceInChild2 = child2.GetInstance<IService1>("token2");
var service3InChild1 = child1.GetInstance<IService3>();
var service2InChild1 = child1.GetInstance<IService2>();
var service1InChild1 = child1.GetInstance<IService1>();
var namedService1InChild1 = child1.GetInstance<IService1>("token1");
var namedService1InChild2 = child2.GetInstance<IService1>("token1");
var service1InRoot = root.GetInstance<IService1>();
var service3InRoot = root.GetInstance<IService3>();
var service2InRoot = root.GetInstance<IService2>();
// Check root services.
Assert.IsNotNull(service1InRoot);
Assert.IsNotNull(service2InRoot);
Assert.IsNotNull(service3InRoot);
Assert.IsNull(root.GetInstance<IService1>("token1"));
Assert.AreSame(root, root.GetInstance<ServiceContainer>());
Assert.AreSame(service1InRoot, root.GetInstance<IService1>());
Assert.AreSame(service2InRoot, root.GetInstance<IService2>());
Assert.AreNotSame(service3InRoot, root.GetInstance<IService3>());
Assert.AreSame(service1InRoot, service3InRoot.Service1);
Assert.AreSame(service2InRoot, service3InRoot.Service2);
Assert.IsNull(service2InRoot.Service1);
root.ResolveProperties(service2InRoot);
Assert.AreSame(service1InRoot, service2InRoot.Service1);
// Check child1 services.
Assert.IsNotNull(service1InChild1);
Assert.IsNotNull(service2InChild1);
Assert.IsNotNull(service3InChild1);
Assert.IsNotNull(namedService1InChild1);
Assert.AreSame(child1, child1.GetInstance<ServiceContainer>());
Assert.AreSame(service1InChild1, child1.GetInstance<IService1>());
Assert.AreSame(service2InChild1, child1.GetInstance<IService2>());
Assert.AreNotSame(service3InChild1, child1.GetInstance<IService3>());
Assert.AreSame(service1InChild1, service3InChild1.Service1);
Assert.AreSame(service2InChild1, service3InChild1.Service2);
Assert.IsNull(service2InChild1.Service1);
child1.ResolveProperties(service2InChild1);
Assert.AreSame(service1InChild1, service2InChild1.Service1);
Assert.AreSame(service1InRoot, service1InChild1);
Assert.AreNotSame(service2InRoot, service2InChild1);
// Check child2 services.
Assert.IsNotNull(service1InChild2);
Assert.IsNotNull(service2InChild2);
Assert.IsNotNull(service3InChild2);
Assert.IsNotNull(namedService1InChild2);
Assert.IsNotNull(disposableServiceInChild2);
Assert.AreSame(child2, child2.GetInstance<ServiceContainer>());
Assert.AreSame(service1InChild2, child2.GetInstance<IService1>());
Assert.AreSame(service2InChild2, child2.GetInstance<IService2>());
Assert.AreNotSame(service3InChild2, child2.GetInstance<IService3>());
Assert.AreSame(service1InChild2, service3InChild2.Service1);
Assert.AreSame(service2InChild2, service3InChild2.Service2);
Assert.IsNull(service2InChild2.Service1);
child2.ResolveProperties(service2InChild2);
Assert.AreSame(service1InChild2, service2InChild2.Service1);
Assert.AreNotSame(service1InRoot, service1InChild2);
Assert.AreNotSame(service1InChild1, service1InChild2);
Assert.AreNotSame(service2InRoot, service2InChild2);
Assert.AreNotSame(service2InChild1, service2InChild2);
Assert.AreSame(child1.GetInstance<IService1>("token1"), child2.GetInstance<IService1>("token1"));
Assert.AreSame(disposableServiceInChild2, child2.GetInstance<IService1>("token2"));
var service1InstancesInChild2 = child2.GetAllInstances<IService1>().ToArray();
Assert.AreEqual(2, service1InstancesInChild2.Length);
Assert.IsTrue(service1InstancesInChild2.Contains(namedService1InChild1));
Assert.IsTrue(service1InstancesInChild2.Contains(disposableServiceInChild2));
// Unregister local entry.
child2.Unregister(typeof(IService1));
Assert.AreSame(service1InRoot, child2.GetInstance<IService1>());
// Dispose hierarchy.
root.Dispose();
Assert.IsTrue(((DisposableService)disposableServiceInChild2).Disposed);
}
示例4: TypeShouldBeDisposed
public void TypeShouldBeDisposed()
{
var ioc = new ServiceContainer();
// DisposalPolicy.Automatic
ioc.Register(typeof(IService1), null, typeof(DisposableService));
var service = ioc.GetInstance<IService1>();
ioc.Dispose();
Assert.IsTrue(((DisposableService)service).Disposed);
}
示例5: ShouldThrowIfDisposed
public void ShouldThrowIfDisposed()
{
var ioc = new ServiceContainer();
ioc.Dispose();
ioc.Clear();
}
示例6: InstanceShouldNotBeDisposed
public void InstanceShouldNotBeDisposed()
{
var ioc = new ServiceContainer();
// DisposalPolicy.Manual
var disposableService = new DisposableService();
ioc.Register(typeof(IService1), "Token", disposableService);
ioc.Dispose();
Assert.IsFalse(disposableService.Disposed);
}
示例7: DisposalPolicies
public void DisposalPolicies()
{
var ioc = new ServiceContainer();
// Not disposable.
ioc.Register(typeof(IService1), null, new Service1());
// DisposalPolicy.Automatic
ioc.Register(typeof(IService1), null, typeof(DisposableService));
var service = ioc.GetInstance<IService1>();
// DisposalPolicy.Manual
var disposableService = new DisposableService();
ioc.Register(typeof(IService1), "Token", disposableService);
ioc.Dispose();
Assert.IsFalse(disposableService.Disposed);
Assert.IsTrue(((DisposableService)service).Disposed);
}