Wednesday, April 11, 2012

WCF RIA Services with JSON endpoint

Today I had the task to add a JSON endpoint for a WCF RIA service for which there exists a Silverlight client. I wanted to state some problems that I encountered such that maybe others can get it to work faster:

  1. Install the following two updates: 
    1. WCF RIA Services V1.0 SP2 
    2. WCF RIA Services Toolkit (September 2011)
  2. Add a reference to "Microsoft.ServiceModel.DomainServices.Hosting" to your web application project
  3. Add the following to your web.config into the tag
        <domainServices>
          <endpoints>
            <add name="OData" type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, System.ServiceModel.DomainServices.Hosting.OData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add name="Soap" type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add name="Json" type="Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
          endpoints>
        domainServices>
    
  4. Here the first pitfall arised; the application did not compile anymore complaining:
    The "CreateRiaClientFilesTask" task failed unexpectedly.
    Unrecognized configuration section system.serviceModel/domainServices.

    To resolve this add the following to the tag:
    <sectionGroup name="system.serviceModel">
          <section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowDefinition="MachineToApplication" requirePermission="false" />
        sectionGroup>
    
  5. According to some howtos the URL is composed as follows:
    http://localhost:[port]/[Name of Silverlight Project]-Web-[Name of Webservice]/Json/[Method name]
    e.g.: http://localhost:2002/MySilverlightApp-Web-DataAccessService/Json/GetArticles
  6. The above URL always returned 404 Not found
  7. My next pitfall was that my service class file was not in the web project but in a subfolder ("Services") of it
  8. To solve that I had to modify the URL as follows:
    http://localhost:[port]/[Name of Silverlight Project]-Web-[Folder name]-[Name of Webservice]/Json/[Method name]
    e.g.: http://localhost:2002/MySilverlightApp-Web-Services-DataAccessService/Json/GetArticles
  9. After that I finally got my JSON from my webservice :-)