Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to create and use a Web Service in ASP.NET Visual Studio 2010

Author: Alanna Kremer

Web service is a way to use web applications on the internet. Web service is created first and then it is used or consumed.

What is Web Service?

Web Service is an application that is designed to interact with other applications on the internet. Web Services allow a server to expose its functionality that clients can utilize. Web services can publish their methods or messages to the world and these methods, when invoked, perform some action and return some data or result.

Web services allow different applications from different sources to communicate with each other. As all communication in a web service is in XML so it is independent of language, platform and protocol.
 
Create a Web Service

To create a web service in visual studio 2010, follow these steps:
  1. Open New Project
  2. Select .NET Framework 3.5
  3. Select ASP.NET Web Service Application in web either in Visual C# or in Visual Basic
  4. A HelloWorld web method is already in the Service1 class. Write another web method in class Service1 as below

    C#

    [WebMethod]
    public int AddNumbers(int firstNum, int secondNum) {
                int thirdnum = firstNum + secondNum;
                return thirdnum;
     }
     
    VB.NET

    <WebMethod()> _
    Public Function AddNumbers(ByVal firstNum As Integer, ByVal secondNum As Integer)   As Integer
                Dim thirdnum As Integer = firstNum + secondNum
                Return thirdnum
    End Function
     
  5. See web service in browser and copy the URL to add reference of the service when you have to use the service. Select Web Method to see result in XML.

 

Use Web Service

 
When a web service is created the next step is to use this service. It is also called consuming the Web Service.
 
To use a web service, follow these steps in visual studio 2010:
 
  1. Open New Web Site
  2. Select .NET Framework 3.5
  3. Create an ASP.NET Empty Website either in Visual Basic or Visual C#
  4. Right click on the Website in Solution Explorer
  5. Select Add Service Reference
  6. Select Advanced option in Add Service Reference window
  7. Select Add Web Reference in Service Reference Settings
  8. Enter the copied URL in Add Web Reference window
  9. Change the Web reference name and click Add Reference
  10. Import namespace for the Web Service

    C#
     
    using WebServiceTest;
     
    VB.NET

    Imports WebServiceTest
     
  11. Now drag and drop two labels in aspx page and write below code in Page_Load method of code behind file. Create the instance of the Service1 class and call the method of that class.

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    <br />

     
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

     

  12.  
     
  13. Write below code in Page_Load method of code behind file. Create the instance of the Service1 class and call the method of that class.

    C#

    Service1 service = new Service1();
    Label1.Text = service.HelloWorld();
    int num = service.AddNumbers(3, 4);
    Label2.Text = "The result is: " + num.ToString();

    VB.NET

    Dim service As New Service1()
    Label1.Text = service.HelloWorld()
    Dim num As Integer = service.AddNumbers(3, 4)
    Label2.Text = "The result is: " & num.ToString()
     
  14. View website in browser, you will see the result of both web methods of Web Service

A web Service created in any ASP.NET language can be consumed in application written in any other ASP.NET language.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Create webservice in Visual Studio 2008

Disclaimer - Privacy
© 2002-2017 DevASP.net