Web服務中的異常處理(2)
發表于:2007-05-25來源:作者:點擊數:
標簽:
Web服務的實現 為了達到這個示例的目的,我們創建一個名為CategoriesService的Web服務,選擇一個可視化的C#ASP.NETWeb服務作為項目的模版。一旦創建項目,我們就添加一個名為AddCategories的方法,并且給這個方法添加下列代碼: [WebMethod] publicboolAddCa
Web服務的實現
為了達到這個示例的目的,我們創建一個名為CategoriesService的Web服務,選擇一個可視化的C#ASP.NETWeb服務作為項目的模版。一旦創建項目,我們就添加一個名為AddCategories的方法,并且給這個方法添加下列代碼:
[WebMethod]
publicboolAddCategories(stringxml)
{
try
{
using(SqlConnectionconn=newSqlConnection())
{
if(ValidateXml(xml))
{
XmlDocumentdoc=newXmlDocument();
doc.LoadXml(xml);
conn.ConnectionString=
"server=localhost;uid=sa;pwd=thiru;database=northwind";
conn.Open();
XmlNamespaceManagernsManager=new
XmlNamespaceManager(doc.NameTable);
//AddthenamespacetotheNamespaceManager
nsManager.AddNamespace("catNS",
"http://tempuri.org/CategoriesNamespace");
XmlNodecategoryNode=
doc.DocumentElement.SelectSingleNode("catNS:Category",
nsManager);
stringcategoryName=
categoryNode.SelectSingleNode("catNS:CategoryName",
nsManager).InnerText;
stringcategoryDescription=
categoryNode.SelectSingleNode("catNS:CategoryDescription",
nsManager).InnerText;
SqlCommandcommand=new
SqlCommand("usp_InsertCategories",conn);
command.CommandType=CommandType.StoredProcedure;
//AddtheCategoryNameparameter
SqlParameterparamCategoryName=new
SqlParameter("@CategoryName",SqlDbType.NVarChar,15);
paramCategoryName.Direction=ParameterDirection.Input;
paramCategoryName.Value=categoryName;
command.Parameters.Add(paramCategoryName);
//AddtheDescriptionparameter
SqlParameterparamDescription=new
SqlParameter("@Description",SqlDbType.Text);
paramDescription.Direction=ParameterDirection.Input;
paramDescription.Value=categoryDescription;
command.Parameters.Add(paramDescription);
command.ExecuteNonQuery();
}
else
throw
RaiseException("AddCategories",
"http://tempuri.org/CategoriesService",
builder.ToString(),
"2000","AddCategories",FaultCode.Client);
}
returntrue;
}
catch(SoapException
soapEx)
{
throwsoapEx;
}
catch(Exceptionex)
{
EventLog.WriteEntry("Test",ex.Message);
throw
RaiseException("AddCategories",
"http://tempuri.org/CategoriesService",ex.Message,
"1000",ex.Source,FaultCode.Server);
}
}
正如其名所提示的那樣,AddCategories方法負責把category的詳細信息添加到Northwind
數據庫的categories表中。在執行添加操作之前,AddCategories方法使用一個外部的XML模式文件校驗被添加的XML數據,如果校驗失敗,它給Web服務的客戶端拋出一個異常。
讓我們來大致瀏覽上面的代碼吧。首先,把XML數據傳遞給它,調用ValidateXml方法。過一會我們再來看ValidateXml方法的代碼。ValidateXml方法返回true或false,這完全取決于XML校驗是否成功。如果返回true,那么就創建一個XmlDocument對象實例,并給它導入XML數據,另外還設置ConnectionString屬性來初始化SqlConnection對象,然后調用SqlConnection對象的Open方法。其次,創建一個XmlNamespaceManager實例,調用AddNamespace方法關聯一個命名空間。一旦關聯命名空間,我們就可以使用命名空間標識符引用正確的XML元素。再次,創建一個SqlParameter對象實例,給存儲過程添加參數。最后,調用SqlCommand對象的ExecuteNonQuery方法執行存儲過程。
如果ValidateXml方法返回false,則用名為RaiseException的助手方法拋出SoapException。我們現在就來討論RaiseException。RaiseException方法一個基本的助手方法,它封裝用來從Web服務中拋出異常的代碼。RaiseException方法的最后一個參數是一個枚舉常量,它的定義如下。
publicenumFaultCode
{
Client=0,
Server=1
}
XML校驗失敗表示客戶端提供了無效的XML數據。這種情況,我們應該把枚舉常量設為Client,給客戶應用程序指出這種錯誤。這就使得我們通知客戶端應用程序在再一次調用Web服務之前需要檢查輸入數據的格式成為可能。如果Web服務由于一些其他原因(例如,數據庫
服務器的不可用)而失敗,那么就需要設置枚舉常量為Server。這就說明Web服務失敗是由于服務器端的一些問題造成的,客戶應用程序可以在幾秒鐘后重新請求。事實上,在catch塊中捕捉一般Exception,這正是我們要做的。
原文轉自:http://www.kjueaiud.com
老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月
|