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 SharePoint Developer (10 Years)- F2F only
Source: Indeed
Details: *Position: SharePoint Developer* *Location: White Plains, NY* *Duration: 6 Months* *F2F Interview Required* * Develop Design specifications * Develop  More
1 day ago

White Plains, NY 18-September-2017

Hybrid Mobile Web Developer
Source: XDuce
Details: Expert knowledge of the Microsoft technologies, including .NET, ASP.NET, MVC, MS SQL Server, WCF. Hybrid Mobile Web Developer....  More
29 days ago

New York, NY 21-August-2017

Sr. Developer/Technical Lead (Remote)
Source: Swingtech
Details: Location: Topeka, KS (remote available) Duration: 6 months, option for extension Swingtech is seeking candidates for a Sr. Developer / Technical Lead role...  More
13 days ago

Topeka, KS 06-September-2017

Programmer / Developer Lead - Integration Lead
Source: Capgemini
Details: Extensive experience in C#, .NET, Visual Studio, Linq, WCF, Unit Testing (e.g., NUnit), and SQL. With more than 190,000 people, Capgemini is present in over 40...  More
6 days ago

Bloomfield, NJ 13-September-2017

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
12 days ago

Alexandria, VA 07-September-2017

C# Software Engineer
Source: Autonomous Solutions
Details: Bachelor or Master's degree in Computer Science, Computer Engineering, or related field 3+ years of industry experience preferred Good written and verbal...  More
30+ days ago

Logan, UT 12-July-2017

Senior Lead Software Engineer
Source: Practice Fusion
Details: Our cloud-based EHR is an ambitious Services Oriented Architecture written primarily in .NET, utilizing WebApi and WCF. Who We Are:....  More
27 days ago

San Francisco, CA 94102 24-August-2017

Lead Application Developer
Source: Whataburger
Details: Expert knowledge of .NET frameworks 3.5, 4.0, and 4.5, C#, VB.NET, ASP.NET Web Forms and MVC, XML, JavaScript, jQuery, Web Services and SOA, WCF, ADO.NET and...  More
12 days ago

San Antonio, TX 08-September-2017

IT - Developer I-II
Source: The Cincinnati Insurance Companies
Details: VB.NET / ASP.Net / Web Services using AJAX / WCF. IT - Developer I-II - (1700604) US-OH-Fairfield....  More
5 days ago

Fairfield, OH 14-September-2017

.NET Programmer
Source: Bank of America
Details: Web services development using WCF, SOAP and REST frameworks using multiple protocols; This position is responsible for providing .NET Developer level analysis,...  More
4 days ago

Plano, TX 75024 15-September-2017

Sr. C#.NET Developer / Lead
Source: TechPeople
Details: Experience in working with WCF, WPF, WF frameworks and Silverlight. Sr....  More
26 days ago

Houston, TX 25-August-2017

Web Application Developer
Source: NYC Careers
Details: At least three (3) years with hands on experience working with .NET Technologies (C#, ASP.NET, MVC, WebForms, WebAPI, WCF Services, Windows Services, LINQ,...  More
4 days ago

Queens, NY 16-September-2017

Software Developer
Source: Indeed
Details: Knowledge of WCF Services and Web Services JSON, OData and XML. Understanding and analyzing project requirements and translating them into programming...  More
1 day ago

Princeton, NJ 08540 18-September-2017

Application Programmer
Source: Bank of America
Details: Job Description: Position Summary Applications Programmer will help develop, support, maintain and test of Web Based, Client-Server and N-tier Architectures  More
30+ days ago

Pennington, NJ 08534 28-July-2017

C# .Net Developer
Source: Indeed
Details: Should Develop application using WCF and WF. Proven experience as a .NET Developer Framework 4.0, C#, Entity Framework or Application Developer....  More
27 days ago

Newark, NJ 24-August-2017

Application Programmer
Source: Bank of America
Details: Programming knowledge in Web services and WCF development. *Candidates requiring visa sponsorship will not be considered for this position**....  More
30+ days ago

Plano, TX 75024 26-July-2017

CMS Developer (.NET)
Source: CrownPeak
Details: ASP.NET, C#, and WCF. Crownpeak provides web content management Software-as-a-Service (SaaS) to large global enterprises....  More
5 days ago

Denver, CO 80203 15-September-2017

SOFTWARE DEVELOPER
Source: Universal Insurance
Details: Bachelor’s degree in Computer Science (or related degree) 4+ years .NET development experience Strong understanding of .NET, C++ or similar technology, and...  More
30+ days ago

Sarasota, FL 19-August-2017

Sr. .Net Developer (Telecommute)
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
28 days ago

Chantilly, VA 20153 23-August-2017

Sr Infrastructure Engineer II
Source: American Express
Details: IIS, WCF and MVC (Stack Overflow). The Senior Infrastructure Engineering role enables the technical delivery of a set of projects or products....  More
6 days ago

United States 13-September-2017

Back-End Developer
Source: FEi Systems, Inc.
Details: ¿ Hands-on experience with developing internal and external APIs by using ASP.NET WEB API, WCF, Nancy framework, or other frameworks....  More
4 days ago

Columbia, MD 15-September-2017

Junior .NET Developer (REMOTE)
Source: Citizant
Details: Experience with Web Services, WCF and Service Oriented Architecture a plus. Function as part of agile development team supporting O&M effort with multiple .NET...  More
4 days ago

Austin, TX 16-September-2017

SDET - No C2C
Source: Indeed
Details: Experience building WCF Web Services. We are looking for candidates with a technical background and an aptitude for big-data storage and large-scale web...  More
21 days ago

Redmond, WA 29-August-2017

Consultant
Source: Microsoft
Details: WCF, WF, MVC). Are you ready to seize an opportunity to build leading edge technology solutions and to deploy into production with real world impact?...  More
3 days ago

New York, NY 17-September-2017

SDE
Source: CSI Interfusion Inc
Details: Microsoft technologies, including ASP.NET, C#, MVC, WCF, SQL Server 2012/2014 or above, T-SQL and JavaScript/JQuerry/HTML5. CSI Interfusion (size:....  More
2 days ago

Redmond, WA 18-September-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.