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).
WebService, in fact, it is an external interface, there are functions available for external clients 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 client (and we gave them the call rule), Customers can be in a relatively transparent state when they get 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 and want this function to be an externally callable interface function, we must add a line of code to the function [ WebMethod (Description of description= "function")], if your function does not have this declaration, it will not be referenced by the user. Down we started writing an example of a simple Web Service.
2.1. Create one of the simplest web Service
First, open VS2005, open file-new-Web site, and select ASP.
Add the following code in the Appcode/service.cs
1using System;
2using system.web;
3using System.Web.Services;
4using System.Web.Services.Protocols;
5[webservice (Namespace = "http://tempuri.org/")]
6[webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)]
7public 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//}
[WebMethod (Description = "method of summation")]
Public double addition (double I, Double j)
19{
return i + j;
21}
[WebMethod (Description = "Method of finding Difference")]
% public double Qiucha (double I, Double j)
24{
if (i > J)
I-j return;
Else
J-i return;
29}
30}
to
2. Call the interface in the WebService:
1). Open VS2005, open "file-new-Web" and select "ASP." NET Web site.
2). Then add the Web reference and bring the WebService to the current project. By right-clicking in the Explorer, select Add Web Reference and bring up the dialog box;
3). Fill in the URL, the previously written webservice the address shown above the browser, click the "Go" button, for example, will show the referenced webservice can be called method, and then click "Add Reference", The webservice is referenced to the current project, for example, the solution will appear in the introduction of the WebService file
Here's a look at the code:
Default.aspx:
1<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
2<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
345 <title> Untitled Page </title>
67<body>
8 <form id= "Form1" runat= "Server" >
9 <div>
Ten <asp:textbox id= "NUM1" runat= "Server" ></asp:TextBox>
<select id= "Selectoper" runat= "Server" >
<option>+</option>
<option>-</option>
</select>
<asp:textbox id= "Num2" runat= "Server" ></asp:TextBox>
<span id = "E" runat = "server" ></span>
<asp:button id= "BTN" runat= "server" text= "=" onclick= "Btn_click"/>
<asp:textbox id= "Result" runat= "Server" ></asp:TextBox>
19
</div>
</form>
22</body>
2324
Background Default.apsx.cs file:
1using System;
2using System.Data;
3using System.Configuration;
4using system.web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10
11public Partial class _default:system.web.ui.page
12{
protected void Page_Load (object sender, EventArgs e)
14{
15
16}
protected void Btn_click (object sender, EventArgs e)
18{
if (num1.text!=null&&num2.text!=null)
20{
+ localhost. Service objservice=new localhost. Service ();
int i = Selectoper.selectedindex;
Switch (i)
24{
Case 0:
Result.text = Objservice.addition (double. Parse (Num1.text), double. Parse (Num2.text)). ToString ();
break;
Case 1:
Result.text = Objservice.qiucha (double. Parse (Num1.text), double. Parse (Num2.text)). ToString ();
break;
31}
32}
33}
34}
35
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.
Note: 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 the WebService program must be started when running the program, otherwise it will report an exception that cannot connect to the remote servers.
(Reference from: http://www.cnblogs.com/zhangzheny/archive/2007/06/16/785734.html)
(Turn) A small demo of WebService