Motive

The reason for writing this article is that a lot of people have published too many articles on creating web services and making it simple but non is actually simple.  Almost all of those article focus on the boring theory and functionality  of the web service and not on actually what we least require to create a simple web service. Beginners to web services like me get confused in the functionality and un necessary theory. With this confusion the actual cause of creating a very simple web service dies. I have made it very simple to create a .net web service and my readers can extend this article like instead of addition they can put in what ever functionality they want in the WebMethod to get this web service work for them according to their needs.

Some Boring Theory That We Can’t Get Rid Off

  • When ever we create a web service we need some part of it to be accessed from outside. So that we can call it and make it do something for us. to make a method accessible to the outside world we need to add [WebMethod] Attribute on top of that method.
  • The method that we need to expose to the outside world must be public.
  • The file that we need to call is the one with “.asmx” extension to execute our web service.
  • We can have a private method in our web service that can be called from with in our public method. Like we can have some classes that will have some method that will do some stuff for us. To get access to that functionality of those method. We need to call those methods from our public method by creating objects of those classes in the Service1.asmx file and calling those methods through those objects.

Now Coming To The Real Stuff.

My development environment

  • Visual Studio 2010
  • Windows 7 professional 64 bit
  • Dot net framework 3.5

Creating the Web Service

Open your Visual Studio and click New Project (Section 1 in image).  From the left side panel choose C#(Section 2 in image) , Web(Section 3 in image). From the Framework selection dropdown at the top select .Net Framework 3.5 (Section 4 in image) and from the list below that select ASP.Net Web Service Application (Section 5 in image). Give the name of the web service application (Section 6 in image)  and the location (Section 7 in image) where it will be saved.  Click OK  (Section 8 in image)to create the project.

Creating a simple .net 3.5 web service application


namespace WsMirTestWebService
{
 /// <summary>
 /// Summary description for Service1
 /// </summary>
 [WebService(Namespace = "http://tempuri.org/")]
 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 [System.ComponentModel.ToolboxItem(false)]
 // 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 Service1 : System.Web.Services.WebService
 {
 [WebMethod]
 public string HelloWorld()
 {
  return "Hello World";
 }
 }
}

This is what is created for us by default. now replace the method

[WebMethod]
public string HelloWorld()
 {
  return "Hello World";
 }

with your own method that you want to call from outside. Lets say we want to send two numbers to the web service and get the sum

[WebMethod]
 public int add(int a,int b)
 {
  int c = 0;
  c = a+b;
  return c;
 }

Testing Created Web Service

Now when we run our web service, we will see Service1.asmx in brower with all our WebMethods. Currently we have only one method i.e, add.

Running our web service. Service1.asmx is displayed in browser with our WebMethod “add” on display

Clicking on the add link will take us to the second screen where we will supply two values to our WebMethod and will click the Invoke button to execute the add method.

WebMethod add executed. The method expects two values, a and b.

We provide two values 7,3 and click Invoke button. This will execute our WebMethod add and if our method is returning some value it will show us the returned value.

Returned Value of our WebMethod add.

 

http://mirnauman.wordpress.com/2012/08/15/publishing-a-dot-net-web-service-or-website/

The above link can be used to publish the created web service.

About these ads