Introduction:

This article we will explain how to create a web service in ASP.NET.

Description:
What is Web Service
A "web service is the communication platform between two different or same platform applications that allows to use their web method."
Once a web service is created and hosted on the server in the internet it can be consumed by any kind of application developed in any technology.

How to create a Web Service
Let us implement create a sample Asp.Net Application and add web service in ASP.Net application. Open Visual Studio and click on  File menu=> New=> Project.

How to create webservice in asp.net

In the following figure select “Templates”, Visual C#, then ASP.NET Web Application, and here I give the name of project “webservice”. You can give the project name as you wish.
And then click onOKbutton

How to create webservice in asp.net

Now right click on project name “webservice” to add webservice like as  following figure.

How to create webservice in asp.net

Choose the Web Service template and Enter the name (for example: MyWebService.asmx) then click on "Add".

How to create webservice in asp.net


This will create the following two files

.How to create webservice in asp.net

Open the file MyWebService.asmx.cs and write the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
 
namespace webservice
{
    /// <summary>
    /// Summary description for MyWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]   
    public class MyWebService : System.Web.Services.WebService
    {
 
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public int Add(int x, int y)
        {
            return x + y;
        }
        [WebMethod]
        public int Sub(int x, int y)
        {
            return x - y;
        }
        [WebMethod]
        public int Mul(int x, int y)
        {
            return x * y;
        }
        [WebMethod]
        public int Div(int x, int y)
        {
            return x / y;
        }      }
}
 

Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service. You can also use the properties of this attribute to further configure the behavior of the XML Web service method. The WebMethod attribute provides the following properties


  • 1. BufferResponse
  • 2. CacheDuration
  • 3. Description
  • 4. EnableSession
  • 5. MessageName
  • 6. TransactionOption

To see whether the service is running correctly go to the Solution Explorer then open " MyWebService.asmx " and run your application.

Now you will find all the method names in the browser. And click to add method name.

How to create webservice in asp.net

Now you will see two TextBoxes for checking. Enter the value for x and y and click on the "Invoke" button.


How to create webservice in asp.net


Now you will see the result in an open standard form (XML).

 How to create webservice in asp.net

 

I hope it will help you after reading.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like