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

Hybrid Mobile Web Developer
Source: XDuce
Details: Expert knowledge of the Microsoft technologies, including .NET, ASP.NET, MVC, MS SQL Server, WCF. New York City, NY (Midtown)....  More
30+ days ago

New York, NY 11-November-2016

.net developer
Source: Indeed
Details: Projects will use Medical Mutual Agile/Scrum Methodology, standards and document templates. Project involves development of member tools and mobile app.  More
7 days ago

Seattle, WA 20-February-2017

.Net/C# Developer
Source: Indeed
Details: Web Services with WCF. Please go through the JD and apply with updated copy of your resume....  More
18 hours ago

Jersey City, NJ 27-February-2017

.NET Developer
Source: Indeed
Details: Summary: Our vision is to be the best provider of building products in the eyes of our customers, employees, shareholders, suppliers and communities. We  More
18 hours ago

Tampa, FL 27-February-2017

.Net Developer
Source: Indeed
Details: Web Services leveraging WCF or Web API. Indeed Hire has partnered with Isagenix....  More
30+ days ago

Gilbert, AZ 06-December-2016

Sr .Net Developer
Source: conrep
Details: Position: Sr .Net Developer Location: Bensalem, PA Duration: 6+ Months Technical skills : ========================== ⿢ Advanced skills in creating multi-tier...  More
3 days ago

Bensalem, PA 19020 24-February-2017

SharePoint/.NET Developer with Top Secret Clearance (1483)
Source: Indeed
Details: Bachelor's Degree in Computer Science and 3 years of experience with .NET technologies (C#, ASP.NET, XML, WCF, etc.)....  More
11 days ago

Washington, DC 20001 16-February-2017

NET Backend Developer
Source: Indeed
Details: Experience designing and/or developing web services using WCF or Web API. Roles and Responsibilities*....  More
4 days ago

Dallas, TX 23-February-2017

C# .Net Developer
Source: Indeed
Details: Experience with RESTful services, WCF and other Web Services. C# .NET Developer*....  More
4 days ago

Chantilly, VA 20151 23-February-2017

.Net Full Stack Developer
Source: Indeed
Details: - Solid C# .NET background (ASP.NET MVC, WebAPI, WCF, nUnit/xUnit) is required in addition to the above.*. .Net Full Stack Developer.*....  More
4 days ago

Irvine, CA 23-February-2017

MS .Net Developer - Fulltime/ W2 Contract to hire
Source: Indeed
Details: Web Services, WCF and OData Data Services. \*HTC Global Services, Inc....  More
4 days ago

Sacramento, CA 23-February-2017

.net Developer
Source: Indeed
Details: TechStratium is a rapidly growing strategy and advanced technology firm driving innovations in mission-critical software, systems and processes. With an elite  More
5 days ago

Greenbelt, MD 23-February-2017

.Net Developer
Source: Veterans Sourcing Group
Details: ASMX and WCF Web Services, and ADO.NET. The ideal candidate would have solid experience with Microsoft development technologies and have extensive experience...  More
5 days ago

Wilmington, DE 19801 23-February-2017

Sr .Net Developer
Source: Indeed
Details: *Role: * Sr .Net Developer *Location: * Phoenix, Arizona F2F interview is must . *W2 ONLY. NO C2C* *MANDATORY SKILLS/EXPERIENCE: * • 9+ years of...  More
6 days ago

Phoenix, AZ 22-February-2017

Junior .Net Developer
Source: Indeed
Details: Experience building WCF Services. Infinity, a Stamford Technology Company, is a global provider of Life Science Enterprise Level IT services for biotech,...  More
6 days ago

New York, NY 10028 21-February-2017

.Net Developer
Source: Indeed
Details: Experience with Entity Framework, MVC, WCF and Web API preferred. Role and Responsibilities*....  More
6 days ago

Mechanicsville, VA 21-February-2017

.Net Developer
Source: Indeed
Details: *MUST BE US CITIZEN!!!* Seeking a mid‐level .Net Developer who is able to independently solve analytical problems with innovative approaches and solutions.  More
6 days ago

Athens, GA 21-February-2017

.NET Developer
Source: Indeed
Details: 5-7 years of experience developing and supporting enterprise business applications using Microsoft .NET technologies as they apply to Web development (MVC, WCF,...  More
30+ days ago

Linthicum, MD 29-November-2016

.Net / C# Developer
Source: Indeed
Details: WCF 3.5, 4.0 or higher. Seeking an experienced .Net / C# developer to work on applications/services that will be integrated with the current suite of iOS and...  More
7 days ago

Tampa, FL 20-February-2017

.Net Developer with SSIS // Plano, TX // Fulltime
Source: Indeed
Details: Programming knowledge in Web services and WCF development. .Net Developer with SSIS*....  More
30+ days ago

Plano, TX 15-November-2016

.net developer
Source: Indeed
Details: WCF Web service experience – 1 year. Required Local Candidate who can attend Face to Face interview.*....  More
7 days ago

Indianapolis, IN 20-February-2017

(Local to Washington) .Net Full Stack Angular Developer
Source: Indeed
Details: MVC, WCF, REST, Web Services:. Experience with Web API and REST, WCF, LINQ, and MVC required (3+ year). Need local to Washington only....  More
30+ days ago

Redmond, WA 98052 25-January-2017

.NET developer
Source: Indeed
Details: .Net Web Services, WCF or Web API:. 24 Months of experience with .Net Web Services, WCF or Web API. 12+ Months of automated testing frameworks (including but...  More
9 days ago

Brooklyn, NY 18-February-2017

Senior Software Developer
Source: Indeed
Details: Experience with OPC, DNP3, MODBUS, MongoDB, SQL, WCF, SOAP, C#, WPF and MVVM is a plus. Well established Electrical Power System Analysis & Operation Software...  More
28 days ago

Irvine, CA 30-January-2017

.Net Developer
Source: Indeed
Details: 84 months experience developing N-Tier .net web applications 72 months experience developing applications utilizing .Net 3.5 or above. 72 months experience  More
10 days ago

Troy, NY 18-February-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.