WCF中的异常处理

简介:

异常消息与特定技术有关,.NET异常同样如此,因而WCF并不支持传统的异常处理方式。如果在WCF服务中采用传统的方式处理异常,由于异常消息不能被序列化,因而客户端无法收到服务抛出的异常,例如这样的服务设计:

[ServiceContract(SessionMode = SessionMode.Allowed)]
public  interface IDocumentsExplorerService
{
    [OperationContract]        
    DocumentList FetchDocuments( string homeDir);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public  class  DocumentsExplorerService : IDocumentsExplorerService,IDisposable
{
   public DocumentList FetchDocuments( string homeDir)
   {
     //Some Codes

     if (Directory.Exists(homeDir))
     {
         //Fetch documents according to homedir
     }
     else
     {
         throw  new DirectoryNotFoundException(
                      string.Format( "Directory {0} is not found.",homeDir));
     }
   }
   public  void Dispose()
   {            
    Console.WriteLine( "The service had been disposed.");
   }
}

则客户端在调用如上的服务操作时,如果采用如下的捕获方式是无法获取该异常的:

catch (DirectoryNotFoundException ex)
{
     //handle the exception;
}

为了弥补这一缺陷,WCF会将无法识别的异常均当作为FaultException异常对象,因此,客户端可以捕获FaultException或者Exception异常:

catch (FaultException ex)
{
     //handle the exception;
}
catch (Exception ex)
{
     //handle the exception;
}

然而,这样捕获的异常,却无法识别DirectoryNotFoundException所传递的错误信息。尤为严重的是这样的异常处理方式还会导致传递消 息的通道出现错误,当客户端继续调用该服务代理对象的服务操作时,会获得一个CommunicationObjectFaultedException异 常,无法继续使用服务。如果服务被设置为PerSession模式或者Single模式,异常还会导致服务对象被释放,终止服务。

WCF为异常处理专门提供了FaultContract特性,它可以被应用到服务操作上,指明操作可能会抛出的异常类型。例如前面的服务契约就可以修改为:

[ServiceContract(SessionMode = SessionMode.Allowed)]
public  interface IDocumentsExplorerService
{
[OperationContract]
[FaultContract(typeof(DirectoryNotFoundException))]
    DocumentList FetchDocuments( string homeDir);
}

然而,即使通过FaultContract指定了操作要抛出的异常,然而如果服务抛出的异常并非FaultException或者FaultException<T>异常,同样会导致通道发生错误。因此在服务实现中,正确的实现应该如下:

public  class  DocumentsExplorerService : IDocumentsExplorerService,IDisposable
{
    public DocumentList FetchDocuments( string homeDir)
    {
     //Some Codes

     if (Directory.Exists(homeDir))
     {
         //Fetch documents according to homedir
     }
     else
     {
        DirectoryNotFoundException exception =  new DirectoryNotFoundException();
         throw  new FaultException<DirectoryNotFoundException>(exception,
                     new FaultReason( string.Format( "Directory {0} is not found.", homeDir)));
     }
   }
}

我们可以将服务所要抛出的异常类型作为FaultException<T>的类型参数,然后创建一个FaultReason对象用以传递错误消 息。客户端在调用服务代理对象时,可以捕获FaultException< DirectoryNotFoundException>异常,并且该异常不会使得通道发生错误,并且客户端可以继续使用该服务代理对象。即使服务 为PerCall服务,客户端仍然可以继续调用服务操作。如果服务为Session服务或Singleton服务,那么即使发生了异常,服务对象也不会被 终结。

如果只是为了让客户端获得异常消息,即使不施加FaultContract特性,或者抛出非FaultException异常,我 们也可以通过ServiceBehavior特性,将服务的IncludeExceptionDetailInFaults设置为true(默认为 false),此时,客户端可以捕获抛出的非FaultException异常信息,但该异常仍然会导致通道出现错误。

但是,在发布服务与部署服务时,我们应避免将服务的IncludeExceptionDetailInFaults设置为true。

如 果不希望使用FaultContract,同时又要保证服务抛出的异常能够被客户端捕获,并且不会导致通道错误,我们还可以通过错误处理扩展的方式实现。 此时,我们可以将服务本身作为错误处理对象,令其实现System.ServiceModel.Dispatcher.IErrorHandler接口:

public  class  DocumentsExplorerService : IDocumentsExplorerService,IErrorHandler, IDisposable
{
   //…
}

在该接口的ProvideFault()方法中,可以将非FaultContract异常提升为FaultContract<T>异常,例如将 DirectoryNotFoundException异常提升为FaultExceptino< DirectoryNotFoundException>异常:

public  void ProvideFault(Exception error, MessageVersion version,  ref Message fault)
{
     if (error  is DirectoryNotFoundException)
     {
        FaultException<DirectoryNotFoundException> faultException =  new FaultException<DirectoryNotFoundException>(
             new DirectoryNotFoundException(),  new FaultReason(error.Message));
        MessageFault messageFault = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version,messageFault,faultException.Action);
     }
}

