[Original] ASP. NET web service creation, deployment and use

Source: Internet
Author: User

 

 

 

PS: Development Tool vs2010. All projects are in debug state. I just got in touch with web service. This article is the entry-level use case for cainiao. Don't laugh!

Reprinted please indicate the source

1. Create a Web Service Project

1. Create a web service project named webserviceproject

File-> New-> project --> Visual C #-> Web-> Asp. NET Web Service Application

Note: the. NET Framework version is 3.5 and 4.0 does not contain this type of project.

 

2. In webserviceproject, delete the original helloworld method in the servie1 class and add a reversestring method.

Code

         [WebMethod]
public string ReverseString(string s)
{
System.Threading.Thread.Sleep(5000);
char[] a = s.ToCharArray();
Array.Reverse(a);
return new string(a);
}

You must add the [webmethod] Attribute before the method.

Sleep (5000) In the first line of the method to demonstrate the differences between synchronous call and asynchronous call of methods in Web Service

 

Add [WebService (namespace = "http://tempuri.org/")]

Change to [WebService (namespace = "http: // webservicesample/")] by name.

You can leave it unchanged. If you do not change it, a prompt will be prompted when you view it in the browser (you can check it yourself)

 

3. Compile and test the webserviceproject

Compile the project by pressing F7 and view servic1.asmx in the browser

 

Because there is only one method in the project, the page is displayed as follows:

Click reversestring To Go To The test page of the method for testing.

 

 

2. Deploy Web Services

1. Release the project to a local directory (D: \ webservicesample)

 


2. After the release, add a virtual directory (or application) pointing to the directory in IIS)

3. Check in the browser to test whether the release is successful.

Http: // localhost/webservicesample/service1.asmx

The page display should be the same as that in the previous section

 

3. Use Web Service

1. Use the WSDL tool to generate a proxy class of servie1 in the Web Service

Open the vs2010 naming line tool

 

Enter the following name and generate a myproxyclass. CS file in D: \, which contains a proxy class.

Public partial class service1: system. Web. Services. Protocols. soaphttpclientprotocol

For details about how to generate proxy classes, see

Http://msdn.microsoft.com/zh-cn/library/7h3ystb6.aspx 

 

 

2. Create a Windows form application to use web service. The project name is webserviceclient.

Add the myproxyclass. CS file generated in step 1 to the Project

Add system. Web. Services reference: Project-> Add reference

 

3. Drag several controls on form1

 

4. Add an event response function for the button

 // Synchronize
Private void button#click (Object sender, eventargs E)
{
Service1 Ws = new service1 ();
Textbox2.text = ws. reversestring (textbox1.text );
}

// Asynchronous
Private void button2_click (Object sender, eventargs E)
{
Service1 Ws = new service1 ();
WS. reversestringcompleted + = new reversestringcompletedeventhandler (reversestringcompleted );
WS. reversestringasync (textbox1.text );
}

Private void reversestringcompleted (Object sender, reversestringcompletedeventargs E)
{
Textbox2.text = E. result;
}

 

5. Test the program Effect

When Synchronous Response is used, the program cannot respond before the method in the Web service ends.

When asynchronous response is used, the program can respond before the Web service method ends.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.