At Mix 2010, ScottGu talked about the developer story of Windows Phone 7. As you know by now, that the developer story is on Silverlight. I have also seen some questions regarding Windows Phone and whether RIA Services can be used part in it.
Unfortunately, there is no in built support for the Mobile platform in RIA Services V1.0. I know that is a big bummer because every Silverlight developer is now a windows phone developer. Rest assured we do think RIA Services support for Windows Phone is very important, but it will not make it in our V1.0.
With that said, you can still enable a Windows Phone 7 application to communicate with a DomainService and here is how…
At PDC 09, we made a big change, where every DomainService is now a WCF Service. Windows Phone 7 has support for talking to a WCF Service and we can leverage that to our advantage and create a mobile application that talks to a domain service. Yes, you do not get the rich client experience RIA Services provides for Silverlight, but you still get all the benefits of the server richness in the V1 release.
Here is how you would do that:-
- Install the latest Silverlight Tools from here (contains RIAServices, SL4 Sdk, SL4 Runtime etc)
- Install the RIA Services March 2009 Toolkit from here
- Install the Mobile Tools CTP from here
Create a new domain service
I am going to skip over the section on how to create a domain service. You can find that at @brada’s post about exposing data from entity framework. Next we are going to configure the domain service to expose a soap endpoint.
Configuring a domain service to expose a soap endpoint
Now that you have have a DomainService configured, we are going to configure it to expose a soap endpoint. In the PDC preview bits this was exposed by default, but we have now scaled that back for various reason. In the RC release you would need to configure your DomainService to be exposed as a WCF Soap Endpoint. Here is how you would do that (assuming you have created a domain service already):-
- Install the RIA Services toolkit
- 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
- Once you have added the reference, open up your web.config file and add the following code under the <System.ServiceModel>
5: <add name=”soap” type=”Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=22.214.171.124, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />
- Also add the following in the web.config
2: <sectionGroup name=”system.serviceModel”>
3: <section name=”domainServices” type=”System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=126.96.36.199, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />
- Save the web.config
- Compile the application and run it.
- Open up a new Internet Explorer window and go to the following address http://localhost:[portnumber]/[SLprojectName]-Web-[DomainsServiceName].svc
- You will now see a familiar WCF service screen:-
- You can get to the WSDL by clicking on the wsdl link
Now that we have configured our DomainService to expose a SOAP endpoint, we can now write a Windows Phone application that talks to it.
Creating a Windows Phone application that communicates with a DomainService
- Launch up Microsoft Visual Studio 2010 Express for Windows Phone. Do not use the Visual Studio 2010 Add on as it does not have Add Service Reference for this Mobile CTP release.
- Create a new Windows Phone Application
- Click on the project and select “Add Service Reference’” and type in http://localhost:[portnumber]/[SLprojectName]-Web-[DomainsServiceName].svc in the Address and Click Go (make sure the service is running from the prior project)
- Click on Ok once the services is found.
- Open up MainPage.xaml and add a list box to it.
- Open up the Reference.cs from the Service References and look for the entity type that you exposed. In my case i am using Employee from NorthWind
- Override the ToString and configure it to display what you like. In my example i did the following
- Now go to MainPage.xaml.cs and add the following code.
- Call the LoadEmployee from anywhere. In my case, i just called it from the Ctor
- Run the application
- Voila! you now have a windows application talking to a DomainService
1: public override string ToString()
3: return LastName+“, “+FirstName +” Title: “ +Title;
1: public void LoadEmployee()
3: ServiceReference1.DomainService1soapClient client = new ServiceReference1.DomainService1soapClient();
4: client.GetEmployeesCompleted += new EventHandler<ServiceReference1.GetEmployeesCompletedEventArgs>(client_GetEmployeesCompleted);
9: void client_GetEmployeesCompleted(object sender, ServiceReference1.GetEmployeesCompletedEventArgs e)
11: this.listBox1.ItemsSource = e.Result.RootResults;
There is a very good sample posted at our code gallery site, that explains how you can do CRUD operation with the SOAP endpoint of a DomainService. You can find it here.