我正在重构有5个不同的网络服务项目,并且每个Web服务有一吨的相同的代码,包括添加消息检查到客户端的终结点行为等等我们可以看到请求和响应数据。重构的C# - WCF消息检查不运行
一部分是来为Web服务更清洁的模型(即做了所有的常见的设置,包括增加的消息督察例如一个抽象的基本服务模式)。
现在,当我进行服务调用(通过反射调用)时,服务调用完全正常,如果在响应返回后立即添加断点,我可以看到有3个行为添加到客户端端点:
[0] Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior
[1] System.ServiceModel.Description.ClientCredentials
[2] MyProject.MyMessageInspector
...但消息检查器代码似乎根本不会被调用。检查员代码是目前等同于MSDN例子在这里(除了类名): https://msdn.microsoft.com/en-us/library/ms733786(v=vs.110).aspx
的主要区别是,我现在使用的通用方法设置客户端,它看起来像这样:
...sanity checks, etc...
TClient client = Activator.CreateInstance(typeof(TClient), binding, new EndpointAddress(url)) as TClient;
ClientBase<TInterface> _clientBase = client as ClientBase<TInterface>;
...credentials, timeout, etc...
MyEndpointBehavior _inspector = new MyEndpointBehavior()
_clientBase.Endpoint.Behaviors.Add(_inspector);
然后,当我打电话时,我用这个代码,位于新抽象基类(原代码就是这么做的,也是迄今唯一的区别是使用泛型):
ClientBase<TInterface> _clientBase = _client as ClientBase<TInterface>;
using (new OperationContextScope(_clientBase.InnerChannel))
{
// Get the method
MethodInfo mi = _client.GetType().GetMethod(APICall);
// Make the call and return the result if successful
object response = mi.Invoke(_client, APICallParameters);
return response;
}
任何想法,为什么这在切换到泛型方法现在的工作之前,而不是?