【问题标题】:C# - WCF Message Inspector Doesn't RunC# - WCF 消息检查器不运行
【发布时间】:2017-06-07 22:11:17
【问题描述】:

我正在重构一个包含大约 5 个不同 Web 服务的项目,每个 Web 服务都有大量相同的代码,包括在客户端端点行为中添加一个消息检查器,以便我们可以看到请求和响应数据。

重构的一部分是为 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;
}

任何想法为什么这在切换到通用方法之前有效而不是现在?

【问题讨论】:

    标签: c# .net wcf generics


    【解决方案1】:

    我不确定为什么这会有所不同,但我重新订购了代码以在客户端创建后立即移动检查器添加,因此代码现在看起来像这样:

    ...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...
    

    检查器现在似乎按预期工作。奇怪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多