使用最小起订量模拟 ChannelFactory 时出现 TargetInvocationException
TargetInvocationException on mocking ChannelFactory with moq
为什么在模拟 ChannelFactory<IService>
时抛出 TargetInvocationException
?
public interface IService
{
void Do();
}
public class Service : IService
{
private ChannelFactory<IRemoteService> factory;
public Service(ChannelFactory<IRemoteService> factory)
{
this.factory = factory;
}
public void Do()
{
using (var remote = factory.CreateChannel())
{
remote.DoRemote();
}
}
}
[TestFixture]
public class ChannelFactoryMoqTest
{
private IService service;
private Mock<ChannelFactory<IRemoteService>> factory;
[Test]
public void Test()
{
this.factory = new Mock<ChannelFactory<IRemoteService>>();
this.service = new Service(this.factory.Object);
// Calling Object on this.factory throws TargetInvocationException
}
}
我想使用 ChannelFactory
依赖项而不是简单的 IRemoteService
因为在我看来,就并发性而言,每次调用创建 Service
个实例更安全。
这是异常堆栈跟踪:
System.Reflection.TargetInvocationException was unhandled by user code
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
at Moq.Mock`1.<InitializeInstance>b__2()
at Moq.PexProtector.Invoke(Action action)
at Moq.Mock`1.InitializeInstance()
at Moq.Mock`1.OnGetObject()
at Moq.Mock.GetObject()
at Moq.Mock.get_Object()
at Moq.Mock`1.get_Object()
at TR.Eikon.Services.AppVersion.Tests.Workflows.ChannelFactoryMoqTest.Test() in d:\Temp.cs:line 61
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=System.ServiceModel
StackTrace:
at System.ServiceModel.ChannelFactory.EnsureSecurityCredentialsManager(ServiceEndpoint endpoint)
at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
at System.ServiceModel.ChannelFactory`1..ctor()
at Castle.Proxies.ChannelFactory`1Proxy..ctor(IInterceptor[] )
InnerException:
您收到该错误是因为 ChannelFactory
正在尝试为与其关联的不存在的端点创建实际代理。
我的建议是 hide/isolate 来自您控制的界面后面的客户端的 ChannelFactory<IRemoteService>
,这样您可以更好地管理您想要的 API。这样你就可以替换工厂,为客户端提供一个创建模拟而不是真实代理的客户端。
public interface IMyChannelFactory<TChannel> {
TChannel CreateChannel();
}
...稍后可以包装 ChannelFactory<IRemoteService>
...
的实际实例
public class ChannelFactoryWrapper<TChannel> : IMyChannelFactory<TChannel> {
private ChannelFactory<TChannel> factory;
public ChannelFactoryWrapper(ChannelFactory<TChannel> factory) {
this.factory = factory;
}
public TChannel CreateChannel() {
return factory.CreateChannel();
}
}
您的重构服务 class 将引用您的新 mockable 接口...
public class Service : IService {
private IMyChannelFactory<IRemoteService> factory;
public Service(IMyChannelFactory<IRemoteService> factory) {
this.factory = factory;
}
public void Do() {
using (var remote = factory.CreateChannel()) {
remote.DoRemote();
}
}
}
...然后可以相应地重构和测试您的测试...
[Test]
public void Should_Mock_ChannelFactory() {
//Arrange
var remoteService = new Mock<IRemoteService>();
remoteService.Setup(m => m.DoRemote()).Verifiable();
var factory = new Mock<IMyChannelFactory<IRemoteService>>();
factory.Setup(f => f.CreateChannel()).Returns(remoteService.Object).Verifiable();
var service = new Service(factory.Object);
//Act
service.Do();
//Assert
remoteService.Verify();
factory.Verify();
}
为什么在模拟 ChannelFactory<IService>
时抛出 TargetInvocationException
?
public interface IService
{
void Do();
}
public class Service : IService
{
private ChannelFactory<IRemoteService> factory;
public Service(ChannelFactory<IRemoteService> factory)
{
this.factory = factory;
}
public void Do()
{
using (var remote = factory.CreateChannel())
{
remote.DoRemote();
}
}
}
[TestFixture]
public class ChannelFactoryMoqTest
{
private IService service;
private Mock<ChannelFactory<IRemoteService>> factory;
[Test]
public void Test()
{
this.factory = new Mock<ChannelFactory<IRemoteService>>();
this.service = new Service(this.factory.Object);
// Calling Object on this.factory throws TargetInvocationException
}
}
我想使用 ChannelFactory
依赖项而不是简单的 IRemoteService
因为在我看来,就并发性而言,每次调用创建 Service
个实例更安全。
这是异常堆栈跟踪:
System.Reflection.TargetInvocationException was unhandled by user code
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(Type proxyType, List`1 proxyArguments, Type classToProxy, Object[] constructorArguments)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
at Moq.Mock`1.<InitializeInstance>b__2()
at Moq.PexProtector.Invoke(Action action)
at Moq.Mock`1.InitializeInstance()
at Moq.Mock`1.OnGetObject()
at Moq.Mock.GetObject()
at Moq.Mock.get_Object()
at Moq.Mock`1.get_Object()
at TR.Eikon.Services.AppVersion.Tests.Workflows.ChannelFactoryMoqTest.Test() in d:\Temp.cs:line 61
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=System.ServiceModel
StackTrace:
at System.ServiceModel.ChannelFactory.EnsureSecurityCredentialsManager(ServiceEndpoint endpoint)
at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
at System.ServiceModel.ChannelFactory`1..ctor()
at Castle.Proxies.ChannelFactory`1Proxy..ctor(IInterceptor[] )
InnerException:
您收到该错误是因为 ChannelFactory
正在尝试为与其关联的不存在的端点创建实际代理。
我的建议是 hide/isolate 来自您控制的界面后面的客户端的 ChannelFactory<IRemoteService>
,这样您可以更好地管理您想要的 API。这样你就可以替换工厂,为客户端提供一个创建模拟而不是真实代理的客户端。
public interface IMyChannelFactory<TChannel> {
TChannel CreateChannel();
}
...稍后可以包装 ChannelFactory<IRemoteService>
...
public class ChannelFactoryWrapper<TChannel> : IMyChannelFactory<TChannel> {
private ChannelFactory<TChannel> factory;
public ChannelFactoryWrapper(ChannelFactory<TChannel> factory) {
this.factory = factory;
}
public TChannel CreateChannel() {
return factory.CreateChannel();
}
}
您的重构服务 class 将引用您的新 mockable 接口...
public class Service : IService {
private IMyChannelFactory<IRemoteService> factory;
public Service(IMyChannelFactory<IRemoteService> factory) {
this.factory = factory;
}
public void Do() {
using (var remote = factory.CreateChannel()) {
remote.DoRemote();
}
}
}
...然后可以相应地重构和测试您的测试...
[Test]
public void Should_Mock_ChannelFactory() {
//Arrange
var remoteService = new Mock<IRemoteService>();
remoteService.Setup(m => m.DoRemote()).Verifiable();
var factory = new Mock<IMyChannelFactory<IRemoteService>>();
factory.Setup(f => f.CreateChannel()).Returns(remoteService.Object).Verifiable();
var service = new Service(factory.Object);
//Act
service.Do();
//Assert
remoteService.Verify();
factory.Verify();
}