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:


    [WebMethod]
    public DataSet GetUserDetails()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from admission ", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // Create an instance of DataSet.
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        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 ...

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

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();
        grid1.DataBind();
    }
}


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





0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives