C# 如何从WCF服务引用对通用服务代理进行单元测试
我希望使用供应商的SOAP API,并通过我们自己的统一WCF实现将其公开。因为我无法控制源服务接口,所以我不得不使用服务引用和代码gen客户端类 这些客户机类别之一的示例C# 如何从WCF服务引用对通用服务代理进行单元测试,c#,wcf,generics,proxy,C#,Wcf,Generics,Proxy,我希望使用供应商的SOAP API,并通过我们自己的统一WCF实现将其公开。因为我无法控制源服务接口,所以我不得不使用服务引用和代码gen客户端类 这些客户机类别之一的示例 public partial class CompanyApiSoapClient : System.ServiceModel.ClientBase<Cwx.Repository.ConnectWise.CompanyService.CompanyApiSoap>, Cwx.Repository.ConnectW
public partial class CompanyApiSoapClient : System.ServiceModel.ClientBase<Cwx.Repository.ConnectWise.CompanyService.CompanyApiSoap>, Cwx.Repository.ConnectWise.CompanyService.CompanyApiSoap
public部分类CompanyApiSoapClient:System.ServiceModel.ClientBase,Cwx.Repository.ConnectWise.CompanyService.CompanyApiSoap
为了一般地处理对不同服务的调用,我采用了以下一般代理方法:
public class ServiceClientProxy<TChannel> : IServiceClientProxy<TChannel> where TChannel : ICommunicationObject
{
private readonly TChannel _innerChannel;
public ServiceClientProxy(TChannel innerChannel)
{
_innerChannel = innerChannel;
}
public TResult Execute<TResult>(Func<TChannel, TResult> operation)
{
var result = default(TResult);
try
{
if (_innerChannel.State != CommunicationState.Opened)
{
_innerChannel.Open();
}
result = operation(_innerChannel);
}
catch (CommunicationException)
{
_innerChannel.Abort();
}
catch (TimeoutException)
{
_innerChannel.Abort();
}
catch (Exception)
{
_innerChannel.Abort();
throw;
}
return result;
}
public void Dispose()
{
_innerChannel.Close();
}
}
公共类ServiceClientProxy:IServiceClientProxy,其中TChannel:ICommunicationObject
{
专用只读TChannel\u innerChannel;
公共服务客户端代理(TChannel innerChannel)
{
_innerChannel=innerChannel;
}
公共TResult执行(Func操作)
{
var结果=默认值(TResult);
尝试
{
如果(_innerChannel.State!=CommunicationState.Opened)
{
_innerChannel.Open();
}
结果=操作(_innerChannel);
}
捕获(通信异常)
{
_innerChannel.Abort();
}
捕获(超时异常)
{
_innerChannel.Abort();
}
捕获(例外)
{
_innerChannel.Abort();
投掷;
}
返回结果;
}
公共空间处置()
{
_innerChannel.Close();
}
}
这就是我的问题所在。为了让我的proxy generic正常工作,TChannel需要实现ICommunicationObject。我并没有从CompanyApiSoap接口中得到这个实现,而是具体的ClientBase通用实现。当然,如果我想通过IoC容器对服务代理泛型的使用进行单元测试,我不能将泛型类型约束为具体类型