Web服务中的异常处理(2)

王朝java/jsp·作者佚名  2006-01-08
宽屏版  字体: |||超大  

Web服务的实现

为了达到这个示例的目的,我们创建一个名为CategoriesService的Web服务,选择一个可视化的C# ASP.NET Web服务作为项目的模版。一旦创建项目,我们就添加一个名为AddCategories的方法,并且给这个方法添加下列代码:

[WebMethod]

public bool AddCategories(string xml)

{

try

{

using(SqlConnection conn = new SqlConnection())

{

if (ValidateXml(xml))

{

XmlDocument doc = new XmlDocument();

doc.LoadXml(xml);

conn.ConnectionString =

"server=localhost;uid=sa;pwd=thiru;database=northwind";

conn.Open();

XmlNamespaceManager nsManager = new

XmlNamespaceManager(doc.NameTable);

// Add the namespace to the NamespaceManager

nsManager.AddNamespace("catNS",

"http://tempuri.org/CategoriesNamespace");

XmlNode categoryNode =

doc.DocumentElement.SelectSingleNode("catNS:Category",

nsManager);

string categoryName =

categoryNode.SelectSingleNode("catNS:CategoryName",

nsManager).InnerText;

string categoryDescription =

categoryNode.SelectSingleNode("catNS:CategoryDescription",

nsManager).InnerText;

SqlCommand command = new

SqlCommand("usp_InsertCategories",conn);

command.CommandType = CommandType.StoredProcedure;

//Add the Category Name parameter

SqlParameter paramCategoryName = new

SqlParameter("@CategoryName", SqlDbType.NVarChar,15);

paramCategoryName.Direction = ParameterDirection.Input;

paramCategoryName.Value = categoryName;

command.Parameters.Add(paramCategoryName);

//Add the Description parameter

SqlParameter paramDescription = 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);

}

return true;

}

catch (SoapException soapEx)

{

throw soapEx;

}

catch(Exception ex)

{

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方法的最后一个参数是一个枚举常量,它的定义如下。

public enum FaultCode

{

Client = 0,

Server = 1

}

XML校验失败表示客户端提供了无效的XML数据。这种情况,我们应该把枚举常量设为Client,给客户应用程序指出这种错误。这就使得我们通知客户端应用程序在再一次调用Web服务之前需要检查输入数据的格式成为可能。如果Web服务由于一些其他原因(例如,数据库服务器的不可用)而失败,那么就需要设置枚举常量为Server。这就说明Web服务失败是由于服务器端的一些问题造成的,客户应用程序可以在几秒钟后重新请求。事实上,在catch块中捕捉一般Exception,这正是我们要做的。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有