Web服务异常 [英] Exceptions in Web Services

查看:136
本文介绍了Web服务异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小组正在开发基于服务的(.NET WCF)应用程序,我们正在努力决定如何处理我们内部服务中的异常。我们应该抛出异常吗?返回异常序列化为XML?只需返回错误代码?

My group is developing a service-based (.NET WCF) application and we're trying to decide how to handle exceptions in our internal services. Should we throw exceptions? Return exceptions serialized as XML? Just return an error code?

请记住,用户永远不会看到这些异常,它只适用于应用程序的其他部分。

Keep in mind that the user will never see these exceptions, it's only for other parts of the application.

推荐答案

WCF使用SoapFaults作为从服务到客户端或客户端向服务发送异常的本地方式。

WCF uses SoapFaults as its native way of transmitting exceptions from either the service to the client, or the client to the service.

您可以使用合同界面中的FaultContract属性声明自定义SOAP错误:

You can declare a custom SOAP fault using the FaultContract attribute in your contract interface:

例如:

[ServiceContract(Namespace="foobar")]
interface IContract
{
    [OperationContract]
    [FaultContract(typeof(CustomFault))]
    void DoSomething();
}


[DataContract(Namespace="Foobar")]
class CustomFault
{
    [DataMember]
    public string error;

    public CustomFault(string err)
    {
        error = err;
    }
}

class myService : IContract
{
    public void DoSomething()
    {
        throw new FaultException<CustomFault>( new CustomFault("Custom Exception!"));
    }
}

这篇关于Web服务异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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