The. NET platform has built-in support for Web Services, including the construction and use of web services. Unlike other development platforms, you can use the. NET platform to develop Web services without other tools or sdks .. Net Framework itself fully supports Web services, including server-side request processors and support for sending and receiving soap messages to clients. Next, we will use Microsoft Visual Studio. NET 2005 (vs. NET 2005) to create and use a simple web service.
2.1 create a simple Web Service
First, open vs2005, open "file-New-Website", and select "ASP. NET web service ".
View service. CSCode, You will find that vs. NET 2005 has established a default framework for the Web service file. The original code is:
1 using system;
2 using system. Web;
3 using system. Web. Services;
4 using system. Web. Services. Protocols
5 [WebService (namespace = "http://tempuri.org/")]
6 [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
7 public class service: system. Web. Services. WebService
8 {
9 Public Service ()
10 // If the designed component is used, uncomment the following line
11 // initializecomponent ();
12}
13 [webmethod]
14 Public String helloworld (){
15 return "Hello World ";
16}
17}
There is already a hello World method in the default project. Run it directly to see the effect,
Click the "helloworld" hyperlink on the displayed page to jump to the next page.
Click the "call" button to view the Web service results returned in XML format. It indicates that our web service environment is correct, and we have initially come into contact with the simplest web service.
2.2 create a simple and functional Web Service
We have learned about WebService at a macro level. In fact, WebService is an external interface, which contains functions that can be called by external customers (Note: there are also functions that cannot be called by customers ). if we are a server, we have written a WebService and then gave it to the customer (and we also gave them the call rules ), the customer can be in a relatively transparent State when obtaining information from the server. that is, the customer does not understand (or need to) the process, and they only obtain data. 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. next, let's write a simple web service example.
First, comment out the default helloworld method, and simply write the four methods for finding addition, subtraction, multiplication, division, and so on;
1 using system;
2 using system. Web;
3 using system. Web. Services;
4 using system. Web. Services. Protocols;
5
6 [WebService (namespace = "http://tempuri.org/")]
7 [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
8 public class service: system. Web. Services. WebService
9 {
10 public service (){
11 // if you use the designed component, uncomment the following line
12 // initializecomponent ();
13}
14 // [webmethod]
15 // Public String helloworld (){
16 // return "Hello World ";
17 //}
18 [webmethod (description = "summation method")]
19 public double addition (double I, Double J)
20 {
21 return I + J;
22}
23 [webmethod (description = "difference calculation method")]
24 public double subtract (double I, Double J)
25 {
26 return I-j;
27}
28 [webmethod (description = "Method for product calculation")]
29 public double multiplication (double I, Double J)
30 {
31 return I * J;
32}
33 [webmethod (description = "method of quotient")]
34 public double division (double I, Double J)
35 {
36 IF (J! = 0)
37 return I/J;
38 else
39 return 0;
40}
41}
42
Run the command to view the methods we have written that can be called, such:
Similarly, click the addition method to go to the addition method call page.
Enter the parameters I = 3 and J = 3. For example, click call to view the Web Service result returned in XML format (the result of adding I and j)
Here, we will find that WebService is not so mysterious. It is just an interface. For us, the focus is on writing interface functions.
2.3. use ASP. NET to call Web Service
First, open vs2005, open "file-New-Website", and select "ASP. NET Website ".
Select the storage location and click "OK" to go to the default page. Then add a web reference to bring the WebService to the current project. The method is as follows: Right-click the resource manager and choose add web reference to bring up the dialog box:
Enter in the URL, the address displayed in the browser after the WebService is run, and click "go". For example, the method that can be called in the referenced WebService is displayed, click "add reference" to reference the WebService to the current project. For example, the introduced WebService file appears in the solution.
Here we will practice calling WebService's four methods. For a simple call example, add several controls on the front-end of the website. The Code is as follows:
1 <% @ page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
2
3 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <HTML xmlns = "http://www.w3.org/1999/xhtml">
5
6 <title> WebService call instance </title>
7
8 <body>
9 <Form ID = "form1" runat = "server">
10 <div>
11 <asp: textbox id = "num1" runat = "server"> </ASP: textbox>
12 <select id = "selectoper" runat = "server">
13 <option> + </option>
14 <option>-</option>
15 <option> * </option>
16 <option>/</option>
17 </SELECT>
18 <asp: textbox id = "num2" runat = "server"> </ASP: textbox>
19 <span id = e runat = "server"> </span>
20 <asp: textbox id = "result" runat = "server"> </ASP: textbox>
21 </div>
22 </form>
23 </body>
24
25
Then write the calling code in the background. before calling the code, just like using other objects, instantiate the code first. The instantiation method is localhost. service A = new localhost. service (); then you can use a to access the methods provided in WebService. In this example, a button control is dynamically created to trigger WebService calls. The background code is as follows:
1 using system;
2 using system. DaTa;
3 using system. configuration;
4 using system. Web;
5 using system. Web. Security;
6 using system. Web. UI;
7 using system. Web. UI. webcontrols;
8 using system. Web. UI. webcontrols. webparts;
9 using system. Web. UI. htmlcontrols;
10 public partial class _ default: system. Web. UI. Page
11 {
12 protected void page_load (Object sender, eventargs E)
13 {
14 // dynamically create a button during page loading and call WebService in its event
15 button BTN = new button ();
16 BTN. width = 20;
17 BTN. Text = "= ";
18 BTN. Click + = new eventhandler (btn_click );
19 E. Controls. Add (BTN );
20}
21 /// <summary>
22 // defines the click event for dynamically creating a button, in which WebService is called
23 /// </Summary>
24 /// <Param name = "sender"> </param>
25 /// <Param name = "E"> </param>
26 void btn_click (Object sender, eventargs E)
27 {
28 If (num1.text! = "" & Num2.text! = "")
29 {
30 // instantiate the referenced WebService object
31 localhost. Service webserviceinstance = new localhost. Service ();
32 int counters = selectoper. selectedindex;
33 switch (switches)
34 {
35 // call the WebService exposure method through the instantiated WebService object
36 case 0:
37 result. Text = webserviceinstance. addition (double. parse (num1.text), double. parse (num2.text). tostring ();
38 break;
39 Case 1:
40 result. Text = webserviceinstance. Subtract (double. parse (num1.text), double. parse (num2.text). tostring ();
41 break;
42 Case 2:
43 result. Text = webserviceinstance. Multiplication (double. parse (num1.text), double. parse (num2.text). tostring ();
44 break;
45 case 3:
46 result. Text = webserviceinstance. Division (double. parse (num1.text), double. parse (num2.text). tostring ();
47 break;
48}
49}
50}
51}
52
After running, you can see the effect, as shown in. Enter two operands in the first two Textbox, select the operator from the drop-down list in the middle, and click "=, output the calculated result to the third textbox.
The whole computation is not performed locally. It is performed on the Web server and then the results are returned to the caller through XML. ThereforeProgramThe WebService program must also be started (the so-called start, that is, the project solution over there must be opened), otherwise an exception will be reported that the remote server cannot be connected, such:
By now, the development and calling of a simple WebService have been completed. In actual applications, you can write powerful and complex WebServices according to your own needs. No matter how complicated, the whole process is like this.