本文整理汇总了C#中ServiceContainer.Intercept方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.Intercept方法的具体用法?C# ServiceContainer.Intercept怎么用?C# ServiceContainer.Intercept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceContainer
的用法示例。
在下文中一共展示了ServiceContainer.Intercept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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));
}
示例2: ShouldInterceptDisposeFromBaseClass
public void ShouldInterceptDisposeFromBaseClass()
{
var container = new ServiceContainer();
container.Register<MyClass>();
container.Intercept(
sr => sr.ServiceType == typeof(MyClass),
(factory, definition) => DefineProxyType(definition, new SampleInterceptor()));
var test = container.GetInstance<MyClass>();
test.Dispose();
}
示例3: Main
static void Main(string[] args)
{
var container = new ServiceContainer();
container.Register<Foo>();
container.Intercept(sr => sr.ServiceType == typeof(Foo), factory => new SampleInterceptor());
var foo = container.GetInstance<Foo>();
foo.A();
}
示例4: InitializeContainer
//not best practice, just showing how all of the magic happens.
static void InitializeContainer()
{
container = new ServiceContainer();
container.RegisterAssembly(typeof(Services.Installer).Assembly, () => { return new PerContainerLifetime(); },
(serviceType, implementingType) => serviceType.Namespace == typeof(Services.Installer).Namespace + ".Interfaces");
//register the interceptors
container.Register<ExceptionInterceptor>();
container.Register<TimingInterceptor>();
//register by hand
//container.Register<IConfigurationService, ConfigurationService>();
//Wire up a Timing and Exception Interceptor
container.Intercept(sr => sr.ServiceType == typeof(IExampleService), sf => sf.GetInstance<ExceptionInterceptor>());
container.Intercept(sr => sr.ServiceType == typeof(IExampleService), sf => sf.GetInstance<TimingInterceptor>());
}
示例5: Intercept_Service_ReturnsProxyInstance
public void Intercept_Service_ReturnsProxyInstance()
{
var container = new ServiceContainer();
container.Register<IFoo, Foo>();
container.Intercept(sr => sr.ServiceType == typeof(IFoo), factory => new SampleInterceptor());
var instance = container.GetInstance<IFoo>();
Assert.IsInstanceOfType(instance, typeof(IProxy));
}
示例6: Intercept_ServiceUsingContainerResolvedInterceptor_ReturnsProxyInstance
public void Intercept_ServiceUsingContainerResolvedInterceptor_ReturnsProxyInstance()
{
var container = new ServiceContainer();
container.Register<IFoo, Foo>();
container.Register<IInterceptor, SampleInterceptor>();
container.Intercept(sr => sr.ServiceType == typeof(IFoo), factory => factory.GetInstance<IInterceptor>());
var instance = container.GetInstance<IFoo>();
Assert.IsAssignableFrom<IProxy>(instance);
}
示例7: Intercept_Service_PassesInvocationInfoToInterceptor
public void Intercept_Service_PassesInvocationInfoToInterceptor()
{
var interceptorMock = new Mock<IInterceptor>();
var targetMock = new Mock<IMethodWithNoParameters>();
var container = new ServiceContainer();
container.RegisterInstance(targetMock.Object);
container.Intercept(sr => sr.ServiceType == typeof(IMethodWithNoParameters), (factory, definition) => definition.Implement(() => interceptorMock.Object));
var instance = container.GetInstance<IMethodWithNoParameters>();
instance.Execute();
interceptorMock.Verify(i => i.Invoke(It.IsAny<IInvocationInfo>()), Times.Once());
}
示例8: Intercept_ServiceUsingContainerResolvedInterceptor_InvokesInterceptor
public void Intercept_ServiceUsingContainerResolvedInterceptor_InvokesInterceptor()
{
var interceptorMock = new Mock<IInterceptor>();
var targetMock = new Mock<IMethodWithNoParameters>();
var container = new ServiceContainer();
container.RegisterInstance(targetMock.Object);
container.RegisterInstance(interceptorMock.Object);
container.Intercept(sr => sr.ServiceType == typeof(IMethodWithNoParameters), factory => factory.GetInstance<IInterceptor>());
var instance = container.GetInstance<IMethodWithNoParameters>();
instance.Execute();
interceptorMock.Verify(i => i.Invoke(It.IsAny<IInvocationInfo>()), Times.Once());
}
示例9: Intercept_Method_InvokesInterceptorOnlyForMatchingMethods
public void Intercept_Method_InvokesInterceptorOnlyForMatchingMethods()
{
var container = new ServiceContainer();
var targetMock = new Mock<IClassWithTwoMethods>();
container.RegisterInstance(targetMock.Object);
var interceptedMethods = new List<string>();
container.Intercept(m => m.Name == "FirstMethod",
info =>
{
interceptedMethods.Add(info.Method.Name);
return null;
});
var instance = container.GetInstance<IClassWithTwoMethods>();
instance.FirstMethod();
Assert.AreEqual(1, interceptedMethods.Count);
Assert.AreEqual(interceptedMethods[0], "FirstMethod");
}
示例10: GetInstance_WithNestedNewStatement_ReturnsProxy
public void GetInstance_WithNestedNewStatement_ReturnsProxy()
{
var container = new ServiceContainer();
container.Register<ClassWithConstructor>(factory => new ClassWithConstructor(new string(new char[] {'a'})));
container.Intercept(sr => sr.ServiceType == typeof(ClassWithConstructor), info => new SampleInterceptor());
var instance = container.GetInstance<ClassWithConstructor>();
Assert.IsInstanceOfType(instance, typeof(IProxy));
}
示例11: GetInstance_InterceptedClassRegisteredAsInstance_ThrowsException
public void GetInstance_InterceptedClassRegisteredAsInstance_ThrowsException()
{
var container = new ServiceContainer();
container.RegisterInstance(new ClassWithVirtualMethod());
container.Intercept(sr => sr.ServiceType == typeof(ClassWithVirtualMethod), factory => new SampleInterceptor());
container.GetInstance<ClassWithVirtualMethod>();
}
示例12: GetInstance_InterceptedClassWithUndeterminableImplementingType_ThrowsException
public void GetInstance_InterceptedClassWithUndeterminableImplementingType_ThrowsException()
{
var container = new ServiceContainer();
container.Register(factory => CreateClassWithVirtualMethod());
container.RegisterInstance("SomeValue");
container.Intercept(sr => sr.ServiceType == typeof(ClassWithVirtualMethod), factory => new SampleInterceptor());
container.GetInstance<ClassWithVirtualMethod>();
}
示例13: GetInstance_InterceptedClass_ReturnsClassProxy
public void GetInstance_InterceptedClass_ReturnsClassProxy()
{
var container = new ServiceContainer();
container.Register<ClassWithVirtualMethod>();
container.Intercept(sr => sr.ServiceType == typeof(ClassWithVirtualMethod), factory => new SampleInterceptor());
var instance = container.GetInstance<ClassWithVirtualMethod>();
var test = instance.Execute();
Console.WriteLine(test);
Assert.IsAssignableFrom<IProxy>(instance);
}
示例14: GetInstance_InterceptedClassUsingObjectInitializer_SetsProperties
public void GetInstance_InterceptedClassUsingObjectInitializer_SetsProperties()
{
var container = new ServiceContainer();
container.Register(factory => new ClassWithPropertyAndVirtualMethod{Value = "SomeValue"});
container.Intercept(sr => sr.ServiceType == typeof(ClassWithPropertyAndVirtualMethod), factory => null);
var instance = container.GetInstance<ClassWithPropertyAndVirtualMethod>();
Assert.IsInstanceOfType(instance, typeof(IProxy));
}
示例15: GetInstance_InterceptedClassRegisteredUsingInstanceFactory_ReturnsClassProxy
public void GetInstance_InterceptedClassRegisteredUsingInstanceFactory_ReturnsClassProxy()
{
var container = new ServiceContainer();
container.Register(factory => new ClassWithConstructor("SomeValue"));
container.Intercept(sr => sr.ServiceType == typeof(ClassWithConstructor), factory => null);
var instance = container.GetInstance<ClassWithConstructor>();
Assert.IsInstanceOfType(instance, typeof(IProxy));
}