Abnormal processing in web services (2)

zhaozj2021-02-16  66

Implementation of Web services

In order to achieve this example, we create a Web service called CategoriesService, select a visual C # ASP.NET web service as analog version of the project. Once the project is created, we add a method called AddCategories and add the following code to this method:

[WebMethod]

Public Bool Addcategories (String XML)

{

Try

{

Using (SqlConnection Conn = New SqlConnection ())

{

ValidateXML (XML))

{

XmLDocument Doc = New XmLDocument ();

Doc.loadxml (XML);

Conn.connectionstring =

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

Cn.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;

}

}

As suggested, the AddCategories method is responsible for adding Category's details to the categories table of the Northwind database. Before performing the Add Action, the AddCategories method uses an external XML schema file to verify the added XML data. If the verification fails, it throws an exception to the client client.

Let's come to lector to browse the above code. First, pass the XML data to it, call the validateXML method. After a while, let's see the code of the validatexml method. ValidateXML method returns true or false, depending on whether the XML verification is successful. If you return true, then create an XMLDocument object instance and import XML data, and set the connectionString property to initialize the SQLConnection object, then call the Open method of the SQLConnection object. Second, create an XMLNamespaceManager instance, call the AddNameSpace method to associate a namespace. Once the namespace is associated, we can use the namespace identifier to reference the correct XML element. Again, create a SQLParameter object instance to add parameters to the stored procedure. Finally, call the EXECUTENONQUERY method of the SQLCommand object to perform the stored procedure.

If the ValidateXML method returns false, the SOAPEXCEPTION is thrown with the assistant method named raiseexception. Let's discuss RaiseException now. RaiseException method A basic assistant method, which is encapsulated to throw an exception code from the web service. The last parameter of the RaiseException method is an enumeration, which is defined as follows.

Public enum faultcode

{

Client = 0,

Server = 1

}

Xml

The verification failure indicates that the client provides invalid.

Xml

data. In this case, we should set the enumeration.

Client

, Indicate this error to the client application. This makes us notify the client application again to call again

Web

It is possible to check the format of the input data before the service. in case

Web

Services fails due to some other reasons (for example, the database server is not available), then set the enumeration

Server

. This incidates that

Web

Service failure is due to some of the problems of the server, the client application can reclaim after a few seconds. In fact, in catch

Capture in block

EXCEPTION

This is exactly what we have to do.

转载请注明原文地址:https://www.9cbs.com/read-24730.html

New Post(0)