抛出泛型FaultException [英] Throwing generic FaultException

查看:187
本文介绍了抛出泛型FaultException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务,如果出现问题,会抛出一个通用的FaultException异常。
我不知道为什么,有时客户端将捕获一个非泛型的FaultException,而不是一般异常。



有人知道,问题是?

解决方案

您的服务需要处理所有异常并将其包装到FaultException< T>其中T是您编写的数据合同。所以第一步是定义一个包含你的异常信息的自定义数据合同:

  [DataContract] 
public class CustomFault
{
public string Message {get;组; }
}

然后,您指示您的服务合同,其方法可能会引发FaultException< CustomFault> ;。这允许服务在wsdl中公开CustomFault类,以便客户端可以生成代理类:

  [ServiceContract] 
public interface IMyServiceContract
{
[FaultContract(typeof(CustomFault))]
void MyMethod(string someArgument);
}

下一步是实现此界面:

  public class MyService:IMyServiceContract 
{
public void MyMethod(string someArgument)
{
//做某事这可能会抛出异常但是还没有抓住
}
}

要处理例外,您可以实施 IErrorHandler ,这将会当您的某个服务方法抛出异常时使用它。此错误处理程序的目的是将异常包装到FaultException< CustomFault>中:

  public class MyErrorHandler:IErrorHandler 
{
public bool HandleError(异常错误)
{
return true;
}

public void ProvideFault(异常错误,MessageVersion版本,参考消息msg)
{
var customFault = new CustomFault()
{
Message = error.Message,
};
var fe = new FaultException< CustomFault>(customFault);
MessageFault fault = fe.CreateMessageFault();
string ns =http:// somenamespace / whatever;
msg = Message.CreateMessage(version,fault,ns);
}
}

一旦你注册了你的错误处理程序,在客户端您将永远得到FaultException< CustomFault>。


I have a WCF service that if something goes wrong throws a generic FaultException. I don't know why, sometimes the clients will catch a non-generic FaultException instead of the generic exception.

Does anybody know, what the problem is?

解决方案

Your service needs to handle all exceptions and wrap them into FaultException<T> where T is a data contract you have written. So the first step is to define a custom data contract that will contain your exception information:

[DataContract]
public class CustomFault
{
    public string Message { get; set; }
}

Then you instruct your service contract that its methods could potentially throw FaultException<CustomFault>. This allows the service to expose the CustomFault class in the wsdl, so that clients could generate a proxy class:

[ServiceContract]
public interface IMyServiceContract
{
    [FaultContract(typeof(CustomFault))]
    void MyMethod(string someArgument);
}

Next step is to implement this interface:

public class MyService : IMyServiceContract
{
    public void MyMethod(string someArgument)
    {
        // Do something here that could throw exceptions but don't catch yet
    }
}

To handle exceptions you could implement IErrorHandler which will be used whenever one of your service methods throws an exception. The purpose of this error handler is to wrap the exception into FaultException<CustomFault>:

public class MyErrorHandler : IErrorHandler
{
    public bool HandleError(Exception error)
    {
        return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
    {
        var customFault = new CustomFault()
        {
            Message = error.Message,
        };
        var fe = new FaultException<CustomFault>(customFault);
        MessageFault fault = fe.CreateMessageFault();
        string ns = "http://somenamespace/whatever";
        msg = Message.CreateMessage(version, fault, ns);
    }
}

Once you have registered your error handler, on the client side, you will always get FaultException<CustomFault>.

这篇关于抛出泛型FaultException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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