Introduction
In this post we will learn about how to create a Web Service in asp.net using Visual Studio 2015. How to run service project and also detail about the basic terms used in Service like WebMethod etc.
GetAge method will return age in year and accept the datetime type of input variable as a parameter.
4. Build Project and right click on your asmx page and choose Setas StartPage and Run the Application.
You will find the similar type of screen in front of you after running the application. And its all done, you have successfully created a Web Service just in 4 simple steps.
Test Web Service
Click on the method which created earlier in my example it is GetAge
As we can see i used Dob as parameter name in my WebMethod and in the above image it is clearly shown field name Dob. Enter any Dob in DD/MM/YY format and click on Invoke Button.
Here is the output given by the web service which is 21 conversion of given DOB.
You can add as many methods you want in this service and use these wherever you want in any application by giving your service reference. In my next article i will explain you how to consume this web service.
Description
VS 2015 doesn't allows you to create a Web Service project like VS 2010, 12 etc. Microsoft update its VS-15 with new features . Peoples are very rarely use the web service in today's modern world. Because we have a Web Api concept also WCF service.
But many times you only have a small service requirement ,for these situation this post will be the best understanding explanation for you about the service creation in c# asp.net.
As we already talked about Web Service basics in previous update about service.
But many times you only have a small service requirement ,for these situation this post will be the best understanding explanation for you about the service creation in c# asp.net.
As we already talked about Web Service basics in previous update about service.
Practice
Here we create a Web Service in 4 simple steps.
1. Open VS2015 and Create a New WebSite . Select an Empty Website template ,give a suitable name to it ( in my example its name DemoWebService ) and click OK.
2. After creating new WebSite goto Solution Explorer , select Website Name and right click, select Add New Item and select WebService type page. give a suitable name to this . Here i named this TestService.
After adding webService page your code looks like this
3. Remove given Web Method if you don't want to use this and create your own method based upon your requirement. Here i create a AgeCalulator method named GetAge.
[WebMethod]
public string GetAge(DateTime Dob)
{
DateTime PresentYear = DateTime.Now;
TimeSpan ts = PresentYear - Dob;
int Age = ts.Days / 365;
return Age.ToString();
}
|
GetAge method will return age in year and accept the datetime type of input variable as a parameter.
4. Build Project and right click on your asmx page and choose Setas StartPage and Run the Application.
You will find the similar type of screen in front of you after running the application. And its all done, you have successfully created a Web Service just in 4 simple steps.
Test Web Service
Click on the method which created earlier in my example it is GetAge
As we can see i used Dob as parameter name in my WebMethod and in the above image it is clearly shown field name Dob. Enter any Dob in DD/MM/YY format and click on Invoke Button.
Here is the output given by the web service which is 21 conversion of given DOB.
You can add as many methods you want in this service and use these wherever you want in any application by giving your service reference. In my next article i will explain you how to consume this web service.
Points To Remember :-
- Web Service is a communication platform between two same or different platform application.
- Method in Web service always start with Web-Method attributes which tells about it is a web method which is accessible from anywhere in application.
0 comments:
Post a Comment