为什么 Enterprise Library 在 WCF 中不抛出FaultException

发布于 2024-12-28 02:33:28 字数 2057 浏览 2 评论 0原文

我使用 IIS 上托管的 WCF 服务。
另外,我使用错误契约将错误发送到客户端 (FaultException<'T>)
在客户端,我捕获 FaultException<'T> 向用户显示一些错误。
如果服务器上抛出一些未处理的异常,WCF 会生成普通的 FaultException, 因此客户端捕获它并向用户显示“内部服务器错误”消息。

然后我开始使用Enterprise Library 5.0进行WCF异常屏蔽,并遇到以下问题。使用FaultContractExceptionHandler可以将所有错误传递给客户端,但如果出现未处理的异常,客户端会收到CommunicationException而不是FaultException。其他一切都正常。

如果我关闭异常处理策略(删除 ExceptionShielding 属性),客户端将按预期捕获 FaultExceptions
你知道可能是什么原因吗?
谢谢。

PS:这是一系列内部异常:
通讯异常 -> WebException-> IOException->套接字异常
消息是“底层连接已关闭:服务器关闭了预期保持活动状态的连接。”

Web.config

<add name="ServiceException Policy">
    <exceptionTypes>
      <add name="ServiceLogicException" type="LopService.Contracts.ServiceLogicException, LopService.Contracts, Version=1.0.0.1725, Culture=neutral, PublicKeyToken=null"
        postHandlingAction="ThrowNewException">
        <exceptionHandlers>
          <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            faultContractType="LopService.Contracts.ServiceLogicFaultContract, LopService.Contracts, Version=1.0.0.1725, Culture=neutral, PublicKeyToken=null"
            name="Fault Contract Exception Handler" />
        </exceptionHandlers>
      </add>
    </exceptionTypes>
  </add>

服务类

[ExceptionShielding("ServiceException Policy")]
public partial class LopSoapService
{...

操作契约

    [OperationContract]
    [FaultContract(typeof(DataValidationFaultContract))]
    [FaultContract(typeof(ServiceLogicFaultContract))]
    int CreateUser(UserDto userDto, IEnumerable<ClaimRecordDto> claimRecords);

I use WCF service hosted on IIS.
Also, I use fault contracts to send errors to the client (FaultException<'T>)
On the client side I catch FaultException<'T> to show some error to user.
If some unhandled exception being thrown on a server, WCF generates plain FaultException,
so client catches it and shows user "Internal Server Error" message.

Then I began to use Enterprise Library 5.0 for WCF Exception Shielding, and met following issue. Using FaultContractExceptionHandler it is possible to carry all errors to the client, but in case of unhandled exception client receives CommunicationException instead of FaultException. Everything other works ok.

If I turned off exception handling policy (remove ExceptionShielding attibute), client catches FaultExceptions as expected.
Do you know what may be the reason?
Thanks.

PS: Here is a chain of inner exceptions:
CommunicationException -> WebException -> IOException -> SocketException
Message is "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server."

Web.config

<add name="ServiceException Policy">
    <exceptionTypes>
      <add name="ServiceLogicException" type="LopService.Contracts.ServiceLogicException, LopService.Contracts, Version=1.0.0.1725, Culture=neutral, PublicKeyToken=null"
        postHandlingAction="ThrowNewException">
        <exceptionHandlers>
          <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            faultContractType="LopService.Contracts.ServiceLogicFaultContract, LopService.Contracts, Version=1.0.0.1725, Culture=neutral, PublicKeyToken=null"
            name="Fault Contract Exception Handler" />
        </exceptionHandlers>
      </add>
    </exceptionTypes>
  </add>

Service Class

[ExceptionShielding("ServiceException Policy")]
public partial class LopSoapService
{...

Operation Contract

    [OperationContract]
    [FaultContract(typeof(DataValidationFaultContract))]
    [FaultContract(typeof(ServiceLogicFaultContract))]
    int CreateUser(UserDto userDto, IEnumerable<ClaimRecordDto> claimRecords);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2025-01-04 02:33:28

这似乎是企业库的问题。请参阅此工作项

This appears to be an issue with Enterprise Library. See this workitem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文