Silverlight TV Episode 26: Exposing SOAP, jSON and OData Endpoints from RIA Services

I recently recorded a Silverlight TV episode where I demoed configuring a RIA Services DomainService to expose SOAP, JSon and OData endpoints. The video went online earlier today and it can be found here.

Oh and be sure to check out for Silverlight TV Episode 28. It’s going to be awesome!! Stay Tuned!

image_3[1]

 

Why would you expose other endpoints?

As you all are aware, RIA Services has a great story OM and tooling story for Silverlight.What if you want a mobile application talking to a domain service or an Ajax application? There could be various different reasons why one would want to expose a different endpoint. The great thing is that enabling these endpoints requires minimal changes in your application.

 

What do I need to expose these endpoints?

You will need the following components:-

  1. Visual Studio 2010 or Visual Web Developer Express 2010
  2. Silverlight Tools for Visual Studio 2010 (contains WCF RIA Services for Visual Studio 2010)
    • Contains the OData endpoint
  3. WCF RIA Services Toolkit for SOAP and JSON endpoints

Creating Endpoints

Creating an OData Endpoint

The OData endpoint support is provided out of the box for you. Here is how you can expose an OData Endpoint.

Creating a new DomainService
  1. Create a new or open up an existing RIA Services Project.
  2. Right click on the server project and add a new DomainService class (assuming you have a DataModel in your project).
  3. When the DomainService Wizard comes up, select the model you want to expose and then check the “Expose OData Endpoint”
  4. Click Ok
  5. Now you have a DomainService that exposes 2 endpoints:-
    • Binary
    • Odata
Modifying an existing DomainService to expose an OData Endpoint as well
  1. Create a new or open up an existing RIA Services Project.

  2. Open up your DomainService

  3. Find the query method for your entity type. NOTE: This query method should be parameter less

  4. Add the following attribute on that query method [Query(IsDefault=true)]

        1: [Query(IsDefault=true)]
        2:     public IQueryable<Book> GetBooks()
        3:     {
        4:         return this.ObjectContext.Books.OrderBy(b => b.Title);
        5:     }
    
  5. Add a reference to System.ServiceModel.DomainServices.Hosting.Odata can be found in Program Files\Microsoft SDK’s\RIA Services

  6. Open up the web.config file and add the following section directly under configuration

        1: <configSections>
        2:     <sectionGroup name="system.serviceModel">
        3:       <section name="domainServices"
        4:                type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection,
        5:                System.ServiceModel.DomainServices.Hosting, 
        6:                Version=4.0.0.0, Culture=neutral, 
        7:                PublicKeyToken=31BF3856AD364E35" 
        8:                allowDefinition="MachineToApplication" 
        9:                requirePermission="false" />
       10:       
       11:     </sectionGroup>
       12:   </configSections>
       13:   
    
  7. Under the System.ServiceModel section add the following

        1: <domainServices>
        2:     <endpoints>
        3:       <add name="OData"
        4:            type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, System.ServiceModel.DomainServices.Hosting.OData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />        
        5:     </endpoints>
        6:   </domainServices>
        7:   
    
  8. Now compile and run the application

NOTE: The ODATA endpoint has very limited support in V1. There is no Update or LINQ query support in this release.

Creating a SOAP and JSON endpoint

In my earlier post, I had showed how to configure a domain service for a Windows Phone 7 application. In that post I had used the Soap endpoint. Configuring a soap and JSON endpoint needs the RIA Services toolkit. After you have installed the toolkit, you will need to do the following:-

  1. In the Server project(the one appended with .web) add a reference to Microsoft.ServiceModel.DomainServices.Hosting.dll from %ProgramFiles%\Microsoft SDKs\RIAServices\v1.0\Toolkit\Libraries\Server

  2. Open up the web config file and add the code snippet in Point no 6 for the OData endpoint

  3. Under the System.ServiceModel section add the following.

        1: <domainServices>
        2:     <endpoints>
        3:       
        4:       <add name="OData"
        5:            type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, System.ServiceModel.DomainServices.Hosting.OData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        6:            
        7:       <add name="soap"
        8:             type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        9:  
       10:       <add name="JSON"
       11:             type="Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
       12:       
       13:     </endpoints>
       14:   </domainServices>
    
  4. As you can, we have added the soap and JSON endpoints for the same DomainService. (the two lined just before the endpoints closing tag)

  5. With the additions of these config settings you now have your same Domain Service expose 4 endpoints:-

  • Binary
  • OData
  • SOAP
  • JSON

Note: You can use SOAP and Json endpoints to submit.

Consuming these endpoints

A domain service is a WCF Service; therefore it has a SVC path. You can now access the three endpoint we exposed using that.

The uri for these endpoints are:-

OData : https://localhost:[portnumber]/[SLprojectName]-Web-[DomainsServiceName].svc/OData/

SOAP: https://localhost:[portnumber]/[SLprojectName]-Web-[DomainsServiceName].svc

JSON: https://localhost:[portnumber]/[SLprojectName]-Web-[DomainsServiceName].svc/JSON/

With these exposed endpoints, you can talk to multiple clients. In the episode above, I used the following clients:-

  1. Excel Power pivot - Using the OData endpoint
  2. Windows Phone 7 – Using the SOAP endpoint
  3. AJAX client – Using the JSON endpoint

You can download the source code for the demo from here.

Cheers!!