Creating your first WebService using .NET
Now that we have learnt some basic stuff about about what web services are and how they communicate,lets build our first webservice using Visual Studio.I have done this in VS 2008.
First Step: Open Visual Studio.Click on File and then select “New WebSite”.From the Next Window that appears,click ASP>NET Web Service.Select your language and then click OK.
By Default,VS 2008 will automatically create your first WebService “Hello World”.Notice that the Class inherits the System.Web.Services.WebService Class.
public class Service : System.Web.Services.WebService
Also,before the method “Hello World”,a [WebMethod] attributed has been specified.This basically tells the compiller to expose the method.
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
Attaching the WebMethod attribute to the method indicates that you want to expose this method as part of the XML Web Service.To further configure the behavior of the method,we can make use of the properties in the WebMethod attribute.See the different properties of the WebMethod Attribute
Now if you run the project,you will get the following.
Now if we select the HelloWorld method,ypu will get the next screen that allows you to invoke the method.On invoking the method,you will get the XML output of the method ‘HelloWorld’.
Now lets just create a more meaningful Method to be exposed and make use of the properties of the WebMethod attribute I am creating a Class named Product and i will output the Product Class object.Here is the Class
public class Product
{
public Product()
{
//
// TODO: Add constructor logic here
//
}
public string productName;
public string productPrice;
}
Now the Web Service Method for getting ProductDetails.I have added the EnableSession and Description Properties to the WebMethod attribute.
[WebMethod(EnableSession=true,Description="Gets the Product Details")]
public Product getProductDetails()
{
Product p = new Product();
p.productName = "xxx";
p.productPrice = "$12.00";
return p;
}
The Output on invoking this method will be
Now suppose you want to change the display Xml node to something other than productName and productPrice.For that use the System.Xml.Serialization namespace and do the following.
public class Product
{
public Product()
{
//
// TODO: Add constructor logic here
//
}
[XmlElement(ElementName="Name")]
public string productName;
[XmlElement(ElementName="Price")]
public string productPrice;
}
Now,instead of showing the node name as productName and productPrice,it will be shown as Name and Price.
Like this there are many Classes in the Xml.Serialization namespace that can be used to enhance the output of your Webservice.
Now having created the WebService,lets build a simple client application that uses this WebService.The easiest way is to add a Web Reference to your project.So create a new WebSite or any application.Right click the Solution Explorer and select Add Web Reference. Now on the next dialog box,you will be asked to provide the URL of the WebService.Provide that and also give a name for the reference.Suppose you name ws.3 files will be created.A discoveryfile disco,discovery map file .discoMap and a wsdl file.
Now to access the webservice you will have to create an object of the same.
ws.Service obj=new ws.Service();
To access the method getProductDetails(),create an object of Product and call the method,just like calling a simple method
ws.Product p=new ws.Product(); p=obj.getProductDetails();
That completes our first Tutorial to build a simple XML WebService using .Net.I will come up with more on Web Services in the future.Hope you will enjoy Web Service Programming.
Related posts:











Leave your response!