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 topRequirements
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
topXML 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
topBuild 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. - Add the following code to the Service1 class to create a new function:
public double PythagoreanTheorem(double a ,double b)
{
}
- 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);
- 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);
}
- On the Build menu, click Build
Solution to compile the Web service.
back to the
topUse 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.
- 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. - In the Add Web Reference dialog box, in
the Address box, type http://localhost/PythagoreanTheoremWS/Service1.asmx, and then click Go.
- In the Add Web Reference dialog box, click
Add Reference.
- On the Project menu, click
Code. The Code window of Class1.cs is displayed.
- 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();
- On the Debug menu, click
Start to run the application. The following output is
displayed in the Console window:
5
13
25
back to the
topComplete Code Listing
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>
/// Summary description for Service1.
/// </summary>
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;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
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 "Hello World";
// }
[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>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[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