Design Guidelines, Managed code and the .NET Framework

Brad Abrams

Subscribe to Brad Abrams: eMailAlertsEmail Alerts
Get Brad Abrams: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Blog Feed Post

Silverlight 4 + RIA Services - Ready for Business: Exposing WCF (SOAP\WSDL) 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. 

 

image

 

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. 

 

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

 

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

http://localhost:21516/BusinessApplication1-web-DishViewDomainService.svc

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

image

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.

image

 

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.

image






image

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

 

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.

 

image

 

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 });
 10: 

 

 

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

image

 

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.

Read the original blog entry...

More Stories By Brad Abrams

Brad Abrams is currently the Group Program Manager for the UI Framework and Services team at Microsoft which is responsible for delivering the developer platform that spans both client and web based applications, as well as the common services that are available to all applications. Specific technologies owned by this team include ASP.NET, Atlas and Windows Forms. He was a founding member of both the Common Language Runtime, and .NET Framework teams.

Brad has been designing parts of the .NET Framework since 1998 when he started his framework design career building the BCL (Base Class Library) that ships as a core part of the .NET Framework. He was also the lead editor on the Common Language Specification (CLS), the .NET Framework Design Guidelines, the libraries in the ECMA\ISO CLI Standard, and has been deeply involved with the WinFX and Windows Vista efforts from their beginning.

He co-authored Programming in the .NET Environment, and was editor on .NET Framework Standard Library Annotated Reference Vol 1 and Vol 2 and the Framework Design Guidelines.