WCF 数据服务错误处理

问题描述:

我创建了一个带有服务操作的 WCF 数据服务.

I have created a WCF data service with a service operation.

我想生成一种业务异常.我尝试生成 WebFaultException 但当服务操作抛出此错误时,我看不到如何在客户端捕获此错误.

I want to generate a kind of Business exception. I try to generate WebFaultException but I don't see how to catch this error at the client side when this error is throwing by a service operation.

这里是我模拟异常的服务操作:

Here is my service operation to simulate an exception:

[WebGet] 
public void GenerateException() 
{
    throw new DataServiceException( 403, "Custom Message" );
}

这是我的客户:

WebClient wc = new WebClient(); 
wc.DownloadString(
    new Uri(
      "http://localhost:27820/WcfDataService1.svc/GenerateException"
    )
);

DownloadString 抛出异常,但只是内部服务器错误,我看不到我的自定义消息.

DownloadString is throwing an exception, but it's only Internal Server Error, I can't see my Custom Message.

有什么想法吗?

非常感谢.

您可以使用 WCF FaultContract 属性来抛出和处理业务异常.

You can use WCF FaultContract Attribute to throw and handle a businzess exception.

请参阅链接示例