Automatic Format Selection in WCF RESTful service

By | February 17, 2014

In .NET framework 4.0, Automatic Format Selection feature is introduced by AutomaticFormatSelectionEnabled property of WebHttpBehavior class. The purpose is to dynamically find out and return the response in appropriate format for a service operation.MCSD Exam: 70-487
If the value of this property is set to “true” means using Automatic Format Selection, the response format of the operation is determined by the following factors in the same ordered way:

  1. Accept header of Request Message
  2. Content-Type of the HTTP request Message
  3. ResponseFormat parameter of service operation.
  4. WebHttpBehavior’s default format settings

WCF RESTful Service
By default, AutomaticFormatSelectionEnabled property of WebHttpBehavior class is set to false. So, in my previous article on Step by step creating a WCF RESTful Service, we explicitly defined format selection of ResponseFormat parameter in Web operation as follows:

    [ServiceContract]
    public interface IProductRESTService
    {
        [OperationContract]
        [WebInvoke(Method = “GET”,
                                 ResponseFormat = WebMessageFormat.Xml,
                                 BodyStyle = WebMessageBodyStyle.Bare,
                                 UriTemplate = “GetProductList/”)]
        List GetProductList();
    }

But when Automatic Format Selection feature is enabled, Accept header of request message takes the highest precedence. First of all, Windows Communication Foundation infrastructure looks for Accept header of Request message. If found an appropriate format, response will be returned in the same format. However, if not the value of Accept header is found suitable, then WCF will look for the next factor i.e. Content Type of Request Message.

In one of my WCF service tutorial, I have explained how to consume WCF RESTful service using jQuery? So, if you wanted to see that how to set media-type of Accept header or Content-Type of request message, you can find complete code in that WCF RESTful Service Example. For your understanding, following is the sample jQuery Ajax call for my WCF HTTP Service.

      $.ajax({
                 type: “GET”,
                 url: “http://localhost/XXXXXXX/XXX”,
                 dataType: “json”,
                 contentType: “json”,
                 success: function (data) {
//Success code…                     
                     });
                 },
                 error: function (xhr) {
                     alert(xhr.responseText);
      }

In above jQuery code snippet, dataType represents the expected response format from server that becomes the value of Accept header in Request Message, while contentType is Content Type of the same.

So, in order, if no appropriate Content Type is specified, ResponseFormat parameter in WebInvokeAttribute/WebGetAttribute of service operation will be considered, already explained above.
Lastly, DefaultOutgoingResponseFormat property of WebHttpBehavior class will be used to determine the output format of WCF RESTful service response, if nothing found in service operation attribute.

Now, how to enable this Automatic Format Selection feature for a WCF RESTful service? It can be done programmatically as well as through configuration. Configuration settings for enabling this feature is simple and straight forward using:

  • WebHttpBehavior
  • WebHttpEndpoint

Using WebHttpBehavior:-

    <system.serviceModel>
        <services>
            <service name=”MyRESTService.ProductRESTService” behaviorConfiguration=”serviceBehavior”>
                <endpoint address=””
                               binding=”webHttpBinding”
                               contract=”MyRESTService.IProductRESTService” behaviorConfiguration=”web”>
                </endpoint>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name=”serviceBehavior”>
                    <serviceMetadata httpGetEnabled=”true”/>
                    <serviceDebug includeExceptionDetailInFaults=”false”/>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name=”web”>
                    <webHttp automaticFormatSelectionEnabled=”true” />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled=”true” />
    </system.serviceModel>

Using WebHttpEndpoint:-

    <system.serviceModel>
        <standardEndpoints>
           <webHttpEndpoint>
                    <standardEndpoint name=”web”
 helpEnabled=”true”
 automaticFormatSelectionEnabled=”true”  />
           </webHttpEndpoint>
        </standardEndpoints>
    </system.serviceModel>

Hopefully, this WCF tutorial has simplified the concept of Automatic Format Selection for Windows Communication Foundation RESTful service.



Recents WCF Jobs in USA

