Thursday, 2 November 2017

Introduction to Web Service with Example in ASP.NET

What is web services ?
A "web service is the communication platform between two different or same platform applications that allows to use their web method."

In the preceding definition you observed that I used the two main points in the definition of web service; they are different or same platform application and the second is web method.

so let us learn some basic about it .

What does different or same application and platform mean
It means that I can create a web service in any language, such as Java or other languages and that the language web service can be used in a .Net based application and also a .Net web service or in another application to exchange the information. 

What does web method mean
The method in web services always start with [webMethod] attributes, it means that it is a web method that is accessible anywhere, the same as my web application.
to understand more clear let us ,I represent all above given definitions explanation in following diagram
webserviceuses.jpg
In the above diagram,I have shown,how the Asp.net Web Service is used in different types of applications means i am trying to explain that I can create a web service in any language, such as Java or other languages and that the language web service can be used in a .Net based application as wel as java or other applications and you can also use .Net based web application in Java applications means web Services dont have a any platform restrictions.
I hope you understand the basics of a web service.
So let us start to create the web service.

Note
If you closely observe that ,there is no separate web service template in .Framework 2010 as you see in 2008 while adding a project or web site it might be because of WCF.

So let us start using a different way to add a web service using a template
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010"
  2. "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page)
  3. Provide the web site a name such as "agetodays" or another as you wish and specify the location
  4. Then right-click on Solution Explorer - "Add New Item" - you see the web service templates
webservicetag.jpg
Select Web Service Template and click on add button. then after that the Solution Explorer look like as follows.

solutionofwebservce.png


Then open the Webservice.cs class and write the following method followed by [webMethod] attribute as in.
  1. [WebMethod]  
  2. public int converttodaysweb(int day, int month, int year) {  
  3.     DateTime dt = new DateTime(year, month, day);  
  4.     int datetodays = DateTime.Now.Subtract(dt).Days;  
  5.     return datetodays;  
  6. }  
In the code above I have declared a one integer method named converttodaysweb with the three parameters day, month and year for accepting day, month and year from the user.

Then after that I created an object of date time and ed the those variables that I get from the users. I declared another variable in the method that is age today to store the number of days remaining from the user's input date to the current date and finally I return that variable..

The webservice.cs file will then look as in the following
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.Services;  
  5. ///<summary>  
  6. /// Summary description for UtilityWebService  
  7. ///</summary>  
  8. [WebService(Namespace = "http://tempuri.org/")]  
  9. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  10. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  11. // [System.Web.Script.Services.ScriptService]  
  12. public class WebService: System.Web.Services.WebService   
  13. {  
  14.     public WebService()   
  15.     {  
  16.         //Uncomment the following line if using designed components   
  17.         //InitializeComponent();   
  18.     }  
  19.     [WebMethod]  
  20.     public int converttodaysweb(int day, int month, int year)  
  21.     {  
  22.         DateTime dt = new DateTime(year, month, day);  
  23.         int datetodays = DateTime.Now.Subtract(dt).Days;  
  24.         return datetodays;  
  25.   
  26.     }  
  27.   
  28. }  
Now run the application that look like as follows.

wsdl.png


Now in the above we see our method that we are created in the webservice.cs file, so click on that method and provide input values and click on the "invoke" link as in.

input.png

The output will be as follows

output.png

In the screen above you see that the output is 8671,that is the days from the input date.

No comments:

Post a Comment