Thursday, 13 June 2013

ASP.NET - Web Services

A web service is a web-based functionality accessed using the protocols of the web to be used by the web applications. There are three aspects of web service development:
  • Creating the web service
  • Creating a proxy
  • Consuming the web service

Creating the Web Sevice:

A web service is an web application which is basically a class consisting of methods that could be used by other applications. It also follows a code-behind architecture like the ASP.Net web pages, although it does not have an user interface.

I am just Creating a Simple WebService.

Take the following steps to create the web service:
Step (1): Select File--> New --> Web Site in Visual Studio, and then select ASP.Net Web Service.
Step (2): A web service file called Service.asmx and its code behind file, Service.cs is created in the App_Code directory of the project.
Step (3): The .asmx file has simply a WebService directive on it:

<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>

Step (4): Open the Service.cs file, the code generated in it is the basic Hello World service.
Step (5): Add a New WebMethod in Servie Class named "GetUserDetails". this will fetch all Data from a Table named admission.
WebMethod looks like Below Code Snippet:

    public DataSet GetUserDetails()
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
        SqlCommand cmd = new SqlCommand("select * from admission ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // Create an instance of DataSet.
        DataSet ds = new DataSet();
        return ds;

Step (6) : Running to Test your Service right Click on Service.asmx file and select View in Browser option this will open your web service test page, which allows testing the service methods.

Step (7) : Click on a method name, and check whether it runs properly .Click on GetUserDetails webMethod.

Step (8): Click Invoke Button to Display Output that Service Method returns . like this:

Consuming the Web Service:

For using the web service, create a web site under the same solution. This could be done by right clicking on the Solution name in the Solution Explorer.

Firstly you have to Add service Refrence in your Website. for this Right click on your Website, select Add Service Refrence . in the Add Service Refrence Popup Window Click on Discover Button it will Search all   asmx service in your Solution.

now select asmx service that you want to add in your Website and Click OK Button. now Service has been Added in your Website Project. this will look like this :

in Default.aspx add a GridView Control. Code lookes like Below snippet ...

    <form id="form1" runat="server">
    <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="true">

Now in Default.aspx.cs file Add the Following Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//this is the proxy
using ServiceReference1;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        var proxy = new ServiceReference1.ServiceSoapClient();

        grid1.DataSource = proxy.GetUserDetails();

Now Build Your Project and Right click on default.aspx, select View in Browser option. Grid Display all Data From Database Table.

this was simple Web Service Demo.
You Can Download the complete SourceCode from here Download this Project

 feel free to Ask any Question regarding this Post.

Thanks and Regards:
Suraj Kumar Maddheshiya


Post a Comment


ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)