Senior Software Engineer (C#, .NET)
Source: WinterWyman
Details: 5-7+ years’ experience in C#, .Net, ASPX, WCF, SQL/Oracle. Our client, based near Boston, has the following opening for a Senior Software Engineer skilled in C#...  More
11 days ago

Auburndale, MA 02466 12-January-2017

Sr.Test Automation Engineer
Source: Indeed
Details: Expertise in most of the following -Selenium Webdriver, Web API (MS ASP.Net), MVC, WCF, HTML5, CSS, JavaScript, JSON, SOAP, REST, JQuery, .Net Framework, C#,...  More
5 days ago

Round Rock, TX 17-January-2017

Software Developer
Source: Indeed
Details: Experience with WCF services and Windows Services. We are adding another member to our team of talented Software Developers....  More
11 days ago

Rogers, AR 72756 12-January-2017

Sr. Microsoft Dynamics CRM Developer
Source: Indeed
Details: *Position Title: * Sr. Microsoft Dynamics CRM Developer *Primary Work Location: * New York City *Start Date/ End Date: * Immediate Min Exp 10 years + ...  More
5 days ago

New York, NY 17-January-2017

.Net Full Stack Developer
Source: Sonsoft Inc
Details: At least 4 years of product development experience in Dot Net technologies (C#, ASP.Net, MVC, MVVM, WCF, SQL Server)....  More
5 days ago

Redmond, WA 18-January-2017

.Net Full Stack Developer
Source: Sonsoft Inc
Details: At least 4 years of product development experience in Dot Net technologies (C#, ASP.Net, MVC, MVVM, WCF, SQL Server)....  More
5 days ago

Bellevue, WA 18-January-2017

C# ASP.NET Developer (10+ years of experience) (W2 Candidate only)
Source: 360 IT Professionals
Details: O C#,.NET 2.0-4.5 (ASP.Net, WCF, WF, ADO.Net, Asmx, WebAPI), Visual Studio 2010/2015. O Senior level candidate with 10+ years of overall development experience....  More
12 days ago

Bellevue, WA 11-January-2017

Mid Level .NET / C# / SQL Server Developer
Source: Apoorva
Details: XML / XSLT and WCF Web Services. WCF and REST web services experience nice to have. Front End Development....  More
13 days ago

Colorado 09-January-2017

CSI-IT - ASP Dot Net with MVC
Source: CSI
Details: WCF. Participate in development and maintenance of middle and backend layers for United Mobile Platform by maintaining existing code base as well as extending...  More
4 days ago

Houston, TX 19-January-2017

Windows C# .NET MVC Web Portal Developer in Ft. Lauderdale,FL
Source: Indeed
Details: Technical expertise in Microsoft Technologies such as .Net Framework, ASP.Net MVC, C#, WCF, WPF, Ajax, ADO.NET,XML....  More
5 days ago

Fort Lauderdale, FL 18-January-2017

Senior .NET WPF Developer Lead
Source: Indeed
Details: Experience with WCF is a plus. We are looking for a seasoned .NET developer with WPF experience to lead our development efforts for a critical Windows...  More
10 days ago

Boston, MA 13-January-2017

Senior .NET WPF Developer Lead
Source: Indeed
Details: Experience with WCF is a plus. We are looking for a seasoned .NET developer with WPF experience to lead our development efforts for a critical Windows...  More
10 days ago

Atlanta, GA 13-January-2017

Technical Integration .NET Developer
Source: Campus Management
Details: Solid knowledge of .Net framework 4.0 or higher, C#, ASP.NET, WCF Web Services, Windows Workflow, Visual Studio 2013, SOA, Web Services, XML/XSL, AngularJS,...  More
9 hours ago

Boca Raton, FL 33431 23-January-2017

C# / ASP.NET Developer (FTE) (Dallas)
Source: Brand Protection Agency, LLC
Details: The developer will be exposed to the latest technologies such as WCF, ASP.NET, ASP.NET MVC, .NET 3.5 and 4.0, JavaScript, AJAX, JSON, ext-js and other open...  More
57 minutes ago

Dallas, TX 23-January-2017

.NET Developer
Source: SITA
Details: Xml web services, SOAP, XSD, WSDL and WCF and WPF technologies. Ability to complete coding tasks assigned of specific products and / or applications in the SITA...  More
1 day ago

Islip, NY 22-January-2017

.NET Developer
Source: Empirical Edge
Details: Knowledge or Exposure to WPF, WCF, LINQ ,RAD controls & Silverlight will be added advantage. 2+ Years of Experience (for multiple positions)....  More
2 days ago

Marlton, NJ 08053 21-January-2017

.NET Developer
Source: Data2Design Technologies
Details: Primary development in C# and the ASP.NET framework, as well as Microsoft MVC, WCF, Entity Framework, LINQ, and JQuery. .NET Application Developer....  More
3 days ago

Arlington, VA 20-January-2017

Full Stack .NET Developer
Source: Partner's Consulting, Inc.
Details: WCF; Our client is seeking a Full Stack .NET Developer to collaborate with Project Stakeholders to identify product and technical requirements....  More
3 days ago

Philadelphia, PA 19-January-2017

.Net Developer
Source: Wells Fargo
Details: Good verbal, written, and interpersonal communication skills Other Desired Qualifications 8+ years of hands on experience on developing .NET applications on...  More
30+ days ago

Chandler, AZ 28-October-2016

.Net Developer
Source: Wells Fargo
Details: Good verbal, written, and interpersonal communication skills Other Desired Qualifications 8+ years of hands on experience on developing .NET applications on...  More
30+ days ago

Charlotte, NC 28-October-2016

.Net Developer
Source: Wells Fargo
Details: Good verbal, written, and interpersonal communication skills Other Desired Qualifications 8+ years of hands on experience on developing .NET applications on...  More
30+ days ago

Oakland, CA 28-October-2016

Junior .NET Developer - Green Bay, WI or Indianapolis, IN
Source: UnitedHealth Group
Details: Position Description: UnitedHealth Group is a company that's on the rise. We're expanding in multiple directions, across borders and, most of all, in the way  More
26 days ago

Indianapolis, IN 46218 28-December-2016

.Net Developer 3
Source: CACI
Details: Familiarity with Microsoft WCF Services and web service utilization a plus. CACI is looking for an experienced web developer to join our team....  More
30+ days ago

Chantilly, VA 20153 09-December-2016

.Net Developer
Source: Technamo LLC
Details: Experience with Web Services design and development using WCF and REST. Description of specific Duties in a typical workday for this position:....  More
4 days ago

McLean, VA 19-January-2017

.NET Developer
Source: Scout ET
Details: Other key skills that we look for in candidate are WCF and SQL. Application Lead Developer experience with .net, WPF, WinForms, ASP.net, RAD with Telerik, WCF...  More
4 days ago

Pennington, NJ 19-January-2017
Category: C# RESTful API WCF RESTful Service Tags: ,

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.

  • http://www.blogger.com/profile/16725990363509935667 anish mounish

    This comment has been removed by a blog administrator.