How to enable LightSwitch as backend for your WP7 App
*Moved to: http://fluentbytes.com/how-to-enable-lightswitch-as-backend-for-your-wp7-app/
I have been asked to work with my two colleagues Roy Cornelissen and Willem Meints on some more cross device Phone Apps the past few weeks and we currently used RIA services as the backend of our story. With the release of LightSwitch Beta 2 and the nice architecture they use behind the scenes I wanted to give it a shot to Use LightSwitch as the backend of our Phone apps. especially since LightSwitch beta 2 now also has a go live license, we can even roll a production version of the app if we want.
So I already blogged about using RIA services for you WP7 app here.
the thing with LightSwitch is, that I needed to figure out a way to make it work the same way. Now since the LigthSwitch team really uses RIA services for their application model, I set the project settings to build a 3 tier Web application and published it to my local IIS instance on my box.
Now at first you I thought they even might use the same end points as I would create myself when building a domain service, but they generate at the backend one single domain service class that contains all your operations. For each entity you build in the model you will at least find the flowing operations, <entityName>_All and a <entityName>_Single and <entityName>_SingleOrDefault. ALl these operations have one argument of string that can contain additional Query syntax. I have not figured out yet all the possibilities of these strings yet, need to do some more sniffing with fiddler to find how that works. For simplicity sake, I just passed an empty string. Passing a null value will result in an exception when the async call completes!
The endpoint they use is named http://yourHostName/YourAppName/services/LightSwitchApplication-Implementation-ApplicationDataDomainService.svc
Now for communication with WP7 I need to use the SOAP endpoint and enable the cookie containers for authentication purposes. Enabling the SOAP endpoint, was extremely easy to do, just add the configuration section to your web.config file after deployment
<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“
allowDefinition=“MachineToApplication“ requirePermission=“false“ />
</sectionGroup>
</configSections>
next you go to the System.ServiceModel section and right after the ServiceHostingEnvironment tag you past the following:
<domainServices>
<endpoints>
<add name=“Soap“ type=“Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory,
Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35“ />
</endpoints>
</domainServices>
For this to function correctly you must register the Microsoft.ServiceModel.DomainService.Hosting assemblies in the GAC or copy the assemblies from the RIA services toolkit to the bin folder of the LightSwitch app.You can find the toolkit assemblies at the following location after install: <installdrive>:Program Files (x86)Microsoft SDKsRIA Servicesv1.0ToolkitLibrariesServer
Now after adding this, you can browse to the service endpoint location and see the WSDL is now available at this endpoint. So next part is to add a service reference to the WP7 App and use the endpoint where you can find the WSDL. Now calling the LightSwitch DomainService operations is done as follows:
public MainPage() { InitializeComponent(); BasicHttpBinding binding = new BasicHttpBinding(); EndpointAddress address = new EndpointAddress("http://YourHost/YourApp" + "/services/LightSwitchApplication-Implementation- ApplicationDataDomainService.svc/Soap"); binding.EnableHttpCookieContainer = true; var client = new ServiceNameSpace.ApplicationDataDomainServiceSoapClient(binding, address); client.CookieContainer = new CookieContainer(); client.EntityName_AllCompleted += new EventHandler<ServiceNameSpace.EntityName_AllCompletedEventArgs>(client_EntityName_AllCompleted); client.EntityName_AllAsync(""); } void client_EntityName_AllCompleted(object sender, ServiceNameSpace.EntityName_AllCompletedEventArgs e) { if (e.Error == null) { yourListBox.ItemsSource= e.Result.RootResults; } }
And voila, your LightSwitch RIA services are now servicing your WP7 app!
Cheers,
Marcel
Follow my new blog on http://fluentbytes.com