gpt4 book ai didi

c# - 如何克服 System.ServiceModel.FaultException

转载 作者:行者123 更新时间:2023-11-30 16:56:49 24 4
gpt4 key购买 nike

在更新我的 WCF 客户端的服务引用时(只需在 Visual Studio 2008 中单击“更新服务引用”),出现以下错误:System.ServiceModel.FaultException: The 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 循环时,我完全没有收到任何错误,但这不是出路(因为我希望我的错误得到处理)。

下面是我的服务配置:

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

最佳答案

替换你的代码并尝试:

<bindings>
<wsHttpBinding>
<binding name="NoSecurityBinding" >
<security mode="None">
<transport clientCredentialType="None"/>
<message clientCredentialType="None"/>
</security>
</binding>
<binding name="DefaultBinding" />
</wsHttpBinding>
</bindings>

关于c# - 如何克服 System.ServiceModel.FaultException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27444758/

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