How to: Return multiple response from wcf

Imagine you have a wcf application that return xml response but after some time your requirements changed that we want to return multiple response (XML /Json) from the same service.How do you achieved this? Earlier it was nightmare for developer to return multiple response from .net. Now you have a good news that you can return multiple response from wcf 4.0 with a very minimal changes.

So In this post I am going to show you how to return multiple response from wcf in .net framework 4.0

Open visual studio and create an empty website.

Right click on the website project and add a new wcf service.It will create three files named Service.svc,IService and Service.cs.
Open IService.cs file and paste the following code

Open Service.cs file and add following code

Right click on App_Code folder and add new class named Person and add following code(This class contains dummy data for demo purpose only )
Till now whaterver I have created so for is normal process for creating wcf service application in .net.Now open web.config and go to endpointBehaviours section and set automaticFormatSelectionEnabled equal to true .

<?xml version="1.0"?>
  For more information on how to configure your ASP.NET application, please visit
            <service name="Service" behaviorConfiguration="ServiceBehavior">
                <endpoint address="" binding="webHttpBinding" behaviorConfiguration="JsonBehavior" bindingConfiguration="" contract="IService"/>
                <behavior name="JsonBehavior">
                    <webHttp automaticFormatSelectionEnabled="true"/>
                <behavior name="ServiceBehavior">
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="true"/>
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="true"/>
        <compilation debug="true" targetFramework="4.0">
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>

What does automaticFormatSelectionEnabled do? It is a new property introduced in wcf 4.0 that do following task.

When enabled, automatic formatting chooses the best format in which to return the response. It determines the best format by checking the following, in order:
  1. The media types in the request message’s Accept header.

  2. The content-type of the request message.

  3. The default format setting in the operation.

  4. The default format setting in the WebHttpBehavior.
 Now our wcf service application is complete and ready to use.Let's consume this service using Jquery.Open Default.aspx page (in my case ) and paste following code.

<%@ 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">
    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnJson").click(function () {
            $("#btnXml").click(function () {
        function XmlCall() {
                type: "POST",
                url: "Service.svc/GetAll",
                dataType: "xml",
                contentType: "application/xml",
                data: "",
                success: function (ret) {
        function JsonCall() {
                type: "POST",
                url: "Service.svc/GetAll",
                dataType: "json",
                contentType: "application/json",
                data: "{}",
                success: function (ret) {
    <form id="form1" runat="server">
        <input type="button" id="btnJson" value="Get JSON" />
        <input type="button" id="btnXml" value="Get XML" />

In the above page I have added two functions XmlCall and JsonCall function and I am calling these two functions on button click.Run and test the application you will see that same service is returning multiple respnse.

Next Post Previous Post
No Comment
Add Comment
comment url