The previous article introduced some basic features of webservice and the structure of our example. This article will begin with the specific coding work.
This topic focuses on webservice. Therefore, our code here focuses on Webservice, while other projects, such as ServiceGatherSite and WebSiteA, will only be briefly introduced.
It is not difficult to develop a webservice in VS2003. First, create a webservice Project (file-> New-> Project-> C #-> Web service application)
After this project is created, we will see a file named Service1.asmx, which is the standard file of webservice. It also has the UI concept, but we generally do not pay attention to it. Therefore, we can view its cs code file. if you haven't done anything yet, you will see a commented-out helloworld WebMethod, remove the annotation, and run it to get the simplest webservice running instance. click "helloworld" to execute the method. obviously, this function only means to understand the web Service writing method at a macro level.
Next, we will introduce the webservice method in detail. in the code file, if we want this function to become an externally callable interface function after writing a function, we must add a line of code [WebMethod (Description = "function Description")] to the function. If your function does not have this Declaration, it cannot be referenced by users. for example:
[WebMethod (Description = "simplest method")]
Public string HelloWorld ()
{
Return "Hello World ";
}
This function is an externally callable interface function, which is equivalent to an API for users. if a user calls the HelloWorld () method after referencing this service, he will get the return value "HelloWorld.
We can see whether we have found that webservice is not so mysterious. It is just an interface. For us, the focus is on writing interface functions. below, I will provide the interface functions required for our example.
[WebMethod (Description = "querying to obtain required courseware information")]
Public XmlDataDocument GetSiteAData (string AssignName)
{
XmlDataDocument xd = new XmlDataDocument ();//
DataSet ds = new DataSet ();
CStoreProc cp = new CStoreProc ("SearchAssign ");
Cp. AddParIn ("@ keywords", SqlDbType. VarChar, 30, AssignName );
Cp. AddParOut ("@ res", SqlDbType. Int );
If (cp. SelectProc () // if the execution is successful, the stored procedure
{
Cp. myData. EnforceConstraints = false; // The format is not strictly checked.
If (int) cp. GetReturnValue ("@ res") =-1)
{
String xml = "<NewDataSet> </NewDataSet> ";
Xd. LoadXml (xml );
Return xd;
}
Xd = new XmlDataDocument (cp. myData );
XmlNode root1 = xd. DocumentElement;
XmlNodeList roots = root1.SelectNodes ("list ");
Foreach (XmlNode roota in roots) // Add the site name tag to all elements
{
XmlElement Link = xd. CreateElement ("SiteName ");
Link. InnerText = ConfigurationSettings. receivettings ["SiteName"]. ToString ();
Roota. AppendChild (Link );
}
Return xd;
}
Else return null;
}
This is an interface function for obtaining resource site information. for most of the Code in it, I think it should be clear to some friends who have a certain asp.net base. Here we only describe CStoreProc, which is a stored procedure class I encapsulated, the main function is to execute various types of stored procedures.
Careful friends may find that the return type of this function seems special. It is an xml document. as we have mentioned earlier, webservice can only transmit serialized data. xml obviously meets the conditions, but non-serialized data such as hash tables cannot be transmitted. xml is the most widely used, considering cross-platform applications, we only use xml data transmission as an example.
In the next article, we will go deep into parsing the essence of webservice-xml data transmission :)
From: IT lab