Age To Days Web Service with Example in ASP.NET

 

Age To Days Web Service with Example in ASP.NET

Background

In this article, we will learn about web service using the scenario when Our applications often require code to determine the number of days, such as how long the customer is associated with us, also to convert from date of present days into days or years and so on.

In a normal application, I need to write the Business logic repeatedly for the same requirements so due to the requirements you can write single web service for Multiple applications that allow an access method on any platform used, so let us start with the basics.

What is a web service?

A “web service is the communication platform between two different or same platform applications that allows using 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 basics about it.

What do the different or same applications and platforms 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 the web method mean?

The method in web services always starts with [webMethod] attributes, which means that it is a web method that is accessible anywhere, the same as my web application.

To understand more clear let us represent all the above-given definitions explanation in the following diagram

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 well as java or other applications and you can also use .Net based web application in Java applications means web Services don’t have 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.Framework 2010 as you see in 2008 while adding a project or website it might be because of WCF.

So let us start using a different way to add a web service using a template

Start → All Programs -> Microsoft Visual Studio 2012

Microsoft Visual Studio 2012 -> New -> Project

New Project -> Web -> Empty Web Application (.NET Framework 3.5)

Provide the website a name such as “agetodays” or another as you wish and specify the location

Click the ok button home page

Then right-click on Solution Explorer -> add -> New Item

Then click on Web -> web service templates

Select Web Service Template and click on add button. Then after that, the Solution Explorer looks as follows.

Then open the Webservice class and write the following method followed by [webMethod] attribute as in.

[WebMethod]
public int converttodaysweb(int day, int month, int year) {
DateTime dt = new DateTime(year, month, day);
int datetodays = DateTime.Now.Subtract(dt).Days;
return datetodays;
}

In the code above I have declared 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 assigned 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 web service file will then look as in the following

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
///<summary>
/// Summary description for UtilityWebService
///</summary>
[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 WebService: System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int converttodaysweb(int day, int month, int year)
{
DateTime dt = new DateTime(year, month, day);
int datetodays = DateTime.Now.Subtract(dt).Days;
return datetodays;
}
}

Now run the application that looks as follows.

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

The output will be as follows

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

Note

  • For Github code please download the zip file attached above.

Summary

From all the examples above we see how to convert a date into days. In my next article we will learn how to implement this web service in a web application so click here to learn Consuming Web Service In an ASP.Net Web Application. I hope this article is useful for all students and beginners. If you have any suggestions related to this article then please contact me.

Post a Comment

0 Comments