Consume Restful WCF service in ASP.NET Web Application Examples


Consume Restful WCF service in ASP.NET Web Application Examples 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:-

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

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

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)
            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;

                object xmlResult = proxyClient.GetCustomerListXML();
                grdDisplayXML.DataSource = xmlResult;
        catch (Exception ex)
            throw ex;
Demo Web Application Consuming WCF Restful Service Server Side, consuming, Restful WCF service, 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


Email *

Message *

Powered by Blogger.