(原文排版:www.marshine.com) ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
version : 1.1 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
committed : 2004/2/26 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
updated : 2004/3/4 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  在.Net中实现Web服务时,在Web服务接口中产生的任何用户异常(非SoapException之外的异常)都被包装为SoapException传递给客户端,这使得难以采用通常的方式处理WebService的异常。本文讲述如何通过SoapExceptionHelper实现一致的异常处理。ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
Web Service的异常处理问题在.Net中实现Web服务时,Web服务接口中产生的任何用户异常(非SoapException之外的异常)都被包装为SoapException传递给客户端 ,用户错误信息放置在SoapException的Message属性中。ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
下面的例子演示了一个SoapException封装的用户异常信息。WebMethod接口TestException代码抛出一个InvalidOperationException:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  [WebMethod] ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
public void TestException() { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    throw new InvalidOperationException("Invalid Operation."); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
}    ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
WebMethod的客户端将捕获一个SoapException异常,Message消息如下: ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
其中Message消息包含一段“...-->[ 1 ]:[ 2 ] at....”的信息,[1]为用户异常类,[2]为用户异常消息。而一个原始的SoapException(用newSoapException(...)的方式创建并抛出的异常)则没有这些信息,下面是一个原始的SoapException消息:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P

遗憾的是,目前的SoapException并没有提供更多直接的手段直接获取原来的异常信息,唯一包含的用户异常信息在Message字符串中,对于使用WebService作为分布式机制的应用系统来说是非常不方便的,调用者无法捕获原来的异常,难以获取用户友好的异常信息。同时,因为WebService接口代理不再抛出原来的异常,应用的开发者需要考虑两套完全不同的异常处理机制,带来了程序结构的复杂性。ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
创建SoapException辅助类:SoapExceptionHelperSoapExceptionHelper辅助类包含下列主要接口:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  • IsUserException:是否是一个UserException
  • UserException:返回原始的UserException
  • Message:原始异常的错误消息。
获得原始的用户异常类和异常消息通过正则表达式类我们可以获得原始的用户异常类和异常消息:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  /// <summary> ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
/// 读取UserException信息。 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
/// </summary> ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
private void ReadUserExceptionInfo() { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    //match user exception class ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    System.Text.RegularExpressions.MatchCollection mc = ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    Regex.Matches(soapException.Message, "---> ([^:]+):"); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    if (mc.Count >= 1) { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        userExceptionClass = mc[0].Groups[1].Value; ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        //match user exception message ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        mc = Regex.Matches(soapException.Message, "---> [^:]+.*)\n"); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        if (mc.Count > 0) UserExceptionMessage = mc[0].Groups[1].Value; ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    } ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
}    ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
创建用户异常实例UserException接口利用反射机制创建一个原来的Exception类实例:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  ... ... ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
Assembly callingAssemply = Assembly.GetCallingAssembly(); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
Type exceptionType = GetExceptionType(callingAssemply); //获得用户异常类型定义 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
Exception e = null; ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
try { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    try { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        e = Activator.CreateInstance(exceptionType, new object[]{UserExceptionMessage},    null) as Exception; ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    } ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    catch {} ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    //if no exists constructor with message parameter, use no parameters constructor. ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    if (e == null) e = Activator.CreateInstance(exceptionType) as Exception; ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
}catch(Exception ex) { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    throw new SoapExceptionHelperException(userExceptionClass, ex); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
} ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
return e;    ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
创建用户异常的问题因为用户异常可能定义在不同的集成块中,SoapExceptionHelper可能无法知道它的位置,无法正确的获取异常类型,如一个与SoapExceptionHelper所在集成块和调用集成块(CallingAssembly)不再同一个引用范围内的异常类。SoapExceptionHelper如果无法创建原始异常的实例,就创建一个System.Exception对象实例。 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
为了创建真正的原始异常类,调用者可以在外部获得实际的异常类型,并传递给SoapExceptionHelper,因为调用者可以明确的引用异常定义 所在的集成块。示例如下:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  // 项目引用中引入异常定义所在的集成块 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
... ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
SoapExceptionHelper helper = new SoapExceptionHelper(se); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
Type type = Type.GetType(helper.UserExceptionClass, "<异常类所在的集成块>"); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
Exception e = helper.GetUserException(type);    ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
如果外部没有传递异常类型定义,SoapExceptionHelper尝试以以下顺序获取异常类型定义:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
 
  • Executing Assembly
  • Calling Assembly
  • Referenced Assemblies (of Calling Assembly)
  • System.Exception
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
使用SoapExceptionHelper返回用户友好的消息使用SoapExceptionHelper显示示例1中的错误消息:ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  try { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    ... ... // call web method ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
} catch (SoapException se){ ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    MessageBox.Show(new SoapExceptionHelper(se).Message) ; //show "Invalid Operation." string ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
} ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
      ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
屏蔽SoapExceptionWeb Service客户端代理类可以在捕获SoapException后重新抛出原来的异常,使用这种机制,可以有效的屏蔽WebService异常处理的差异,使应用程序采用一致的本地方式处理异常。下面的代码修改Visual Studio生成的Web ServiceClient Proxy Class(Reference.cs文件)实现了这种机制(加粗的部分为新增的代码): ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
  [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/TestException",RequestNamespace="http://tempuri.org/",Resp,Use=System.Web.Services.Description.SoapBindingUse.Literal,ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
public void TestException() { ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    try{ ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        this.Invoke("TestException", new object[0]); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    }catch(SoapException se){ ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        SoapExceptionHelper helper = new SoapExceptionHelper(se); ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        if (helper.IsUserException) throw helper.UserException; //rethrow user exception ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
        else throw; ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
    } ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
}    ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
下载SoapExceptionHelper.cs源代码 ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
参考:Throwing SoapExceptions ái»<8µâ9www.netcsharp.cná¹%¼.¨g#P
Handling and Throwing Exceptions in XML Web Servicesái»<8µâ9www.netcsharp.cná¹%¼.¨g#P