WCF 数据服务错误处理 [英] WCF Data Service Error Handling

查看:30
本文介绍了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.

请参阅链接示例

这篇关于WCF 数据服务错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