rest service with webhttpbinding crossdomain configuartion

Prerequisite - understanding of WCF and WebHTTPBinding.

The motto of this article is not to elaborate - how to create a WCF service with webhttpbinding ,we already have lot of articles on it,rather to explain what needs to be done if we want our service to be

  • Directly consumed by variety of clients ,which are expecting different format JSON/ XML /text.
  • Enabling Cross domain to our service
  •  Performance configuration attributes

Here are the bullets

PUBLISHERS - SERVICE

Append headers in Global.asax.cs (I am assuming that we are hosting our website in IIS)

 protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }

the above code is useful when we are using various browsers especially Mozilla.If we hit our service and trying to see the header information...
Under NET TAB you find instead of GET /POST there is OPTIONS which we never heard about.



you can google about verb OPTIONS.
In order to cater this particular need we would like to add below in our configuration system.webserver



<httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Headers" value="Content-Type" />
            </customHeaders>
  </httpProtocol>


make sure we also enable cross domain scripting by adding

<webHttpBinding>
                <binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
            </webHttpBinding>

in ASP.net 4.0 there are attributes which enable format output by auto detecting what is client expecting (JSON/XML/text) and the tag also specifies the default format.

therfore in endpoint behaviours of your webhttp binding specify

<endpointBehaviors>
                <behavior name="webHttp">
                    <webHttp automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Xml" helpEnabled="true" />
                </behavior>
            </endpointBehaviors>

and refer the behaviour configuration

<endpoint binding="webHttpBinding" contract="namesapce.IContractName" behaviorConfiguration="webHttp">
                </endpoint>


we may also like to consider dropping clientaccesspolicy.xml and crossdomainpolicy.xml for clients like silverlight.

PERFORMANCE/Load/Throttling

Performance of the web-service can not be governed by single variable ,it has to do with lot of factors like code /loops/configuration

here we will talk about the configuration that might help us

<serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="100" maxConcurrentSessions="100"/>

the above parameters needs to fine tuned, depending upon what are we expecting out of our service and the server on which it needs to be deployed


CLIENT :- CONSUMERS

Once we have done above settings , we will find that our rest service can be directly consumed in various client/browsers , when we ask for XML format.However when a client ask for JSON format  there are couple of browsers like IE which will throw error .

We may like to enable corrs if we are using jquery before sending a call to service.

$.support.cors = true;


That's all :)



No comments:

Post a Comment