(1) create a WebService Application
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:
Using System; Using System. LINQ; Using System. Web; Using System. Web. Services; Using System. Web. Services. Protocols; Using System. xml. LINQ; [WebService (namespace =" Http://tempuri.org/ ")] [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream. // [System. Web. Script. Services. scriptservice] Public Class Service: system. Web. Services. WebService { Public Service (){ // If you use the designed component, uncomment the following line // Initializecomponent (); } [Webmethod] Public String Helloworld (){ Return " Hello World ";}}
You can directly run service. asmx to view the effect as follows:
Click it to call the default helloworld method and return an XML result, indicating that our simple WebService environment is normal.
Now, we manually add a simple Summation Method to the service. CS class. The Code is as follows:
Using System; Using System. LINQ; Using System. Web; Using System. Web. Services; Using System. Web. Services. Protocols; Using System. xml. LINQ; [WebService (namespace =" Http://tempuri.org/ ")] [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream. // [System. Web. Script. Services. scriptservice] Public Class Service: system. Web. Services. WebService { Public Service (){ // If you use the designed component, uncomment the following line // Initializecomponent (); } [Webmethod] Public String Helloworld (){ Return " Hello World ";} [Webmethod (description ="Sum of two integers ")] Public Int Sum ( Int A, Int B ){ Return A + B ;}}
Note that if we write a function and want it to become an externally callable interface 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.
NOTE 1: The description = "function description" section can be omitted without affecting the method to be called. However, a brief introduction of the method is usually provided.
Note 2: The service class can also contain methods that are not called by the outside world. Do not confuse them.
The effect after running is as follows:
Click sum:
After the values of A and B are input and called, The results page in XML format is returned.
Now, a simple WebService application has been created. You can call it by publishing it on the server.
(2) WebService application call
Create a winform ApplicationProgramOr ASP. NET applications, WebService calling is very simple, steps and points are as follows:
-
- Publish the created web application on the server. For example, configure the new web service on the local IIS ,:
-
- Right-click the project in resource manager, select Add web reference, find the target service, and click Add application.
-
- After successful addition, in the project:
- You must instantiate the Web service before calling the web method. in this example, the background code calls the web method as follows:
Public partial class _ default: system. web. UI. page { protected void page_load ( Object sender, eventargs e) {btndo. click + = New eventhandler (btndo_click) ;}< span style = "color: # 0000ff; "> void btndo_click ( Object sender, eventargs e) {localhost. service S = New localhost. service (); txtresult. TEXT = S. sum (convert. toint32 (txtnum1.text), convert. toint32 (txtnum2.text )). tostring () ;}