客户端的异常处理
这个部分,我们将看看在客户端怎样处理从Web服务中所抛出的异常。为了说明这个做法,我们来创建一个新项目CategoriesServiceClient。一旦项目被创建,就在默认的表单上添加一个命令按钮,并命名为btnInvoke。因为需要在客户端引用Web服务,所以在项目CategoriesService中添加一个WebReference。可以通过Project->AddReference菜单选项来完成添加。然后修改命令按钮的Click事件,如下所示。
privatevoidbtnInvoke_Click(objectsender,System.EventArgse)
{
try
{
Categoriescat=newCategories();
MessageBox.Show(cat.AddCategories("
TestCategory
TestCategoryDescription
").ToString());
}
catch(SoapExceptionsoapEx)
{
MessageBox.Show(soapEx.Code.ToString());
//LoadtheDetailelementoftheSoaopExceptionobject
XmlDocumentdoc=newXmlDocument();
doc.LoadXml(soapEx.Detail.OuterXml);