google-site-verification=ECX1M6_Vb39ty4VtQDXQce6wOjmPTxSettd3hTIZb9Q

Consume Restful WCF service in ASP.NET Web Application Examples

random

Consume Restful WCF service in ASP.NET Web Application Examples

ASP.net Web Application Consuming WCF Restful Service Server Side


In the previous article I explained About WCF - An Introduction with Example, Register WCF Service with IIS and ASP Dot, Creating WCF REST Services in ASP.NET Web Based Applications

In this article I will explain how to consume a WCF service in ASP.Net Web Based Applications. Follow the steps below:-
  • Now right click on project name and select "Add Service Reference".
  • Now Enter your WCF web service URL and click on Go button to see a list of available services. All your available web methods (services) will appear on the pane below. Give specific reference namespace. Here I am giving reference namespace as "ServiceReference1". Click on Ok button to finish the Add service reference Wizard.
  • After clicking on Ok button a service model will generate in your web.config file as follows.
Find the source code below:-

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Consume Restful WCF service in ASP.NET Server Side Web Application Examples</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        From JSON Response
        <asp:GridView ID="grdDisplayJSON" runat="server">
        </asp:GridView>
        <br />
        <br />
        From XML Response
        <asp:GridView ID="grdDisplayXML" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web.Script.Serialization;
using System.Data;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                ServiceReference1.Create_Restful_WCF_ServiceClient proxyClient = new ServiceReference1.Create_Restful_WCF_ServiceClient();

                JavaScriptSerializer js = new JavaScriptSerializer();
               
                object jsonResult = proxyClient.GetCustomerListJSON();
                string strJSON = js.Serialize(jsonResult); // Converting object to JSON format string

                grdDisplayJSON.DataSource = jsonResult;
                grdDisplayJSON.DataBind();

                object xmlResult = proxyClient.GetCustomerListXML();
                grdDisplayXML.DataSource = xmlResult;
                grdDisplayXML.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
Demo

ASP.net Web Application Consuming WCF Restful Service Server Side, consuming, Restful WCF service, asp.net 4.0, web application architecture .net framework 4.0,aspx coding,code behind c#,code behind server side programming

Download the source code, click here
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading .. :)
Consume Restful WCF service in ASP.NET Web Application Examples Reviewed by Ravi Kumar on 5:28 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.