gpt4 book ai didi

c# - EndpointDispatcher 的 ContractFilter 不匹配(错误处理)

转载 作者:IT王子 更新时间:2023-10-29 04:13:14 28 4
gpt4 key购买 nike

更新我的 WCF 客户端的服务引用时(只需在 Visual Studio 2008 中单击更新服务引用),出现以下错误:

System.ServiceModel.FaultException: The message with Action 'http://schemas.xmlsoap.org/ws/2004/09/transfer/Get' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None). at System.ServiceModel.Dispatcher.ErrorBehavior.ThrowAndCatch(Exception e, Message message)

背景:

我已经创建了 ErrorServiceBehaviour 类。由于此类行为是为错误处理而创建的,因此必须将 IErrorHandler 实现应用于每个 ChannelDispatcher

public class ErrorServiceBehaviour : Attribute, IServiceBehavior
{
...
public Type FaultType
{
get { return _faultType; }
set { _faultType = value; }
}

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(new ErrorHandler(_faultType));
}
}
}

public class ErrorHandler : IErrorHandler
{
public ErrorHandler(Type faultType)
{
_faultType = faultType;
}
...
}

后来,我通过将 ErrorServiceBehavior 属性应用于我的服务类来使用该行为:

[ErrorServiceBehavior(FaultType = typeof(MyServiceFault))] 
public class MyService : IMyService
{
...
}

问题是,当我在 ApplyDispatchBehavior 方法中注释掉 foreach 循环时,我根本没有得到no 错误,但事实并非如此出路(因为我希望我的错误得到处理)。

下面是我的服务配置:

<system.serviceModel>
<services>
<service behaviorConfiguration="DefaultBehavior" name="MyService">
<endpoint address="" binding="wsHttpBinding" contract="IMyService" bindingConfiguration="NoSecurityBinding"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="NoSecurityBinding" >
<security mode="None">
<transport clientCredentialType="None"/>
<message establishSecurityContext="false"/>
</security>
</binding>
<binding name="DefaultBinding" />
</wsHttpBinding>
</bindings>
</system.serviceModel>

有人可以帮助我吗?

更新

前面显示的代码:

foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(new ErrorHandler(_faultType));
}

为所有端点添加自定义错误处理 - 包括元数据端点。但实际上这不是问题的根源 - 即使我禁用为元数据端点添加错误处理,问题仍然存在。

另一个注意事项是,当我将第一个端点的 bindingConfiguration 更改为 DefaultBinding 时,我完全没有错误:

<services>
<service behaviorConfiguration="DefaultBehavior" name="MyService">
<endpoint address="" binding="wsHttpBinding" contract="IMyService" bindingConfiguration="DefaultBinding"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>

这样的选项也不是我想要的 - 我仍然需要有问题的 NoSecurityBinding 才能工作。

提前致谢。

最佳答案

查看 IExtensibleDataObject ,它用于处理仍然能够相互通信的不同版本的 Web 服务。这样契约(Contract)就不需要完全匹配。希望这会有所帮助。

关于c# - EndpointDispatcher 的 ContractFilter 不匹配(错误处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3458934/

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