Development, configuration, and call-three steps to build a simple Asp.net WebService (C #)-suitable for beginners

Source: Internet
Author: User
Http://www.cnblogs.com/farrah/archive/2009/04/03/1429099.html

 

Two days ago, I encountered a need to develop a simple WebService

I searched a lot on the Internet.ArticleBut there are not many of them. In particular, it is difficult to find examples of remote calls.

After completing this, I decided to write a blog, from development, configuration to remote calling. I wrote an example step by step for your reference.

 

1. Development.

 

Open visual studio.net 2008, create a project, select ASP. NET WebService, and name it webservicesample

The system automatically generates a helloworld

[Webmethod]
Public   String Helloworld ()
{
Return   " Hello World " ;
}

The preceding "[webmethod]" indicates that this method can be called remotely.

Only public functions can be marked as "[webmethod]".

Below we will write a simple addition function:

Code
[Webmethod]
Public   Int Myadd ( Int I, Int J)
{
Return I + J;
}

Now press the debug button to display a dialog box. Press enter to display a webpage with two links at the top, helloworld and myadd.

Click "myadd". You can see a debugger. You can debug the myadd function by entering the I and j parameters.

It should be noted that if debugging is successful, debugging in IIS may fail for various reasons, because local debugging and remote debugging are different in many aspects, for example, access permissions for local files.

During remote debugging, WebService is the same as a general ASP. NET Website. Users can only access the files under wwwroot.

At the bottom of this page, there are three methods to remotely call this WebService and their respective return statements.

When these three methods are called, they only change the content sent and received. The methods recommended by Microsoft are the above two.

So far, the most simple WebService has been developed

 

2. Configuration

The simplest configuration method:

Copy the Web. config, service1.asmx, and bin directories under the Development Directory (you only need to copy webservicesample. dll below) to the C: \ Inetpub \ wwwroot \ folder.

If the local IIS service is not installed or enabled, install or enable the IIS service in the control panel.

If you enter http: // localhost/service1.asmx in the browser and the page appears during debugging, the configuration is successful.

 

3. Remote Call

The local call is not a remote call. However, remote calls are to distinguish between calling DLL directly and using HTTP. In fact, the HTTP method is the same, whether it is accessing localhost on the local machine or accessing the local IP address on another machine.

On the debugging page, there are three call methods. The following uses the third method as an example to show how to callCode.

 

Send:
Post / Service1.asmx / Myadd HTTP / 1.1
HOST: localhost
Content - Type: Application / X - WWW - Form - Urlencoded
Content - Length: Length

I = string & j = string
receipt:
HTTP / 1.1 200 OK
content - type: Text / XML; charset = UTF - 8
content - length: length

<? XML version = " 1.0 " Encoding = " UTF-8 " ?>
< Int Xmlns = " Http://tempuri.org/ " > Int </ Int >

 

The Code is as follows:

 

Code
Public   Static   Int Callmyadd ( Int I, Int J)
{
Try
{
String URI =   " Http: // localhost/service1.asmx/myadd " ;
String content =   " I = "   + I. tostring () +   " & J = "   + J. tostring ();
Httpwebrequest request = (Httpwebrequest) webrequest. Create (URI );
Request. contenttype =   " Application/X-WWW-form-urlencoded " ;
Byte [] Contentbyte = Encoding. utf8.getbytes (content );
Request. contentlength = Contentbyte. length;
Request. Method =   " Post " ;
Stream contentstream = Request. getrequeststream ();
Contentstream. Write (contentbyte, 0 , Contentbyte. Length );
Request. protocolversion = Httpversion. version11;
Httpwebresponse response = (Httpwebresponse) request. getresponse ();
String Retstring = Response. statusdescription;
If (Retstring ! =   " OK " )
{
Throw   New Exception (retstring );
}
Stream responsestream = Response. getresponsestream ();
Streamreader SR =   New Streamreader (responsestream );
Retstring = Sr. readtoend ();
Xmldatadocument xdd =   New Xmldatadocument ();
Xdd. loadxml (retstring );
Int Result =   Int . Parse (xdd. childnodes [ 2 ]. Innertext );
Return Result;
}
Catch (Exception E)
{
MessageBox. Show (E. tostring ());
}

}

To use the other two methods, you must generate the XML text and modify the Content-Type value.

Http: // localhost: 3310/website1/service. asmx? WSDL

 

 

To convert a WSDL file Source code File, we will use a wsdl.exe console application Program To generate code. This tool parses the WSDL file and other additional files to generate a single source code file. The source code file contains all the classes, methods, and types required to run the web service. Wsdl.exe will be installed along with Visual Studio. NET 2003 (and. Net SDK. To use this tool, you must first open Visual Studio. NET 2003 command prompt console, by default, this item is located in the Start Menu> program> Microsoft Visual Studio.. NET 2003 Visual Studio.. Net tool. Once opened, you can manipulate command prompts in the previously saved WSDL file.

Run the following command to generate a client proxy class for wsdl.exe.

WSDL/out: temp. CS http: // localhost/vvopamp/vvopampsrv. php? WSDL

The client proxy class is saved in the temp. CS file.

 

 

Related Article

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.