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

首頁 > 開發 > 綜合 > 正文

WEB SERVICES異常

2024-07-21 02:21:23
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。當web services引發一個不在內部處理的異常時,異常將被傳送到asp.net,來決定如何處理.
通常有兩種:


1)將異常發往http客戶端,asp.net將向客戶端返回一條http internal server
error(內部錯誤,錯誤代碼是500)消息,客戶端根據消息進行處理.


2)將異常發往soap客戶端
如果客戶端使用soap協議,asp.net則先將其作為soap錯誤,然后傳回客戶端.使用soap代理的
客戶端收到一個soap錯誤時,其響應方式與前一種差不多,不同的是soap代理引發的是system
.web.services.protocols.soapexception.,并將soapexception.code的值設置為soap
錯誤的faultcode字段(默認是服務器錯誤)的值,將soapexception.message屬性設置為
faultstrin字段(默認是表示捕獲異常的堆棧跟蹤的字符串)的值.

但這種方法的缺點是無論web services拋出何種異常,客戶端都是拋出soapexception.
解決的方法就不允許asp.net自動處理未被捕獲的客戶端綁定異常,web services必須捕獲所有應用程序異常,并明確拋一個soapexceptionasp.net捕獲時將正確地使用soapexception的code和message屬性來填充soa的錯誤的faultcode和faultstring元素,這樣,你就可以返回自定義錯誤代碼,這些代碼向客戶端提供了有意義的錯誤信息,然后,客戶端就可以捕獲soapexception,并用soapexception.code屬性提供的錯誤代碼來執行適當的處理.不過這還是有缺陷,那就是客戶端的開發人員必須知道怎么樣處理這些代碼,所以必須將這些信息放進系統文檔.

自定義的錯誤代碼
public sealed class e1 : applicationexception
{
public e1(string message):base(message)
{
//描述該錯誤的信息,不做任何事,依賴于基類
// todo: add constructor logic here
//
}
}
public sealed class e2 : applicationexception
{
public e2(string message):base(message)
{
//描述該錯誤的信息,不做任何事,依賴于基類
// todo: add constructor logic here
//
}
}
需要注意的是,soapexception.code屬性并非一個字符串,而是system.xml.xmlqualifiedname
的一個實例,因此必須實例化它.如下:
using system.web;
using system.web.services;
using system.web.services.protocols;
using system.xml;
using system;
[webservicebinding(conformanceclaims=wsiclaims.bp10,emitconformanceclaims = true)]
public class service : system.web.services.webservice
{
xmlqualifiedname xname;
[webmethod]
public int causeexception(int num)
{
int n;
try
{
n=num;
if (n == 1)
{
throw new e1("erroe e1 happen");
}
if (n == 2)
{
throw new e2("erroe e2 happen");
}
}
catch(applicationexception ex)
{
throw getexception(ex);
}

return n;

}
private soapexception getexception(applicationexception exx)
{

if (exx is e1)
{
xname = new xmlqualifiedname("e1");
}
if (exx is e2)
{
xname = new xmlqualifiedname("e2");
}
return new soapexception(exx.message, xname);
}

}

客戶端:
private void button1_click(object sender, eventargs e)
{
try
{
exception.service s = new testexception.exception.service();//實例化代理
if (this.radiobutton1.checked)
{
s.causeexception(1);//引發異常
}
if (this.radiobutton2.checked)
{
s.causeexception(2);
}
}
catch (soapexception ex)
{
switch(ex.code.tostring())//根據soapexception.code的值顯示相應的消息
{
case "e1":messagebox.show("has error e1");
break;
case "e2":messagebox.show("has error e2");
break;
}
}

}
http://blog.csdn.net/marshine/archive/2004/03/25/17170.aspx

c#:web service異常處理的其他文章.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潢川县| 龙里县| 马关县| 台中县| 濮阳县| 黔西| 嵊泗县| 六盘水市| 南木林县| 沈阳市| 海口市| 通山县| 巴林左旗| 蓬溪县| 永康市| 土默特左旗| 绥阳县| 元氏县| 赤峰市| 汕尾市| 旬阳县| 林芝县| 韶山市| 滦南县| 乌什县| 兴安盟| 甘孜县| 绥化市| 南开区| 台中县| 克什克腾旗| 札达县| 宜宾市| 大兴区| 保德县| 辉南县| 景泰县| 肃北| 萨嘎县| 中卫市| 静乐县|