Share via


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

  1. Install the latest Silverlight Tools from here (contains RIAServices, SL4 Sdk, SL4 Runtime etc)
  2. Install the RIA Services March 2009 Toolkit from here
  3. 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):-

  1. Install the RIA Services toolkit

  2. 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 image

  3. 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>
    
  4. 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>
    
  5. Save the web.config

  6. Compile the application and run it.

  7. Open up a new Internet Explorer window and go to the following address https://localhost:[portnumber]/[SLprojectName]-Web-[DomainsServiceName].svc

  8. You will now see a familiar WCF service screen:-image

  9. You can get to the WSDL by clicking on the wsdl link

image

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

 

 

  1. 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.

  2. Create a new Windows Phone Applicationimage

  3. 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)image

  4. Click on Ok once the services is found.

  5. Open up MainPage.xaml and add a list box to it.

  6. 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

  7. 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:   }
    
  8. 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:      }
    
  9. Call the LoadEmployee from anywhere. In my case, i just called it from the Ctor

  10. Run the application

  11. Voila! you now have a windows application talking to a DomainService

image

 

 

 

 

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

  • Anonymous
    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 up

  • Anonymous
    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?