WCF 服务和客户端应用程序的错误处理 [英] Error Handling with WCF Service and Client Application

查看:38
本文介绍了WCF 服务和客户端应用程序的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我而言,我有一个 WCF 服务 (MyService.svc).我还有一个正在实例化和使用服务合同的客户端应用程序.

In my case, I have a WCF Service (MyService.svc). I also have a client application that is instantiating and consuming the service contract.

在服务级别处理异常并以有序和自我描述的方式将它们传输"到客户端的最佳方法是什么?

What is the best way to handle exceptions at the service level and "transmit" them over to the client in an orderly and self-describing way?

如果我在 WCF 服务上有一个未处理的异常,它似乎作为一个 CommunicationException 冒泡回客户端应用程序.

If I have an unhandled exception on the WCF service, it seems as though that bubbles back to the client application as a CommunicationException.

但是在服务级别抛出异常并将相同的异常传输到客户端级别的最佳方法是什么?或者,如果我不在服务级别处理异常(或者只是在服务级别重新抛出它),如何将其明确定向到客户端?

But what's the best way to throw an exception at the service-level and have that same exception transmitted to the client-level? Or if I don't handle an exception at the service-level (or just re throw it at the service-level) how can that get explicitly directed to the client?

或者这不是 SOA 通常的工作方式吗?这里的正确方法"是什么?

Or is that not typically how this SOA would work? What's the "right way" here?

谢谢!

推荐答案

首先,如果你想通过协议传递异常,你必须将它包装在一个错误异常中,否则你会得到一个服务器错误.

First, if you want to pass the exception over the protocol, you have to wrap it in a faultexception, otherwise you will get a server error.

在方法上使用 FaultContract 属性来启用 faultContract 并定义要使用创建消息协定传递的消息:

Use the FaultContract attribute over methods to enable faultContract and define the message you want to pass using creating a Message contract:

public interface IMyService
{
    [OperationContract]
    [FaultContract(typeof(Message))]
    void WCFOperation();
}

[DataContract(Namespace = "http://www.mycompany.pt/myservice")]
public class Message
{
    String _code;
    [DataMember]
    public String Code
    {
        get { return _code; }
        set { _code = value; }
    }

    String _text;
    [DataMember]
    public String Text
    {
        get { return _text; }
        set { _text = value; }
    }
}

要将异常转换为 FaultExceptions,我使用以下帮助程序:

To convert exceptions to FaultExceptions, i use the following helper:

    class Helper
    {

      internal static System.ServiceModel.FaultException<Message> ConvertToSoapFault(MyException ex)
      {
          FaultCode fc = new FaultCode(ex.Code);
          return new FaultException<Message>(new Message(){ Text= ex.Message, Code= ex.Code});
      }

      internal static System.ServiceModel.FaultException ConvertToSoapFault(Exception ex)
      {            
          return new FaultException(ex.Message);
      }
    }

最后,在 operationContract 的实现上,简单的这样做:

Finally, at the operationContract implementation, simple do this:

    public void WCFOperation()
    {
        try
        {
           ...
        }           
        catch (Exception ex)
        {
            Helpers.publishError(ex);
            throw Helpers.ConvertToSoapFault(ex);
        }
    }

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

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