Jump to content

XML - Managing Data Exchange/Web Services/Appendix

From Wikibooks, open books for an open world

Appendix

[edit | edit source]

A .NET Perspective

Now we will create a simple .NET web service using C# and ASP.NET. This web service will provide two methods for it's clients. One will be an adding service and the other will be a subtracting service. Both methods are marked with the [WebMethod] attribute in the following code snippet.

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
	#region Construction

	/// <summary>
	/// Default Construction
	/// </summary>
	public Service () {

	}

	#endregion

	#region Operations

	/// <summary>
	/// Add a values passed in and return the sum.
	/// </summary>
	/// <param name="values">All values to be added together.</param>
	/// <returns>Sum of all values passed in.</returns>
	[WebMethod]
	public double Add(double numOne, double numTwo)
	{
		return numOne + numTwo;
	}

	/// <summary>
	/// Return the difference between numOne and numTow
	/// </summary>
	/// <returns>The difference between numOne and numTwo</returns>
	[WebMethod]
	public double Subtract(double numOne, double numTwo)
	{
		return numOne - numTwo;
	}

	#endregion

}

When executing the web service in a debug mode using Visual Studio 2005, we will get the following screen. Note the two methods available for this web service.

Exhibit 1: .Net

If we click on the add link, we will get the following page.

Exhibit 2: .Net

On this page, we will use the add method and enter our parameters for our web service call. For this example, we will enter 10 as the first number and 5 as the second number. After entering numbers and pressing the invoke button, the client will receive the following XML stream containing the sum value of 15.

 <?xml version="1.0" encoding="utf-8" ?> 
 <double xmlns="http://tempuri.org/">15</double> 

Technologies Used

  • Visual Studio 2005 Beta 2
  • .NET Framework 2.0
  • ASP.NET 2.0

The example provided is very simple and does not come close to demonstrating the power of .NET Web Services. For more information on implementing .NET Web Services, please visit http://msdn.microsoft.com/webservices/.