WCF - 如何制作自定义错误?

时间:2022-11-14 17:03:32

I've got a WCF REST project and I want to catch the errors that the WCF framework throws and display them in my way (JSON, that is).

我有一个WCF REST项目,我想捕获WCF框架抛出的错误并以我的方式显示它们(JSON,即)。

For example, If I'm expecting an int parameter in my call and I get a string, the framework would display a page with "request error" and some trace info... I'd just like to get the exception and display it in my JSON format as response to the user.

例如,如果我在调用中期待一个int参数并且我得到一个字符串,框架将显示一个带有“请求错误”的页面和一些跟踪信息...我只是想得到异常并显示它以我的JSON格式作为对用户的响应。

Just to make this more clear - I'm not looking to catch an exception IN CODE, but an exception that happens outside of the code. an exception that the WCF would generate it self, such as (when I passed a string into an int field)

只是为了让它更清楚 - 我不是要在代码中捕获异常,而是在代码之外发生的异常。 WCF会自行生成它的例外,例如(当我将字符串传递给int字段时)

The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service. The exception message is 'Input string was not in a correct format.'. See server logs for more details. The exception stack trace is:...

服务器遇到处理请求的错误。请参阅服务帮助页面以构建对服务的有效请求。异常消息是“输入字符串格式不正确”。请参阅服务器日志以获取更多详异常堆栈跟踪是:...

Any ideas how this can be done?

有什么想法可以做到这一点?

Many thanks in advance!

提前谢谢了!

2 个解决方案

#1


1  

You need to use FaultContracts to generate your own custom errors. Takes a bit of time to setup but once you have it, it works wonders. The good thing about it is that the FaultContract is completely serializable.

您需要使用FaultContracts来生成自己的自定义错误。需要一些时间来设置,但是一旦你拥有它,它就会产生奇迹。关于它的好处是FaultContract是完全可序列化的。

[OperationContract]
[FaultContract(typeof(CustomFault))]
string WillThrowArgumentException();

Here is a really good explanation of getting it to work.
http://blog.ngommans.ca/index.php?/archives/33-Handling-custom-errors-in-WCF.html

这是一个非常好的解释,让它工作。 http://blog.ngommans.ca/index.php?/archives/33-Handling-custom-errors-in-WCF.html

#2


0  

It sounds like you're looking to implement global error handling in the WCF service, so you can catch unhandled exceptions (either an unanticipated error in your code or something outside your control). If my understanding is correct, you'll want to look into the IErrorHandler Interface.

听起来您正在寻找在WCF服务中实现全局错误处理,因此您可以捕获未处理的异常(代码中的意外错误或控件之外的异常)。如果我的理解是正确的,您将需要查看IErrorHandler接口。

There's a number of blog posts and articles on how to impement IErrorHandler. Here's one that covers both SOAP and REST: WCF Exception Handling with IErrorHandler.

有很多关于如何强制IErrorHandler的博客文章和文章。这里介绍了SOAP和REST:使用IErrorHandler进行WCF异常处理。

#1


1  

You need to use FaultContracts to generate your own custom errors. Takes a bit of time to setup but once you have it, it works wonders. The good thing about it is that the FaultContract is completely serializable.

您需要使用FaultContracts来生成自己的自定义错误。需要一些时间来设置,但是一旦你拥有它,它就会产生奇迹。关于它的好处是FaultContract是完全可序列化的。

[OperationContract]
[FaultContract(typeof(CustomFault))]
string WillThrowArgumentException();

Here is a really good explanation of getting it to work.
http://blog.ngommans.ca/index.php?/archives/33-Handling-custom-errors-in-WCF.html

这是一个非常好的解释,让它工作。 http://blog.ngommans.ca/index.php?/archives/33-Handling-custom-errors-in-WCF.html

#2


0  

It sounds like you're looking to implement global error handling in the WCF service, so you can catch unhandled exceptions (either an unanticipated error in your code or something outside your control). If my understanding is correct, you'll want to look into the IErrorHandler Interface.

听起来您正在寻找在WCF服务中实现全局错误处理,因此您可以捕获未处理的异常(代码中的意外错误或控件之外的异常)。如果我的理解是正确的,您将需要查看IErrorHandler接口。

There's a number of blog posts and articles on how to impement IErrorHandler. Here's one that covers both SOAP and REST: WCF Exception Handling with IErrorHandler.

有很多关于如何强制IErrorHandler的博客文章和文章。这里介绍了SOAP和REST:使用IErrorHandler进行WCF异常处理。