gpt4 book ai didi

wcf - 无法使 IDispatchMessageInspector 工作

转载 作者:行者123 更新时间:2023-12-03 06:20:37 25 4
gpt4 key购买 nike

我需要在 REST、Json、WCF Web 服务中使用请求 header 。我创建了 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)
{
}
}

最后是BehaviorExtensionElement:

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

这些类位于同一文件/ namespace 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 BehaviourType”的执行,但此后代码中没有其他方法被触发(我发送了请求并得到了响应,服务响应良好)。我在每个方法中设置了调试点,没有其他执行! (特别是ApplyDispatchBehavior)。有人可以指出我缺少什么吗?

最佳答案

引用CreateBehavior() is not invoked对于类似的问题。

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

即使您没有正确的服务名称,该服务也会提供正确的响应;但只有当您拥有正确的服务元素名称时,才会调用 CreateBehavior()

示例

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

关于wcf - 无法使 IDispatchMessageInspector 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12199373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com