locked
what is wcf default data transfer format RRS feed

All replies

  • User-1509636757 posted

    The WCF Web HTTP programming model allows you to dynamically determine the best format for a service operation to return its response in. Two methods for determining an appropriate format are supported: automatic and explicit. For Automaric formatting: When enabled, automatic formatting chooses the best format in which to return the response. It determines the best format by checking the following, in order:

    1. The media types in the request message’s Accept header.
    2. The content-type of the request message.
    3. The default format setting in the operation.
    4. The default format setting in the WebHttpBehavior.

    Explicit Formatting: As the name implies, in explicit formatting the developer determines the best format to use within the operation code. If the best format is XML or JSON the developer sets Format to either Xml or Json. If the Format property is not explicitly set, then the operation’s default format is used.

    For more information you can check this msdn url discussing the same thing: WCF Web HTTP Formatting

    hope it helps./.

    Tuesday, January 21, 2014 5:16 AM
  • User88744855 posted

    can u plzz point out where to set Automatic formatting or explicit Automatic formatting option in config file. thanks

    Tuesday, January 21, 2014 5:59 AM
  • User-1509636757 posted

    for Automatic fomatting you can achieve that by code:

          WebHttpBehavior whb = sep.Behaviors.Find<WebHttpBehavior>();
          if (whb != null)
          {
             whb.AutomaticFormatSelectionEnabled = true;
          }

    Automatic formatting can also be enabled through configuration. You can set the AutomaticFormatSelectionEnabled property directly on the WebHttpBehavior or using the WebHttpEndpoint. The following example shows how to enable the automatic format selection on the WebHttpBehavior.

      <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp automaticFormatSelectionEnabled="true" />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <standardEndpoints>
          <webHttpEndpoint>
            <!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
            <standardEndpoint name="" helpEnabled="true" />
          </webHttpEndpoint>
        </standardEndpoints>
      </system.serviceModel>
    

    The following example shows how to enable automatic format selection using WebHttpEndpoint.

    <system.serviceModel>
        <standardEndpoints>
          <webHttpEndpoint>
            <!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"  />
          </webHttpEndpoint>
        </standardEndpoints>
      </system.serviceModel>
    

    For Explicit formatting: The following example checks the format query string parameter for a format to use. If it has been specified, it sets the operation’s format using Format.

    public class Service : IService
    {
        [WebGet]
         public string EchoWithGet(string s)
        {
             // if a format query string parameter has been specified, set the response format to that. If no such
             // query string parameter exists the Accept header will be used
            string formatQueryStringValue = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"];
            if (!string.IsNullOrEmpty(formatQueryStringValue))
            {
                 if (formatQueryStringValue.Equals("xml", System.StringComparison.OrdinalIgnoreCase))
                 {
                      WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Xml;
                 }
                 else if (formatQueryStringValue.Equals("json", System.StringComparison.OrdinalIgnoreCase))
                {
                    WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
                }
                else
                {
                     throw new WebFaultException<string>(string.Format("Unsupported format '{0}'", formatQueryStringValue), HttpStatusCode.BadRequest);
                }
            }
            return "You said " + s;
        }
    

    If you need to support formats other than XML or JSON, define your operation to have a return type of Message

    Reference link: WCF Web HTTP Formatting

    hope it helps./.

    Tuesday, January 21, 2014 6:37 AM