Simple Introduction to web services in ASP.net C#


1.       What is a web service:
A web service allows a web site(web application) to expose programmatic functionality via the Internet. Web services can accept messages and optionally return replies to those messages.


in our case what we are doing is we create a web service and put it in a local web-server like IIS. Then we create a website that can request something from that web service.lets simply read time or some kind of string from web server.


1.       Getting familiar with web service..
Creating the web service…
simply go to new project –> web –> Asp.net web service application and give the project name as you want.. in my case im using myWebservice as my project name..
1.       In Solution Explorer you will see something like this..

Note that we don’t have something like ASPX..

Thing we have here is asmx that is the extension of service file..
that means Service1.asmx is the name of our service and all the  service based coding   will be written in Service1.asmx.cs
later you will we able to able to create data-driven web-services,  complex requests ,, almost anything that web server support ..but for now we will be going to create a web-service that respond to time request
1.  Now Without doing anything Just run the project..
 then you will get something like this

 

That means in Service1  have a method called HelloWorld(). Which we can access via our program (website) this is a simple method generated by visual studio. you don’t need this method in order to develop your service. if you don’t need to simply you can delete it. but for now im going to keep it.
Note that http://tempuri.org/ is the place we have place our web service.. 
. now if you click the HellowWorld link you will see a thing like this..
 by clicking you can test this method. it will show you what kind a respond you will receive from web-service. in IE you may get a XML respond. if you use Firefox you will show the filtered message.
note that web servers send reply by using XML in order to enable communication between different type of clients. because  ASP , JSP, java ,C# or any language based applications may be requesting resources from web-service. so in order to serve to different type of clients web services use XML based responds.
after you click the invoke button you will see something like this
 2nd one is the url of the service’s reply which we gets after clicking the invoke button.
Code behind about the things we discussed so far..

doble click and open Service1. then you will see the code behind of service1.asmx


1.       to define where the webservice is located on we use this code segment
 [WebService(Namespace = http://tempuri.org/”)]
We write our methods inside public class Service1: System.Web.Services.WebServices class
Which I have marked in Blue Color
        [WebMethod]
        public string HelloWorld()
        {  return “HellowWorld”;
        }

              
       Notice if you want to add a web method you must define your method   after [WebMethod] tag.. else it will not available to client-side ..
Simply write a method after HelloWorld() bt don’t add the tag [WebMethod] before writing it.. then run your program.. check u can see it after method hellow world? No you cant because server think it should only available to service1 not to the clients..
Then add the tag [WebMethod] above your method and run your project again..
Then you will see your method is redy to be available  clients to acsess..
Ok lets get Rollin with that part I will show you how I did it..

look at pictures its very easy to understand..

 

 
HelloWorld()  is .net generated simple method that returns string “HellowWorld”.. 
 
 
 
 
 
 
 
 
 
 
 
and TellMeServerTime( ) is the method that i created. it will respond to the request with a string type  that sends the server’s time..

ok now run your project and then you will see something like this.. if you dont get the newly created method called  TellMeServerTime() rebuild your project and run again..you must get something like this..
now click on TellMeServerTime  then browser will let you to test your method as follows

if you invoke the method several times you will notice the out put is changing each time.. 

1.      Its time to roll with a real world application
First we need to add a web form to our project that is the place where we going to call our web service output
            simply go to add –> new item –> select Web Form and give a name
In my case its MyWebForm.aspx .. 
Then right click on MyWebForm.aspx and select “Set as Start Page”
Now if you run the project you will see nothing bt a empty web page.. Because MyWebForm.aspx is empty
1.        Adding Web Service’s reference to Project…..
 we need to add service1’s reference to our web project in order to use the functionality of the web-service. to do the.. all you need to do is just right click on the project in solution explorer and click add service references.  
then follow the instructions as below picture explains..

Now you must have something like this .. that means we have added service reference to our project..
so that we can create instances/ objects from Service1 and use it…

note that there is a another way you can add this reference.
you can do this by adding web reference instead of adding service reference. both way you will be able to call the function we implemented in Service1.asmx

 
1.      Requesting  server time from  web server via our web form.
To do that im going to need a label and a button..
(Label1 and Button1 in this scenario..)
when you click the button the label will show the time from server..  to do this double click the button and add the following code inside Button1 click event
      Service1 myTimeService = new Service1();
      Label1.Text= myTimeService.TellMeServerTime();
it should look like this..
 

 now u have set everything.. all you need to do is hit the run button..
if you have done everything correctly you may get a out put like this,..

note that each time you click on the button webform is requesting the time from server..
when server respond to that method .net will rip the XML output and get the exact value you need..
simply you don’t need to worry about XML out put you are getting from the web service. Visual Studio will handle the XML part for your convenience

 ok now i think you must have a simple but overall idea about how web services can be applied to our projects.

 Here is another great example for data driven web service application
http://www.developerfusion.com/article/3114/building-xml-web-services-using-c-and-aspnet/2/



Advertisements

2 thoughts on “Simple Introduction to web services in ASP.net C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s