The. NET platform has built-in support for Web service, including the building and use of Web service. Unlike other development platforms, using the. NET platform, you do not need other tools or SDKs to complete the development of Web service. The. NET Framework itself fully supports Web service, Includes server-side request handlers and support for sending and receiving SOAP messages to clients. We'll step through it. Create and use a simple Web Service using Microsoft Visual Studio. NET 2005 (hereafter referred to as Vs.net 2005).
2.1. Create one of the simplest web Service
First, open VS2005, open file-new-Web site, and select ASP.
Looking at the Service.cs code, 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 you are using a design component, uncomment the line
//initializecomponent ();
12}
[WebMethod]
+ public string HelloWorld () {
Return "Hello World";
16}
17}
The default project inside already has a Hello world method, directly run to see the effect,
Click the "HelloWorld" hyperlink in the show page to jump to the next page
Then click on the "Invoke" button to see the Web service results returned in XML format. Explains that our web service environment is not a problem, but also initial exposure to the simplest Web service.
2.2. Create a simple Web Service with functionality
Above we understand the macro-webservice, in fact, it is an external interface, there is a function for external customers to call (note: There is also a client can not call function). If we were the server, we wrote a webservice and gave it to the customer ( At the same time we give them the call rules), the client can be in a relatively transparent state when obtaining information from the server. That is, the customer does not understand (and does not need) its process, they only get the data. In the code file, if we write a function, we want this function to be an externally callable interface function. We must add a line of code to the function [WebMethod (description of the description= function)], and if your function does not have this declaration, it will not be referenced by the user. Down we started writing a simple Web Service example.
The default HelloWorld method is commented out first, and the four methods of subtraction operation are simply written.
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 {
Public Service () {
11//If you are using a design component, uncomment the line
//initializecomponent ();
13}
//[webmethod]
//public string HelloWorld () {
+//return "Hello World";
17//}
[WebMethod (description= method of summation)]
Public double addition (double i,double j)
20 {
return i + j;
22}
[WebMethod (description= "method of Finding the Difference")]
Public double subtract (double I, Double j)
25 {
I-j return;
27}
[WebMethod (description= "method of Quadrature")]
public double multiplication (double I, Double j)
30 {
return I * j;
32}
[WebMethod (description= "method of seeking quotient)]
Division public double (double I, Double j)
35 {
if (J! = 0)
Panax Notoginseng return i/j;
$ else
0;
40}
41}
42
Run can see the methods we write ourselves that can be called, such as:
Also click on the addition method to enter the call page of the addition method.
Enter the parameter i=3,j=3 on the parameter, for example, click Invoke to see the Web service results returned in XML format (the result of the addition of I and J)
Here, we will find that, in fact, WebService is not so mysterious, it is just an interface, for us, the focus is to write the interface function.
2.3. Calling Web Service with ASP.
First, open VS2005, open file-new-Web site, and select ASP. NET Web site.
Select the storage location, click OK after the language, and go to the default page. Then add the Web reference and bring the WebService to the current project. By right-clicking in the Explorer and selecting Add Web Reference, bring up the dialog box:
Fill in the URL, the previously written webservice the address shown above the browser, click the "Go" button, such as will show the referenced webservice can be called method, and then click "Add Reference", will webservice reference to the current project inside , such as the WebService file that is introduced in the solution
Here we practice calling WebService's four methods, make a simple invocation of the example, first in the site's foreground to add a few controls, 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
5
6 <title>webservice Invoke Instance </title>
7
8 <body>
9 <form id= "Form1" runat= "Server" >
Ten <div>
<asp:textbox id= "NUM1" runat= "Server" ></asp:TextBox>
<select id= "selectoper" runat = "server" >
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<asp:textbox id= "Num2" runat= "Server" ></asp:TextBox>
<span id = E runat = "server" ></span>
<asp:textbox id= "Result" runat= "Server" ></asp:TextBox>
</div>
</form>
</body>
25
Then write the calling code in the background, before invoking it and using other objects, instantiate it first, and instantiate it as localhost. Service a = new localhost. Service (); Then you can access the method provided in WebService via a. In this example, the dynamic creation of a button control to trigger the webservice call, 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;
public partial class _default:system.web.ui.page
11 {
protected void Page_Load (object sender, EventArgs e)
13 {
14//dynamically creates a button when the page is loaded, calling WebService in its event
Button btn = New button ();
Btn. Width = 20;
Btn. Text = "=";
Btn. Click +=new EventHandler (Btn_click);
E.controls.add (BTN);
20}
///<summary>
22///Define a Click event that dynamically creates a button that is called in this event webservice
//</summary>
//<param name= "Sender" ></param>
///<param name= "E" ></param>
$ void Btn_click (object sender, EventArgs e)
27 {
if (Num1.text! = "" && num2.text! = "")
29 {
30//Instantiate the referenced WebService object
to localhost. Service webserviceinstance = new localhost. Service ();
int Oper = Selectoper.selectedindex;
Switch (Oper)
34 {
35//Call the WebService exposed method by instantiating the WebService object
Case 0:
PNS Result.text = webserviceinstance.addition (double. Parse (Num1.text), double. Parse (Num2.text)). ToString ();
a break;
Case 1:
Result.text = Webserviceinstance.subtract (double. Parse (Num1.text), double. Parse (Num2.text)). ToString ();
a break;
Case 2:
Result.text = Webserviceinstance.multiplication (double. Parse (Num1.text), double. Parse (Num2.text)). ToString ();
a break;
Case 3:
Result.text = Webserviceinstance.division (double. Parse (Num1.text), double. Parse (Num2.text)). ToString ();
a break;
48}
49}
50}
51}
52
After running, you can see the effect, as shown in the preceding two textbox, enter two operands, select the operator in the Middle drop-down list, and then click the "=" sign to output the computed result to the third textbox.
The entire calculation is not done locally, it is computed on the Web server and then the result is returned to the caller via XML, so when running the program, the WebService program must also be started (so-called startup, that is, the project solution there must be open), Otherwise, an exception is reported that cannot connect to the remote server, such as:
To this a simple webservice development and invocation has been completed, in the actual application can according to their own needs, write some powerful, complex webservice, no matter how complex, the whole process is like this.
Article Source: http://blog.csdn.net/lishimin1012/article/details/38269045
Go: A simple example of WEB Service starter Development--very detailed