2017-06-07 43 views
0

我正在重構有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; 
} 

任何想法,爲什麼這在切換到泛型方法現在的工作之前,而不是?

回答

0

我不知道爲什麼這樣做有差別,但我重新排序的代碼移動督察除了客戶端創建後立即發生,所以現在的代碼如下所示:

...sanity checks, etc... 
TClient client = Activator.CreateInstance(typeof(TClient), binding, new 
EndpointAddress(url)) as TClient; 
ClientBase<TInterface> _clientBase = client as ClientBase<TInterface>; 
MyEndpointBehavior _inspector = new MyEndpointBehavior() 
_clientBase.Endpoint.Behaviors.Add(_inspector); 
...credentials, timeout, etc... 

現在檢查員似乎按預期工作。奇怪。