views:

447

answers:

1

I want to create a web service for my clients so they can display their data on their own web sites. Since I will not know what platform each client is running, what would be the best solution to create a proxy to the WCF service that can be used by all browsers? Also, I am not sure how I should present the data. Let's assume my users do not have any development skills. I will, by some other interface, give the user the ability to download the code needed to create the request and then process the response. Would it be better to parse an xml response on the client side then create the list of data or have the list already formatted (in a string) and have the client do a document.write? I have looked at a few solutions, but they seem to require the use of an ASP page with a script manager. Like I said, I would like something generic enough to use different browsers. Mainly IE and FireFox.

Daniel

+1  A: 

First, since you don't want to depend on Microsoft Ajax ScriptManager, don't use <enableWebScript /> in the endpointBehaviors/behavior. It is Microsoft-specific JSON.

Fortunately, however, WCF makes it very easy to allow your client to decide whether they want XML or generic JSON.

  1. Use the <webHttp /> behavior.

    <endpointBehaviors>
    <behavior name="My.WcfServices.webHttpBehavior">
    <webHttp />
    </behavior>
    </endpointBehaviors>

  2. Create a custom WebServiceHost and custom property attribute as described in Damian Mehers' blog, WCF REST Services. In Mehers' code, the type is determined by the request content type. You may want to extend it to examine the URL, for example, .xml or .json or ?format=xml|json.

  3. In the SerializeReply method, examine the URL.

    Message request = OperationContext.Current.RequestContext.RequestMessage;
    Uri url = request.Properties["OriginalHttpRequestUri"] as Uri;
    // Examine ?format query string
    System.Collections.Specialized.NameValueCollection colQuery = System.Web.HttpUtility.ParseQueryString(url.Query);
    string strResponseFormat = colQuery["format"];
    // or examine extension
    string strResponseFormat = url.LocalPath.Contains(".json") ? "json" : "xml";

  4. Define your method(s)

    [OperationContract]
    [WebGet(UriTemplate="Hello.{responseFormat}")] // or "Hello?format={responseFormat}"
    [DynamicResponseType]
    public string Hello(string responseFormat)
    {
    return "Hello World";
    }

Example URLs:
http://localhost/myrest.svc/Hello.xml
http://localhost/myrest.svc/Hello.json
or
http://localhost/myrest.svc/Hello?format=xml
http://localhost/myrest.svc/Hello?format=json

  1. Both JSON and XML are easy to consume across browsers. Libraries, such as jQuery for JSON and Sarissa for XML make it even easier.

NOTE: If you see error "Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding.", add the baseAddressPrefixFilters element and add localhost (or whatever your domain) to IIS Host Header Names.

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
 <baseAddressPrefixFilters>
  <add prefix="http://localhost"/&gt;
 </baseAddressPrefixFilters>
</serviceHostingEnvironment>
Doug D
Now, that just ROCKS!!!! This is the best post I have seen on this topic!!!!
DDiVita