国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#:Web Service異常處理

2024-07-21 02:20:04
字體:
來源:轉載
供稿:網友
在.net中實現web服務時,在web服務接口中產生的任何用戶異常(非soapexception之外的異常)都被包裝為soapexception傳遞給客戶端,這使得難以采用通常的方式處理web service的異常。本文講述如何通過soapexceptionhelper實現一致的異常處理。

web service的異常處理問題
在.net中實現web服務時,web服務接口中產生的任何用戶異常(非soapexception之外的異常)都被包裝為soapexception傳遞給客戶端 ,用戶錯誤信息放置在soapexception的message屬性中。

下面的例子演示了一個soapexception封裝的用戶異常信息。webmethod接口testexception代碼拋出一個invalidoperationexception:

[webmethod]
public void testexception() {
throw new invalidoperationexception("invalid operation.");
}

webmethod的客戶端將捕獲一個soapexception異常,message消息如下:



其中message消息包含一段“...-->[ 1 ]:[ 2 ] at ....”的信息,[1]為用戶異常類,[2]為用戶異常消息。而一個原始的soapexception(用new soapexception(...)的方式創建并拋出的異常)則沒有這些信息,下面是一個原始的soapexception消息:



遺憾的是,目前的soapexception并沒有提供更多直接的手段直接獲取原來的異常信息,唯一包含的用戶異常信息在message字符串中,對于使用web service作為分布式機制的應用系統來說是非常不方便的,調用者無法捕獲原來的異常,難以獲取用戶友好的異常信息。同時,因為web service接口代理不再拋出原來的異常,應用的開發者需要考慮兩套完全不同的異常處理機制,帶來了程序結構的復雜性。

創建soapexception輔助類:soapexceptionhelper
soapexceptionhelper輔助類包含下列主要接口:

isuserexception:是否是一個userexception
userexception:返回原始的userexception
message:原始異常的錯誤消息。
獲得原始的用戶異常類和異常消息
通過正則表達式類我們可以獲得原始的用戶異常類和異常消息:

/// <summary>
/// 讀取userexception信息。
/// </summary>
private void readuserexceptioninfo() {
//match user exception class
system.text.regularexpressions.matchcollection mc =
regex.matches(soapexception.message, "---> ([^:]+):");
if (mc.count >= 1) {
userexceptionclass = mc[0].groups[1].value;
//match user exception message
mc = regex.matches(soapexception.message, "---> [^:]+:(.*)/n");
if (mc.count > 0) userexceptionmessage = mc[0].groups[1].value;
}
}

創建用戶異常實例
userexception接口利用反射機制創建一個原來的exception類實例:

... ...
assembly callingassemply = assembly.getcallingassembly();
type exceptiontype = getexceptiontype(callingassemply); //獲得用戶異常類型定義
exception e = null;
try {
try {
e = activator.createinstance(exceptiontype, new object[]{userexceptionmessage}, null) as exception;
}
catch {}
//if no exists constructor with message parameter, use no parameters constructor.
if (e == null) e = activator.createinstance(exceptiontype) as exception;
}catch(exception ex) {
throw new soapexceptionhelperexception(userexceptionclass, ex);
}

return e;

創建用戶異常的問題
因為用戶異常可能定義在不同的集成塊中,soapexceptionhelper可能無法知道它的位置,無法正確的獲取異常類型,如一個與soapexceptionhelper所在集成塊和調用集成塊(callingassembly)不再同一個引用范圍內的異常類。soapexceptionhelper如果無法創建原始異常的實例,就創建一個system.exception對象實例。

為了創建真正的原始異常類,調用者可以在外部獲得實際的異常類型,并傳遞給soapexceptionhelper,因為調用者可以明確的引用異常定義 所在的集成塊。示例如下:

// 項目引用中引入異常定義所在的集成塊
...
soapexceptionhelper helper = new soapexceptionhelper(se);
type type = type.gettype(helper.userexceptionclass, "<異常類所在的集成塊>");
exception e = helper.getuserexception(type);

如果外部沒有傳遞異常類型定義,soapexceptionhelper嘗試以以下順序獲取異常類型定義:

executing assembly
calling assembly
referenced assemblies (of calling assembly)
system.exception
使用soapexceptionhelper
返回用戶友好的消息
使用soapexceptionhelper顯示示例1中的錯誤消息:

try {
... ... // call web method
} catch (soapexception se){
messagebox.show(new soapexceptionhelper(se).message) ; //show "invalid operation." string
}
 

屏蔽soapexception
web service客戶端代理類可以在捕獲soapexception后重新拋出原來的異常,使用這種機制,可以有效的屏蔽web service異常處理的差異,使應用程序采用一致的本地方式處理異常。下面的代碼修改visual studio生成的web service client proxy class(reference.cs文件)實現了這種機制(加粗的部分為新增的代碼):

[system.web.services.protocols.soapdocumentmethodattribute("http://tempuri.org/testexception", requestnamespace="http://tempuri.org/", responsenamespace="http://tempuri.org/", use=system.web.services.description.soapbindinguse.literal, parameterstyle=system.web.services.protocols.soapparameterstyle.wrapped)]
public void testexception() {
try{
this.invoke("testexception", new object[0]);
}catch(soapexception se){
soapexceptionhelper helper = new soapexceptionhelper(se);
if (helper.isuserexception) throw helper.userexception; //rethrow user exception
else throw;
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新源县| 鸡东县| 高台县| 丁青县| 桃园县| 福泉市| 宁陕县| 西城区| 绵竹市| 平安县| 班玛县| 额尔古纳市| 信阳市| 安陆市| 泗水县| 遂宁市| 遂溪县| 沙坪坝区| 共和县| 贺州市| 抚州市| 德惠市| 大化| 汉川市| 石屏县| 嘉兴市| 通辽市| 察雅县| 石景山区| 河东区| 育儿| 松溪县| 理塘县| 华池县| 长宁县| 溆浦县| 佛学| 绍兴市| 郓城县| 平山县| 京山县|