Web service implementation
To achieve the purpose of this example, we create a web service named categoriesservice and select a Visual C # ASP. netweb service as the project template. Once a project is created, we add a method named addcategories and add the following Code :
[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 (),
& Quot; 2000 & quot;, & quot; addcategories & quot;, faultcode. Client );
}
Returntrue;
}
Catch (soapexceptionsoapex)
{
Throwsoapex;
}
Catch (exceptionex)
{
EventLog. writeentry ("test", Ex. Message );
Throw
Raiseexception ("addcategories ",
Http://tempuri.org/CategoriesService, Ex. Message,
& Quot; 1000 & quot;, Ex. Source, faultcode. Server );
}
}
As prompted by its name, the addcategories method is responsible for adding detailed information about the category to the categories table of the northwind database. Before the add operation, addcategories uses an external XML mode file to verify the added XML data. If the verification fails, it throws an exception to the Web service client.
Let's take a rough look at the above Code. First, pass the XML data to it and call the validatexml method. Later, let's look at the code of the validatexml method. The validatexml method returns true or false, depending entirely on whether the XML verification is successful. If true is returned, create an xmldocument object instance and import XML data to it. In addition, set the connectionstring attribute to initialize the sqlconnection object and call the open method of the sqlconnection object. Create an xmlnamespacemanager instance and call the addnamespace method to associate a namespace. Once a namespace is associated, you can use the namespace identifier to reference the correct XML element. Create a sqlparameter object instance and add parameters to the stored procedure. Finally, call the executenonquery method of the sqlcommand object to execute the stored procedure.
If the validatexml method returns false, A soapexception is thrown using the assistant method named raiseexception. We will discuss raiseexception now. The raiseexception method is a basic helper method that encapsulates the code used to throw an exception from a Web service. The last parameter of the raiseexception method is an enumeration constant, which is defined as follows.
Publicenumfaultcode
{
Client = 0,
Server = 1
}
XML validation failure indicates that the client provides invalid XML data. In this case, we should set the enumerated constant to client and apply the Program to the customer to indicate this error. This makes it possible to notify the client application to check the input data format before calling the Web service again. If the Web service fails for some other reasons (for example, the database server is unavailable), you need to set the enumerated constant to server. This indicates that the failure of the web service is caused by some problems on the server. The customer application can request again in a few seconds. In fact, catch general exceptions in catch blocks, which is exactly what we need to do. (Source: pconline)