而在该接口的HandleError()方法中,则可以处理异常错误,例如记录日志。

要使得错误处理扩展生效,还需要向服务通道安装错误处理扩展。方法是让服务类实现System.ServiceModel.Description.IServiceBehavior接口:

public  class  DocumentsExplorerService : IDocumentsExplorerService,IErrorHandler,IServiceBehavior,IDisposable
{
   //…
}

然后在ApplyDispatchBehavior()方法中安装错误处理扩展:

public  void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
     foreach (ChannelDispatcher dispatcher  in serviceHostBase.ChannelDispatchers)
     {
        dispatcher.ErrorHandlers.Add( this);
     }
}

通过这样的处理,即使服务抛出的异常为DirectoryNotFoundException异常,并且在服务契约中没有通过FaultContract特 性指定该异常,客户端同样能够获得异常的错误信息,且该异常不会导致通道发生错误,客户端可以继续调用服务代理对象的操作。









本文转自wayfarer51CTO博客,原文链接:http://blog.51cto.com/wayfarer/280124,如需转载请自行联系原作者

相关文章
|
网络架构
艾伟:WCF从理论到实践(10):异常处理
本系列文章导航 WCF从理论到实践(1):揭开神秘面纱 WCF从理论到实践(2):决战紫禁之巅 WCF从理论到实践(3):八号当铺之黑色契约 WCF从理论到实践(4):路在何方 WCF从理论到实践(5):Binding细解 WCF从理论到实践(6):WCF架构 WCF从理论到实践(7):消息交换模式...
1141 0
|
XML 数据格式 网络架构
艾伟_转载:WCF基本异常处理模式[中篇]
  通过WCF基本的异常处理模式[上篇], 我们知道了:在默认的情况下,服务端在执行某个服务操作时抛出的异常(在这里指非FaultException异常),其相关的错误信息仅仅限于服务端可见,并不会被WCF传递到客户端;如果将开启了IncludeExceptionDetailInFaults的ServiceDebug服务行为通过声明(通过在服务类型上应用ServiceBehaviorAttrite特性)或者配置的方式应用到相应的服务上,异常相关的所有细节信息将会原封不动地向客户端传送。
878 0
|
XML 数据格式
艾伟_转载:WCF基本异常处理模式[下篇]
  从FaultContractAttribute的定义我们可以看出,该特性可以在同一个目标对象上面多次应用(AllowMultiple = true)。这也很好理解:对于同一个服务操作,可能具有不同的异常场景,在不同的情况下,需要抛出不同的异常。
912 0
|
XML 数据格式
艾伟_转载:WCF基本异常处理模式[上篇]
  由于WCF采用.NET托管语言(C#和NET)作为其主要的编程语言,注定以了基于WCF的编程方式不可能很复杂。同时,WCF设计的一个目的就是提供基于非业务逻辑的通信实现,为编程人员提供一套简单易用的应用编程接口(API)。
1137 0