Web Design that taps into the haromny and vision of your dreams.

Creating a Simple XML Web Service

How to write a simple XML web service

Written By on in C#

423 words, estimated reading time 2 minutes.

In this short tutorial, we will construct a simple XML web service using Visual Studio 2005 that will convert values to and from Centigrade and Fahrenheit. In the later tutorials, we will use this service to demonstrate how to call web services from Windows programs and ASP pages.

Introduction to Web Services Series
  1. What are XML Web Services?
  2. Creating a Simple XML Web Service
  3. Connecting C# Windows Applications to a Web Service

XML Web Services in Visual Studio 2005 are very easy to create. The .Net Framework seamlessly provides all the background work, so all you have to do is work out what you want to do with it.

Create a new XML Web Service project from within Visual Studio or Visual Web Developer and the project will provide a skeleton service that will display "Hello World".

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
  public Service () {
 
    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
  }
 
  [WebMethod]
  public string HelloWorld() {
    return "Hello World";
  }
}

You may notice that the HelloWorld method is marked with a WebMethod attribute. Only methods marked in this way will be exposed to the XML Web Service. You can create as many methods as you require and they will be hidden unless WebMethod is specified.

When creating a new XML Web Service, the standard template is a good start, but there are a few things that you should change, the first of which is the namespace URI. This should be changed to the base URL of the location of the published web service. Secondly, if your web service is going to be called from a ASP.Net application you should uncomment the System.Web.Script.Services.ScriptService line to allow AJAX commands to be transferred. You should also delete (or cannibalise) the HelloWorld method.

For these tutorials we are going to create a web service with two methods, one to convert Celsius to Fahrenheit and another to do the opposite.

The first step is to create two methods to handle the conversions.

[WebMethod]
public double toCelsius(string Fahrenheit)
{
  double degrees = System.Double.Parse(Fahrenheit);
  return (degrees - 32) * 5 / 9;
}
 
[WebMethod]
public double toFahrenheit(string Celsius)
{
  double degrees = System.Double.Parse(Celsius);
  return (degrees * 9 / 5) + 32;
}
Creating a Simple XML Web Service
Creating a Simple XML Web Service

And that's it! You can compile and run this web service now and the compiler will launch your web browser pointed at the services landing page. From there you can invoke the methods. When you develop your own web services don't forget to validate user input. This has not been done here in order to keep the methods as simple as possible and to avoid confusion.

Last updated on: Friday 23rd June 2017

 

Comments

There are no comments for this post. Be the first!

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me