Microsoft KB Archive/308359

= HOW TO: Write a Simple Web Service by Using Visual C# .NET =

Article ID: 308359

Article Last Modified on 9/15/2006

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Web Services Enhancements for Microsoft .NET 1.1
 * Microsoft Web Services Enhancements for Microsoft .NET 2.0
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q308359



For a Microsoft Visual Basic .NET version of this article, see 301273.

IN THIS TASK
SUMMARY
 * Requirements
 * Write a Simple .asmx Web Service
 * Consume a Web Service

REFERENCES



SUMMARY
This step-by-step article shows you how to write a simple Web service, called MathService, that exposes methods for adding, subtracting, dividing, and multiplying two numbers.

back to the top

Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft Internet Information Server 4.0 or Internet Information Server 5.0
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * How to use the Visual Studio .NET integrated development environment

back to the top

Write a Simple .asmx Web Service
 Open Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click ASP.NET Web Service under Templates. Type MathService in the Location text box to change the default name (WebService1) to MathService. Change the name of the default Web service that is created from Service1.asmx to MathService.asmx . Click Click here to switch to code view in the designer environment to switch to code view.  Define methods that encapsulate the functionality of your service. Each method that will be exposed from the service must be flagged with a WebMethod attribute in front of it. Without this attribute, the method will not be exposed from the service.

NOTE: Not every method needs to have the WebMethod attribute. It is useful to hide some implementation details called by public Web service methods or for the case in which the WebService class is also used in local applications. A local application can use any public class, but only WebMethod methods will be remotely accessible as Web services.

Add the following method to the MathServices class that you just created: [WebMethod] public int Add(int a, int b) { return(a + b); }

[WebMethod] public System.Single Subtract(System.Single A, System.Single B) { return (A - B); }

[WebMethod] public System.Single Multiply(System.Single A, System.Single B) { return A * B; }

[WebMethod] public System.Single Divide(System.Single A, System.Single B) { if(B == 0) return -1; return Convert.ToSingle(A / B); }                    Click Build on the Build menu to build the Web service. Browse to the MathService.asmx Web service page to test the Web service. If you set the local computer to host the page, the URL is http://localhost/MathService/MathService.asmx.

The ASP.NET runtime returns a Web Service Help Page that describes the Web service. This page also enables you to test different Web service methods.</li></ol>

back to the top

Consume a Web Service
<ol> Open Visual Studio .NET.</li> Under Project types click Visual C# Projects, then click Console Application under Templates.</li> Add a reference for the MathService Web service to the new console application.

This step creates a proxy class on the client computer. After the proxy class exists, you can create objects based on the class. Each method call that is made with the object then goes out to the uniform resource identifier (URI) of the Web service (usually as a SOAP request). <ol style="list-style-type: lower-alpha;"> On the Project menu, click Add Web Reference.</li> In the Add Web Reference dialog box, type the URL for the Web service in the Address text box and press ENTER. If you set the local computer to host the Web service, the URL is http://localhost/MathService/MathService.asmx.</li> Click Add Reference. Alternatively, you can type the URL to the discovery file (MathService.vsdisco) or click Web References on Local Web Server in the left pane to select the MathService service from the list.</li> Expand the Web References section of Solution Explorer and note the namespace that was used.</li></ol> </li>  Create an instance of the proxy object that was created. Place the following code in the function called Main: localhost.Service1 myMathService = new localhost.Service1; </li>  Invoke a method on the proxy object that you created in the previous step, as follows: Console.Write(&quot;2 + 4 = {0}&quot;, myMathService.Add(2,4)); </li> Click Build on the Build menu to build the console application.</li> Click Start on the Debug menu to test the application.</li> Close and save the project.</li></ol>

back to the top

<div class="references_section">