Partager via


HowTo: Access a WCF RIA DomainService from a WPF client

 

Introduction

If you have developed a solution using WCF RIA and Silverlight and would like to use additional clients such as WPF with your WCF RIA services this article will show you one way it can be done.

Building the Sample

This sample was designed for Visual Studio 2012, .NET 4.5, Silverlight 5 SDK, WCF RIA 1.0 SP2 SDK, and WCF RIA Services Toolkit (September 2011).

 
 

Description

This sample is based on the MSDN Walkthrough:  How to: Create a Domain Service that uses POCO-defined Entities

https://msdn.microsoft.com/en-us/library/gg602754(v=VS.91).aspx

 
 

WCF RIA has functionality to generate custom clients (DomainContexts) in Silverlight projects that does not mean it is impossible for other types of clients to use the Web Services created by the WCF RIA Services project.

WCF RIA Services generates WCF web services, which can be accessed by non-Silverlight clients. 

 
 

Here is how you can access your WCF RIA web service from WPF. 

 
 

 

Step 1: Expose a SOAP endpoint to your WCF RIA web service 

1.  In the web project that hosts the DomainService add a reference to Microsoft.ServiceModel.DomainServices.Hosting.dll from the WCF RIA Services normally located in

  C:\Program Files (x86)\Microsoft SDKs\RIA Services\v1.0\Toolkit\Libraries\Server\SP2\

2.  Update the Web.Config (WCFRIAPOCO.Web\Web.config) to specify a SOAP endpoint for your WCF RIA service. 

  2.1  Add the XML below to the <System.serviceModel> section.

XML

Edit|Remove

   <domainServices>  

         <endpoints>  

          <!--Add the Soap endpoint from the WCF RIA toolkit (seperate download)--> 

          <!--You'll need to add a reference to "C:\Program Files (x86)\Microsoft SDKs\RIA Services\v1.0\Toolkit\Libraries\Server\SP2\Microsoft.ServiceModel.DomainServices.Hosting.dll" for this to work--> 

           <add name="Soap" type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory,Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  />  

         </endpoints>  

       </domainServices>

 
 

2.2 If it is missing, add the XML below to the <configuration> section. 

 
 

XML

Edit|Remove

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

 
 

 
 

 

Step 2:  Access the WCF RIA service as a WCF web service from WPF

1.  Add a ServiceReference to the WCF RIA service. 

Tip:
Make sure the Web project is running, or this won't work (run the web project without debugging, or publush it to your web server).

Note:   The web service will be in the following format (YourWebSite/YourRiaProjectName-Web-YourDomainServiceName.svc where "." are replaced by "-") 

In the case of the sample the address is https://localhost:33653/WCFRIAPOCO-Web-SovereignDomainService.svc

 
 

 

Step 3. Using the WCF SoapClient
to access WCF RIA DomainService

1.  Access the WCF RIA web service as you would any WCF web service.  see WPFClient\MainPage.xaml 

C#

Edit|Remove

 public partial class MainWindow : Window 

    { 

        //Create an instance of the WCF RIA generated web service SOAP client 

        SovereignDomainServiceSoapClient _sovereignContext = new SovereignDomainServiceSoapClient(); 

 
 

        public MainWindow() 

        { 

            InitializeComponent(); 

 
 

            //Query the WCF RIA Webservice to get a List of Sovereigns whos reign ends by 1500 and assign it to the DataGrid for display 

            SovereignGrid.ItemsSource = this._sovereignContext.GetSovereignsByReignEnd(1500).RootResults; 

        } 

    }

 

 

Source Code Files

  • WcfRiaWpf.sln – The main solution file for the Web, SL, and WPF projects.
  • WCFRIAPOCO.Web\SovereignDomainService.cs – the code file for the DomainService from the MSDN walkthough.
  • WCFRIAPOCO.Web\Web.Config – The Web.Config file for the Web Project
  • WpfClient\MainWindow.xaml – The markup for the Main WPF window hosting the DataGrid
  • WpfClient\MainWiundow.xaml.cs- contains the code for the Main Window to call the WCF RIA web service

Comments

  • Anonymous
    January 06, 2013
    nice post, thanks!

  • Anonymous
    February 05, 2013
    It would be nice to get an example where the WCF service requires authentication.

  • Anonymous
    September 04, 2013
    This is not what most people demand: ability to use remote ORM context like it works in WCF RIA.