Microsoft KB Archive/816154

= How to build and use XML Web Services by using Visual C# .NET and Visual C# 2005 =

Article ID: 816154

Article Last Modified on 11/22/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





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

IN THIS TASK

 * SUMMARY
 * Requirements
 * XML Web Services
 * Build a Web Service
 * Use the Web Service
 * Complete Code Listing
 * REFERENCES



SUMMARY
This step-by-step article describes how to create and use an XML Web service by using Visual C# .NET or Visual C# 2005.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * Microsoft Internet Information Server 4.0 or Microsoft Internet Information Services 5.0

This article assumes that you are familiar with the following topics:
 * Visual C# .NET or Visual C# 2005
 * Visual Studio .NET or Visual Studio 2005

back to the top

XML Web Services
XML Web services are reusable units of application logic that you can expose to clients across the Internet. Web services are platform-independent. Additionally, Web services are based on standards that the industry agrees upon such as Extensible Markup Language (XML), Simple Object Access Protocol (SOAP), and Hypertext Transfer Protocol (HTTP). Client applications can be any of the following:
 * Web-based ASP.NET application
 * Windows application
 * Pocket PC application
 * Mobile device application
 * Console application

XML Web services provide a new form of connectivity across your whole enterprise. Visual Studio .NET makes it easy to create and use XML Web services.

back to the top

Build a Web Service
In this section, you create an XML Web service that implements the Pythagorean theorem.  Create a new ASP.NET Web service in Visual C# .as follows:  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New and then click Project.

Note In Visual Studio 2005, click Web Site on the File menu. Under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Service.

Note In Visual Studio 2005, click ASP.NET Web Service under Templates, select Visual C# on the right of Language. In the Location box, type http://localhost/PythagoreanTheoremWS, and then click OK.

Note In Visual Studio 2005, select HTTP on the right of Location, type http://localhost/PythagoreanTheoremWS on the right of HTTP, and then click OK.  On the View menu, click Code. The Code window for Service1.asmx is displayed.

Note In Visual Studio 2005, the default file is Service.asmx.</li>  Add the following code to the Service1 class to create a new function: public double PythagoreanTheorem(double a ,double b) { } </li>  The Pythagorean theorem states that the square of the hypotenuse of a right triangle is equal to the sum of the squares of the other two sides. Add the following code inside the PythagoreanTheorem function to implement this mathematical formula: double dblSum = 0; dblSum = (a * a) + (b * b); return Math.Sqrt(dblSum); </li>  This function fully implements the Pythagorean theorem. However, the function is not yet a Web service method. To expose a function as a Web service method, add the WebMethod attribute to the method declaration. The complete function appears as follows: [WebMethod] public double PythagoreanTheorem(double a ,double b) { double dblSum = 0; dblSum = (a * a) + (b * b); return Math.Sqrt(dblSum); } </li> On the Build menu, click Build Solution to compile the Web service.</li></ol>

back to the top

Use the Web Service
In this section, you create a Windows Application that uses this Web service.  Create a new Console Application project in Visual C# .NET or in Visual C# 2005 to test the Web service that you created in the previous section.</li> To access a Web service from a client application, the client must first include a reference to the Web service.

To add a Web reference, on the Project menu, click Add Web Reference.</li> In the Add Web Reference dialog box, in the Address box, type http://localhost/PythagoreanTheoremWS/Service1.asmx, and then click Go.</li> In the Add Web Reference dialog box, click Add Reference.</li> On the Project menu, click Code. The Code window of Class1.cs is displayed.</li>  Add the following code to the Main function: double hypotenuse; localhost.Service1 ws = new localhost.Service1;

// Pythagorean Triple: 3, 4, 5 hypotenuse = ws.PythagoreanTheorem(3, 4); Console.WriteLine(hypotenuse);

// Pythagorean Triple: 5, 12, 13 hypotenuse = ws.PythagoreanTheorem(5, 12); Console.WriteLine(hypotenuse);

// Pythagorean Triple: 7, 24, 25 hypotenuse = ws.PythagoreanTheorem(7, 24); Console.WriteLine(hypotenuse);

Console.Read; </li>  On the Debug menu, click Start to run the application. The following output is displayed in the Console window: 5 13 25 </li></ol>

back to the top

Code for Web service
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services;

namespace PythagoreanTheoremWS {   ///     /// Summary description for Service1. ///    public class Service1 : System.Web.Services.WebService {       public Service1 {           //CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent; }

#region Component Designer generated code //Required by the Web Services Designer private IContainer components = null; ///        /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        private void InitializeComponent {       }

///        /// Clean up any resources being used. ///        protected override void Dispose( bool disposing ) {           if(disposing && components != null) {               components.Dispose; }           base.Dispose(disposing); }       #endregion

// WEB SERVICE EXAMPLE // The HelloWorld example service returns the string Hello World // To build, uncomment the following lines then save and build the project // To test this web service, press F5

//     [WebMethod] //     public string HelloWorld //     { //          return &quot;Hello World&quot;; //     }        [WebMethod] public double PythagoreanTheorem(double a ,double b)       { double dblSum = 0; dblSum = (a * a) + (b * b); return Math.Sqrt(dblSum); }

} } Note The code generated in Visual Studio 2005 is different from the code in Visual Studio .NET.

Code for Client Application
using System;

namespace UsingPytho {   ///     /// Summary description for Class1. ///    class Class1 {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           double hypotenuse; localhost.Service1 ws = new localhost.Service1;

// Pythagorean Triple: 3, 4, 5 hypotenuse = ws.PythagoreanTheorem(3, 4); Console.WriteLine(hypotenuse);

// Pythagorean Triple: 5, 12, 13 hypotenuse = ws.PythagoreanTheorem(5, 12); Console.WriteLine(hypotenuse);

// Pythagorean Triple: 7, 24, 25 hypotenuse = ws.PythagoreanTheorem(7, 24); Console.WriteLine(hypotenuse);

Console.Read;

}   } } back to the top

<div class="references_section">