无法使IDispatchMessageInspector正常工作



我需要在REST、Json、WCF web服务中使用请求头。我创建我的IDispatchMessageInspector

public class HeaderInspector : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    {
        int ind = request.Headers.FindHeader("xxxxx", "");
        return null;
    }
    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
    }
}

然后是将检查器连接到端点的端点行为:

public class HeaderInspectorBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        HeaderInspector headerinsp = new HeaderInspector();
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new HeaderInspector());
    }
    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

最后是一个行为扩展元素:

public class MyExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(HeaderInspectorBehavior); }
    }
    protected override object CreateBehavior()
    {
        return new HeaderInspectorBehavior();
    }
}

那些类位于同一文件/命名空间PDM.WebService中我的配置是:

<behaviors>
  <endpointBehaviors>
    <behavior name="RestBehavior">
      <HeaderInspectorBehavior/>
      <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" faultExceptionEnabled="true" automaticFormatSelectionEnabled="false" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add name="HeaderInspectorBehavior" type="PDM.WebService.MyExtension, PDM.WebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>

一切都编译得很好,在执行时我可以捕捉到方法"public override Type BehaviorType"的执行,但在那之后,没有其他代码方法被激发(我发送了请求,得到了响应,服务响应良好)。我在每个方法中都设置了调试点,其他方法都不执行!(尤其是ApplyDispatchBehavior)。有人能指出我遗漏了什么吗?

Refer CreateBehavior()不会针对类似问题调用。

确保您的服务元素的名称与mynamespace.myservicename 相对应

即使您没有正确的服务名称,该服务也会提供正确的响应;但是CCD_ 2只有在您具有服务元素的正确名称时才会被调用。

示例

<service
      name="WcfServiceApp001.Service1"
      behaviorConfiguration="InternalPayrollBehavior">
<endpoint address="" binding="basicHttpBinding"
          behaviorConfiguration="EndpointBehavior"
          contract="WcfServiceApp001.IService1"
          />
</service>

相关内容

  • 没有找到相关文章

最新更新