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

Application Programmer/Developer (.NET
Source: Bank of America
Details: Expert knowledge of .NET Framework, MVC , Visual Studio, ASP.net 4.5, WCF, WebAPi, SQL Server 2012, TFS, ADA, XML, XSL, Telerik, design patterns, web and...  More
8 days ago

Addison, TX 75001 13-February-2018

Application Programmer/Developer. Net
Source: Bank of America
Details: Expert knowledge of .NET Framework, MVC , Visual Studio, ASP.net 4.5, WCF, WebAPi, SQL Server 2012, TFS, ADA, XML, XSL, Telerik, design patterns, web and...  More
29 days ago

Charlotte, NC 28255 23-January-2018

.Net Developer
Source: Bank of America
Details: Microsoft.Net, C#, WCF & SQL Server. Develops, enhances, debugs, supports, maintains and tests software applications that support business units or supporting...  More
29 days ago

Charlotte, NC 28255 23-January-2018

.NET/Web developer
Source: Bank of America
Details: WCF, Web Services, ASP.NET MVC or Web API, JSP, Servlets, CSS, JSTL, Custom TAGs creation, JQuery, bootstrap, Analytics (Tagging, PDL) and any HTML5...  More
20 days ago

New York, NY 10038 01-February-2018

Senior .Net Developer
Source: Indeed
Details: *Required Skills: * * MVC .Net * Kendo UI * HTML5, CSS3, Bootstrap * Knockout JS, JQuery * MVC Web API, WCF, SignalR Job Type: Full-time Required experience...  More
23 days ago

Seattle, WA 29-January-2018

ASP.NET Developer
Source: Exeter Government Services LLC
Details: Maintain WCF web services allowing client-implemented software solutions secured access to enterprise databases while still complying with security policies....  More
30+ days ago

Alexandria, VA 19-January-2018

.NET Developer
Source: Acacia Technical Services
Details: Hands on Experience in .NET 3.5/4.0, WCF, SQL Server Integration and Analysis Services is a plus. We are looking for a Contract .NET Developer to join a great...  More
30+ days ago

Township of Warren, NJ 19-December-2017

Software Engineer
Source: Fairfax Data Systems
Details: Fairfax Data Systems Inc, is seeking a self motivated Software Engineer to join our team here in New Milford, CT. Key Responsibilities Include Help develop...  More
23 days ago

New Milford, CT 06776 29-January-2018

Junior Developer - IT Development
Source: ATI Physical Therapy
Details: Junior Developer What’s the Role: ATI’s Software Development Team strives to create software that is attractive, useful, and cutting-edge for our physical...  More
12 days ago

Bolingbrook, IL 60440 09-February-2018

Cyber Security Developer
Source: Bank of America
Details: Demonstrated strength in back-end development (Web API, WCF, etc.). Position will be as a member of the Software Development Team within Global Information...  More
30+ days ago

Chicago, IL 60603 11-November-2017

Cyber Security Developer
Source: Bank of America
Details: Demonstrated strength in back-end development (Web API, WCF, etc.). Position will be as a member of the Software Development Team within Global Information...  More
30+ days ago

Addison, TX 75001 11-November-2017

Cyber Security Developer
Source: Bank of America
Details: Demonstrated strength in back-end development (Web API, WCF, etc.). Position will be as a member of the Software Development Team within Global Information...  More
30+ days ago

Simi Valley, CA 93065 11-November-2017

Senior .Net Developer
Source: Indeed
Details: .NET MVC5, C#, Entity Framework, Web API, WCF, SQL, SQL server. We are currently looking for candidates with skillsets mentioned below....  More
7 hours ago

Moorpark, CA 21-February-2018

Senior .Net Developer
Source: Indeed
Details: Experience with web services technologies such as WCF, SOAP, HTTP, WSDL, XSD and/or REST. We are seeking a Sr .NET developer who will be responsible for...  More
8 hours ago

Plano, TX 21-February-2018

Senior .Net Developer
Source: Indeed
Details: Implementing and consuming Web Services using WCF and/or ASP.NET Web API. Will participates in all aspects of design, architecture, and development of a high...  More
1 day ago

West Chester, PA 20-February-2018

Senior .Net Developer
Source: Indeed
Details: Expertise in designing and developing web services using WEB API, WCF. Solid programming experience with C#, WCF, Restful services using WEB API, Entity...  More
2 days ago

Fremont, CA 19-February-2018

Senior .Net Developer
Source: Indeed
Details: Windows Forms, ASP.Net, WCF, MVC.*. 4+ Years' experience using .Net Technologies such as C#, Windows Forms, ASP.Net, WCF, MVC, SQL.*....  More
4 days ago

Tustin, CA 92780 18-February-2018

Senior .Net Developer
Source: Indeed
Details: JQuery, AngularJS, Bootstrap, nhibernate or other ORM, XML, AWS, WCF, and Web Service development. The candidate will provide development and IT support for a...  More
9 days ago

Fairfax, VA 12-February-2018

Senior .Net Developer
Source: Indeed
Details: Strong technical knowledge on MVC, Web API, WCF services, MS Online TFS - Knowledge of content management system (Tool- Sitecore)....  More
26 days ago

Detroit, MI 26-January-2018

Senior .Net Developer
Source: Indeed
Details: Strong back-end knowledge in MS SQL Server Experienced in writing RESTFul Services, WCF, etc. .NET Microservices Architecture, Docker, Azure, C#.NET, ASP.NET,...  More
27 days ago

El Monte, CA 25-January-2018

Senior .Net Developer
Source: Indeed
Details: .Net, ASP# .net MVC, WCF, WPF And Web API experience. Please share only if you are GC Or Citizen....  More
30+ days ago

North Brunswick, NJ 08902 15-December-2017

Senior .Net Developer ( Direct/ Fulltime Employment Only)
Source: Indeed
Details: Certifications, WCF, Web Services, Web forms, Windows Forms, VB.NET. Senior .Net Developer*....  More
30+ days ago

Irvine, CA 92606 09-January-2018

.Net Developer
Source: Cognizant
Details: Experience in architecting and/or implementing small/large database models on Oracle, SQL Server Hands on design and development experience in ASP.NET 3.5 or 4...  More
14 days ago

Mettawa, IL 07-February-2018

Mid Level Software Engineer (Contract)
Source: Indeed
Details: 4+ years’ experience in ASP .Net MVC, Web API / WCF Services, Entity Framework, C# .Net and Service Oriented Architecture....  More
15 days ago

Princeton, NJ 06-February-2018

Mid-Level Software Engineer
Source: Indeed
Details: Preferred knowledge of creating services with ASP.Net Web API and WCF. Mid-level Software Engineer*....  More
13 days ago

New York, NY 08-February-2018
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.