次の方法で共有


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!!

Comments

  • Anonymous
    June 01, 2010
    Error 1 The "CreateRiaClientFilesTask" task failed unexpectedly. System.Web.HttpException (0x80004005): Could not load file or assembly 'Microsoft.Web.DomainServices.WebControls, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Strong name signature could not be verified.

  • Anonymous
    June 01, 2010
    The workaround: In the SilverlightStore.Web project, from the References folder removeMicrosoft.Web.DomainServices.WebControls. This reference points to the Microsoft.Web.DomainServices.WebControls.dll file in the Lib folder. Add a reference to Microsoft.Web.DomainServices.WebControls found in the c:program filesmicrosoft sdks... on local machine

  • Anonymous
    June 17, 2010
    This is great, thanks... can you tell me how I then configure all the DataServiceConfiguration properties that I normally would in DateService<T>.InitializeService when using oData in the 'normal' way?

  • Anonymous
    July 08, 2010
    This is fantastic stuff guys.  Keep up the good work on the toolkit.  Thank you very much for adding the Json endpoint factory, it's turned out to be the best part of RIA for me.

  • Anonymous
    August 24, 2010
    Hello I had installed all the latest updates required for OData,SOAP, JSON endpoints. But when i run the sample application for odata, i am getting an Error Stating "Service  Endpoint Not Fount". Can you please let me know what could be reason for this? Note: I tried running another sample application from net that expose odata endpoint, it was able to show me the atompub. The same when i tried executing all the steps, i am shown "Endpoint not found" regards Dhinesh Kumar

  • Anonymous
    August 25, 2010
    @Dhinesh - do you have the RIA Services Toolkit installed?

  • Anonymous
    August 29, 2010
    The comment has been removed

  • Anonymous
    September 06, 2010
    Hi Deepesh, Is there a way to use soap or json end points together with the authentication services provided by ria services? I have a winforms app which I want to connect to my domain service using the authentication I have already set up in my RIA services project. I am wondering whether there is a good way to use Client Application Services?  Unfortunately I cannot find any documentation about this. Many thanks, Matt

  • Anonymous
    September 15, 2010
    @Ron - did you manage to fix this problem?  I'm having the same issue.

  • Anonymous
    September 16, 2010
    Hi jasonxz, any is idea for this problem?

  • Anonymous
    October 08, 2010
    Ron, Jasonxz, Ingphillip, I'm getting the same error. find a solution to this? I've got the dll referenced, the configSection correct, but it simply won't validate the domainServices element. >:

  • Anonymous
    October 08, 2010
    you also need a reference to: c:Program FilesMicrosoft SDKsRIA Servicesv1.0LibrariesServerSystem.ServiceModel.DomainServices.Server.dll be advised that the *.DomainServices.Hosting.dll that you need to reference is in the v1.0TOOLKITLibraries folder. (in case you see the non-toolkit version and think you've got it referenced already)

  • Anonymous
    January 19, 2011
    In another post you talked about WCF webservices and performance in Silverlight 3 suggesting that binary encoding over http was a good bet. msdn.microsoft.com/.../ee294456.aspx - Could you show us the custom binding - all the configuration-  for RIA and WP7 using basicHTTP, SOAP and binary encoding? - WP7 needs all the performance it can get

  • Anonymous
    July 13, 2011
    This is good stuff.. But could you also talk about authentication, roles and profiles to make it seamless in other clients including Windows Phone with RIA Services. Is there a resource for it yet?

  • Anonymous
    July 28, 2011
    Hi Abani, My posts series talk about opening a RIA Services layer to other types of clients (WP7, WPF, HTML5): blogs.msdn.com/.../how-to-open-a-wcf-ria-services-application-to-other-type-of-clients-review-of-the-initial-application-1-5.aspx It also covers the authentication part. Regards, David Rousset http://blogs.msdn.com/davrous