Feb
28
Written by:
Michael Washington
2/28/2012 8:28 AM
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_17.png)
You may have the need to access your custom LightSwitch WCF RIA Services from outside of LightSwitch using OData. This article will show you how.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb.png)
We start with the code from the article: WCF RIA Service: Combining Two Tables.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_1.png)
The first thing we do is add:
[EnableClientAccess()]
To the WCF RIA Class. This allows us to call the method from outside of LightSwitch.
Note: Now anyone can get to your web methods! So see: Simple Example To Secure WCF Data Service OData Methods http://openlightgroup.net/Blog/tabid/58/BlogID/1/Default.aspx
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_2.png)
We also add a reference to System.ServiceModel.DomainServices.Hosting.OData.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_3.png)
Next we click on the LightSwitch project in the Solution Explorer, and switch to File View.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_4.png)
We then select Show All Files.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_6.png)
We add the following code to the Configuration section in the Web.config:
<configSections>
<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" />
</sectionGroup>
</configSections>
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_7.png)
We also add the following code to the system.serviceModel tag (you will see an squiggly blue line under domainServices that you can ignore):
<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" />
</endpoints>
</domainServices>
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_8.png)
We switch back to Logical View and go into project Properties.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_9.png)
We change the application to a Web application.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_10.png)
When we launch the application we see the normal URL.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_11.png)
If we change the URL to:
WCF_RIA_Project-WCF_RIA_Service.svc/OData/
We see the OData feed.
You create the URL by using this format:
{namespace}-{class name}.svc/OData
Using LinqPad
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_12.png)
We can download and install LinqPad from: http://www.linqpad.net/.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_13.png)
We add a connection.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_14.png)
We select OData.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_15.png)
We enter the URL.
![image image](/Portals/0/Blog/Files/1/108/Windows-Live-Writer-Calling_12537-image_thumb_16.png)
We can now query the OData feed.
Also See
Simple Example To Secure WCF Data Service OData Methods
http://openlightgroup.net/Blog/tabid/58/BlogID/1/Default.aspx
Download Code
The LightSwitch project is available at:
http://lightswitchhelpwebsite.com/Downloads.aspx
1 comment(s) so far...
And that, my friends, just blew LightSwitch WIIIIIDE open!
By Jan Van der Haegen on
2/28/2012 9:24 AM
|