[英]Unit test Service and Mock Autogenerated WCF Client proxy
我在服务中引用自动生成的WCF客户端。
//Autogenerated Service client
public partial class ServiceClient :
System.ServiceModel.ClientBase<IService>, IService
{
//...
}
//Autogenerated Interface Client
public interface IService {
//...
}
以下列方式:
public MyService{
public IExternalWsClientFactory ExternalWsClientFactory {get; set; }
public void MyMethod(){
using (var wsCliente = ExternalWsClientFactory.ServiceClient())
{
//...
}
}
}
public class ExternalWsClientFactory : IExternalWsClientFactory
{
public ServiceClient ServiceClient()
{
var wsClient = new ServiceClient();
return wsClient;
}
}
我引用了实现,因为我想使用using
语句来处理代码块末尾的资源。 并且因为IDisposable
在ClientBase
下,并且界面不是部分的。
我的问题是我想模拟ServiceClient
(我已经模拟了外部WsClientFactory
)但是因为我使用了实现,所以我很难做到这一点。
注意:实现中自动生成的方法ServiceClient
不是virtual
。
这个班是偏袒的。 界面不是。
创建自己的接口,该接口派生自原始接口,并使用IDisposable
扩展。
public interface IServiceClient: ICommunicationObject, IService, IDisposable { }
使用自定义界面扩展分部类
public partial class ServiceClient : IServiceClient { }
现在你应该可以使用扩展接口和using
语句了
public class ExternalWsClientFactory : IExternalWsClientFactory {
public IServiceClient ServiceClient() {
var wsClient = new ServiceClient();
return wsClient;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.