Configuring your DomainService for a Windows Phone 7 application
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>
1: <system.serviceModel> 2: 3: <domainServices> 4: <endpoints> 5: <add name="soap" type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 6: </endpoints> 7: </domainServices>
Also add the following in the web.config
1: <configSections> 2: <sectionGroup name="system.serviceModel"> 3: <section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 4: </sectionGroup> 5: </configSections>
Save the web.config
Compile the application and run it.
Open up a new Internet Explorer window and go to the following address https://localhost:[portnumber]/[SLprojectName]-Web-[DomainsServiceName].svc
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.
Click on the project and select “Add Service Reference'” and type in https://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
1: public override string ToString() 2: { 3: return LastName+", "+FirstName +" Title: " +Title; 4: }
Now go to MainPage.xaml.cs and add the following code.
1: public void LoadEmployee() 2: { 3: ServiceReference1.DomainService1soapClient client = new ServiceReference1.DomainService1soapClient(); 4: client.GetEmployeesCompleted += new EventHandler<ServiceReference1.GetEmployeesCompletedEventArgs>(client_GetEmployeesCompleted); 5: client.GetEmployeesAsync(); 6: 7: } 8: 9: void client_GetEmployeesCompleted(object sender, ServiceReference1.GetEmployeesCompletedEventArgs e) 10: { 11: this.listBox1.ItemsSource = e.Result.RootResults; 12: }
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
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.
Cheers!!
Comments
Anonymous
March 21, 2010
Deepesh, EXCELLENT article. >>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.<< Are you referring to "HRApp_WPF_VS2010" project? Thanks! ..BenAnonymous
March 21, 2010
Yes Ben!, I am referring to HRApp_WPF example.Anonymous
March 31, 2010
In that last snippet, I was thinking I'd see a domaincontext - is this client a domaincontext or just a normal wcf client proxy ?Anonymous
June 19, 2010
Nice! Saved me a lot of time. thumbs upAnonymous
November 14, 2010
Hi, After I added the service reference to the windows phone application I got an empty reference.cs file! It cannot generate the code for it but I created a windows app project then added the same service to it and it worked. That means it cannot create the proxy when client is a windows phone application. Any idea? Thanks.Anonymous
February 22, 2012
Hi, Mai i know how to call a java web services in wp7?