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

Sr. C# ASP.NET Lead Developer
Source: Indeed
Details: Minimum of 4 years of experience designing and developing web services using SOAP and REST, especially with Microsoft WCF. Sr....  More
4 days ago

Los Angeles, CA 19-April-2017

Senior Software Engineer
Source: Indeed
Details: Application Frameworks – .Net 4.6, ASP.NET, ADO.NET, WCF, Web API. Is looking for senior software engineers to develop property tax assessment systems for...  More
4 days ago

Orange, CA 19-April-2017

Entry Level .Net Developer
Source: Indeed
Details: Experience with C#, JavaScript, SQL (PostgreSql preferred), NET MVC, REST Web API services, WCF & SOA, HTML5/CSS, jQuery, Bootstrap....  More
10 days ago

Dallas, TX 13-April-2017

.NET Developer With Oracle Experience, Location- Mechanicsburg, PA.
Source: Indeed
Details: 5+ years of experience in WCF services, Web services, Microsoft COM development experience. Greeting of the day....  More
10 days ago

Dauphin, PA 13-April-2017

Programmer Analyst
Source: State of Oklahoma
Details: Web Services, Windows Services, work flow, and WCF. Submit resume and cover letter with salary requirements and availability date via email with “Programmer...  More
12 days ago

Oklahoma 11-April-2017

Full Stack Developer
Source: DISYS
Details: Hands-on experience delivering high-scale service-oriented architecture architecting including, but not limited to, SOAP and/or REST web services with strong...  More
26 days ago

Everett, WA 29-March-2017

Trauma Data Abstractor
Source: HCA Inc
Details: The Trauma Data Abstractor, works under the supervision of the Manager, Trauma Registry. The abstractor is responsible for identification, collection, entry,  More
3 days ago

Saint Petersburg, FL 21-April-2017

Entry Level Analyst I
Source: Bank of America
Details: Job Description: **Candidates requiring visa sponsorship will not be considered for this position** Bank of America is looking to build out their core  More
30+ days ago

Plano, TX 75024 08-March-2017

.NET Developers
Source: Apex Systems Inc
Details: Job #: 723618 Apex Systems, the nation’s 2nd largest Information Technology Staffing organization, has several immediate openings for ASP.NET Developers .  More
30+ days ago

Charlotte, NC 28217 01-February-2017

.NET Developer - Redmond, WA
Source: Avanade MY
Details: A .NET Solution Developer focuses on designing, developing and testing mission critical enterprise solutions and other custom application development projects...  More
14 days ago

Redmond, WA 10-April-2017

Net 4.x Developer (GC_USC)
Source: Sonsoft Inc
Details: At least 4 years of experience with WCF framework. At least 5 years of experience in software design and development on .Net framework 4.5, ASP.NET, C#, VC++...  More
16 days ago

Redmond, WA 08-April-2017

Sr. DataStage Developer
Source: Indeed
Details: *Sr. DataStage Developer* 12+ Months New York, NY Experience: 10+ Years Key Skills (Must): *Building DW in SQL Server 2014, SSIS, T-SQL, ETL DataStage. C#...  More
20 days ago

New York, NY 03-April-2017

Mid-Level Software Engineer
Source: Eliza Corporation
Details: Building a next-generation tools platform for creating, managing and deploying multi-channel outreach campaigns in the AWS cloud, designed as a single page web...  More
27 days ago

Danvers, MA 01923 28-March-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
10 hours ago

Dallas, TX 24-April-2017

.NET Developer
Source: iiPAY
Details: Net Developer Based in the UK and US, iiPAY is a successful global payroll and payments services business, providing fully managed payroll and clinical  More
2 days ago

Dallas, TX 22-April-2017

.NET Developer
Source: CTDI
Details: 3+ years recent experience in application development with ASP.NET, C#, Web Forms, Win Forms, Web services (XML, SOAP, WCF), and Oracle (SQL, PL/SQL), C, Perl,...  More
2 days ago

Orlando, FL 22-April-2017

.Net Developer
Source: Sonsoft Inc
Details: At least 2 years of experience in web applications, windows services, WPF, WCF and SOA based web services. Only US Citizen, Green Card Holder can apply....  More
2 days ago

Rosemead, CA 22-April-2017

.NET Developer, Mid
Source: Booz Allen Hamilton
Details: Experience with ASP.NET and C# application architectures and technologies, including ASP.NET Web Forms, MVC, WebAPI, WCF, Entity Framework, and LINQ....  More
30+ days ago

Alexandria, VA 21-March-2017

.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
3 days ago

Township of Warren, NJ 21-April-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
3 days ago

Boca Raton, FL 33431 21-April-2017

.Net Developer
Source: Logos Infotech Inc
Details: At least 2 years of experience in web applications, windows services, WPF, WCF and SOA based web services. Only for Green card and US Citizens....  More
3 days ago

Rosemead, CA 21-April-2017

.Net / C# Developer
Source: Bank of America
Details: Experience with WCF, WPF. Candidate will be joining Global Stock Loan team in Equities Technology team....  More
3 days ago

New York, NY 10038 20-April-2017

.NET Developer
Source: NIC
Details: ASP.NET 4.0 using C#, ADO.NET, SOAP, WCF, and Entity Framework; Master’s degree in Computer Science, Information Technology, or related field;...  More
4 days ago

Trenton, NJ 19-April-2017

.NET Developer
Source: Procentrix
Details: .NET, C#, C++, ASP.NET, Ajax, jQuery, MVC, , WCF, Sybase SQL Anywhere/SQL Server programming. Working as a member of a project team, develops custom Web...  More
3 days ago

Washington, DC 20-April-2017

.NET Developer- FAA
Source: Engility Corporation
Details: 5 years or more experience with C#, WCF Services, MVC 4.0 or higher, Linq, Entity Frame work. .Net development experience in C#, ASP.NET and WCF Services using...  More
30+ days ago

Arlington, VA 22201 14-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.