简体   繁体   English

WCF 服务和客户端应用程序的错误处理

[英]Error Handling with WCF Service and Client Application

In my case, I have a WCF Service (MyService.svc).就我而言,我有一个 WCF 服务 (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?在服务级别处理异常并以有序和自我描述的方式将它们“传输”到客户端的最佳方法是什么?

If I have an unhandled exception on the WCF service, it seems as though that bubbles back to the client application as a CommunicationException.如果我在 WCF 服务上有一个未处理的异常,它似乎作为一个 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?或者,如果我不在服务级别处理异常(或者只是在服务级别重新抛出它),如何将其明确定向到客户端?

Or is that not typically how this SOA would work?或者这不是典型的 SOA 工作方式吗? What's the "right way" here?这里的“正确方法”是什么?

Thanks!谢谢!

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

Use the FaultContract attribute over methods to enable faultContract and define the message you want to pass using creating a Message contract:在方法上使用 FaultContract 属性来启用 faultContract 并定义要使用创建 Message 协定传递的消息:

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; }
    }
}

To convert exceptions to FaultExceptions, i use the following helper:要将异常转换为 FaultExceptions,我使用以下帮助程序:

    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);
      }
    }

Finally, at the operationContract implementation, simple do this:最后,在 operationContract 的实现中,简单地这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM