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.
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:
- Accept header of Request Message
- Content-Type of the HTTP request Message
- ResponseFormat parameter of service operation.
- WebHttpBehavior’s default format settings
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:
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.
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:-
<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:-
<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.
Follow to Resource Page here[/su_column][/su_row]
This comment has been removed by a blog administrator.