Silverlight 4 + RIA Services - Ready for Business: Exposing WCF (SOAPWSDL) Services

Continuing in our series, I wanted to touch on how a RIA Services can be exposed as a Soap\WSDL service.   This is very useful if you want to enable the exact same business logic\data access logic is available to clients other than Silverlight.    For example to a WinForms application or WPF or even a console application.  SOAP is a particularly good model for interop with the Java\JEE world as well. 


First you need to add a reference to Microsoft.ServiceModel.DomainSerivves.Hosting.EndPoints assembly from the RIA Services toolkit. 




Then you need to edit the endpoints section of the domainserivces config in web.config  file.  Below I am showing the SOAP and OData endpoints enabled. 


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


Now, all you have to do is navigate in the browser to this URL:


Note the pattern is [namespace]-[typename].svc


And we get the very familiar WCF service debug page.

And if you really want to drill in, here is the full WSDL describing the service.



You are done on the server, now let’s look at how you consume that on the client.    For the sake of simplicity, I have created a new Console application.  Right click on the project and select Add Services Reference.




   1: var context = new DishViewDomainServiceSoapClient();
  2: var results = context.GetRestaurants();
  3: foreach (var r in results.RootResults)
  4: {
  5:    Console.WriteLine(r.Name);
  6: }


The code for query is very simple.  Line 1 creates the client proxy, line 2 does a synchronous call to the server. Then we simply loop through the results.




Updating the data is a little bit more interesting…    I need to create a ChanageSet that I populate with any updates. in this case I am updating just one value.  I also need to send the original value back to the server. 

   1: var changeSet = new ChangeSetEntry();
  2: var org = new Restaurant();
  3: org.ID = entity.ID;
  4: org.Name = entity.Name;
  5: entity.Name = "Updated:" + entity.Name;
  6: changeSet.Entity = entity;
  7: changeSet.OriginalEntity = org;
  8: changeSet.Operation = DomainOperation.Update;
  9: var updateResults = context.SubmitChanges(new ChangeSetEntry[] { changeSet });



And you can see the result of running this a couple of times:



For more information, see Deepesh’s post on Configuring your DomainService for a Windows Phone 7 application


For V1.0, this post describes the RIA Services story for WPF applications.  While it works, it is clearly not the complete, end-to-end solution RIA Services offers for Silverlight.   For example, you may want the full experience:  the DataContext, entities with validation, LINQ queries, change tracking, etc.  This level of support for WPF is on the roadmap, but will not make V1.  But if you like the RIA Services approach, you might consider DevForce from IdeaBlade or CSLA.NET which works for both WPF and Silverlight.   The great folks at IdeaBlade have offered me 10 free licenses to give out.  If you are interested in one, please let me know.