Calling WCF Service using Asp.Net Web Application

 

In my previous article I explain about how to create WCF service and in this article we discuss about how to consume WCF service through Asp.Net Web application.

 

Create new website from visual studio select New---> Website and give some name as you like.

 

    

 

After Creation Website now we need to add WCF reference to our web application for that right click on your web application and select Add Service Reference.

 

                      

 Now one wizard will open in that give your Microsoft WCF service link and click Go after add your service click OK button.

         

After completion of adding WCF Service first open Default.aspx page and write the following code

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>

<style type="text/css">

.style1 {

height: 26px;

}

    .style2

    {

        width: 230px;

    }

    .style3

    {

        height: 26px;

        width: 230px;

    }

</style>

</head>

<body>

<form id="form1" runat="server">

<div>

<table align="center">

<tr>

<td class="style2" >

<b>Emp Details</b>

</td>

</tr>

<tr>

<td class="style2">

FirstName:

</td>

<td>

<asp:TextBox ID="txtfname" runat="server"/>

</td>

</tr>

<tr>

<td class="style2">

LastName:

</td>

<td>

<asp:TextBox ID="txtlname" runat="server"/>

</td>

</tr>

<tr>

<td class="style3">

Location:

</td>

<td class="style1">

<asp:TextBox ID="txtlocation" runat="server"/>

</td>

</tr>

<tr>

<td class="style2">

</td>

<td>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />

</td>

</tr>

<tr>

<td colspan="2">

<font color="lime">Get By EmpID</font><asp:TextBox runat="server" ID="txtEmpID" width="90px" />

<asp:Button runat="server" ID="btnGet" OnClick="BindEmpDetailsByID" Text="GetEmpDetails" />

</td>

</tr>

<tr>

<td colspan="2">

<asp:Label ID="lblResult" runat="server"/>

</td>

</tr>

<tr>

<td colspan="2">

<asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false">

<RowStyle BackColor="#EFF3FB" />

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

<Columns>

<asp:BoundField HeaderText="Employee ID" DataField="EmployeeID" />

<asp:BoundField HeaderText="FirstName" DataField="FirstName" />

<asp:BoundField HeaderText="LastName" DataField="LastName" />

<asp:BoundField HeaderText="Location" DataField="Location" />

</Columns>

</asp:GridView>

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

 

Now open Default.aspx.cs file and add following namespace

 

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using ServiceReference1;

using System.Text;


After adding namespaces write the following code in codebehind

 

public partial class _Default : System.Web.UI.Page

{

    ServiceClient objService = new ServiceClient();

   protected void Page_Load(object sender, EventArgs e)

   {

         if(!IsPostBack)

        {

           BindUserDetails();

        }

   }

 

  protected void BindEmpDetailsByID(object sender,EventArgs e)

  {

        IList<UserDetails> objUserDetails = new List<UserDetails>();

        objUserDetails = objService.GetEmployeeDetails(Convert.ToInt32(txtEmpID.Text));

        gvDetails.DataSource = objUserDetails;

        gvDetails.DataBind();

  }

 

 protected void BindUserDetails()

 {

       IList<UserDetails> objUserDetails = new List<UserDetails>();

       objUserDetails = objService.GetEmployeeDetails(1);

       gvDetails.DataSource = objUserDetails;

       gvDetails.DataBind();

 }

 protected void btnSubmit_Click(object sender, EventArgs e)

 {

      UserDetails userInfo = new UserDetails();

      userInfo.FirstName = txtfname.Text;

      userInfo.LastName = txtlname.Text;

      userInfo.Location = txtlocation.Text;

      string result=  objService.InsertEmployeeDetails(userInfo);

      lblResult.Text = result;

 

       BindUserDetails();

 

      txtfname.Text = string.Empty;

      txtlname.Text = string.Empty;

      txtlocation.Text = string.Empty;

   }

}


After completion of adding code check your endpoint connection for Microsoft WCF Service reference that should be like this

 

<endpoint address="http://localhost:55953/SampleUserService/Service.svc"

    binding="wsHttpBinding"bindingConfiguration="WSHttpBinding_IService"

    contract="ServiceReference1.IService"name="WSHttpBinding_IService">

    <identity>

     <dnsvalue="localhost" />

    </identity>

   </endpoint>

------------------------------- End ------------------------------------

Output of the Project: