Using WCF RIA Services with your POCO, Part 2: Creating a Simple Domain Service

In part 1 of this series of posts I covered adding a new WCF RIA Services Class Library to your solution.  In this post, I'm going to cover creating a domain service and getting the code generated for the domain service.  Note that in this post I'm not even going to create a new POCO - I'll keep it even simpler.  In follow up posts I'll show how to use POCO and an POCO in an existing library.

To add a domain service for that will eventualy serve POCO objects, and hence create a web service to implement it, I add a new items called 'Domain Service Class' to the SocialBus.RIA.Web project:

When you press add on this dialog, you will then be presented with the 'Add New Domain Service Class' dialog.  The purpose of this dialog is to allow you to select a data context (or object context) from you solution for the derive the domain service.  Normally this adds a lot of smarts to the domain service for managing entities exposed by providers like the Entity Framework.  Since we are using POCO, we can just select 'OK':

This adds a file named 'SocialBusDomainService.cs' to the project, and it looks like the following:

There's not a whole lot to this class, but I'll shortly be adding methods to provide functionality to clients.

One thing that I'm going to do right now is add a quick Silverlight application to the solution for showing how to use these classes; right now we have no place to use these so we need to do that now.  So, in the RIA folder I add a new Silverlight application (named SocialBus.RIA.TestClient) and I already had a web project in the solution (named SocialBus.Web) so I serve up the Silverlight app from that project. 

I also make sure to 'Enable .NET RIA Services' on the dialog that pops up:

Two things now need to be done.  I need to add references for the RIA projects to both the new Silverlight client, and to SocialBus.web web site.  So, in the new Silverlight project I a reference to SocialBus.RIA, and to the SocialBus.Web I add a reference to SocialBus.RIA.Web.  What this does is allow the Silverlight app to use the code generated by VS.NET / RIA, and the reference to SocialBus.Web extends that solution to know how to serve up the domain services declared sin SovialBus.RIA.Web.

Before proceeding, a useful thing to do is to select 'Project -> Show All Files' from the main menu when the SocialBus.RIA project is selected.  RIA auto-generates code into a hidden folder anmed in that solution, and this lets you see that folder and the code files:

Right now there is not folder or files files as no code has been generated, even if you compile the solution.  This is because the domain service is empty at this point, and the code generation extensions see this and don't generate anything.  As a matter of fact, there is also a warning to this fact emitted by the compiler:

To get some code generated, I'll add a real simple method to the domain service.  I don't think this scenario is written anywhere, as every example seems based upon entities, and CRUD operations.  What I'm going to do is just add a method that adds two doubles and returns the result:

I'll be honest, I'm not sure this is even supposed to work as I can't find any info that supports it.  But it does.  Compile the solution and you now see code generated:


Double click the generated file to examine the generated code.  The at this point contains one class, SocialBusDomainContext, which is the class we will use in the Silveright project to call the domain service (partially shown here):

Note the 'Add' method which is generated to match the add method of the domain service we created.  There are actually two versions with different signatures that are created (to give different ways of calling the service):

One thing I want to point out at this point.  How is this code generation triggered, and how does it know what code to look at?  Open the properties for the SocialBus.RIA project, and it shows the following:

The .NET RIA Services link specifies a project, that when this project is compiled, that will be examined for domain services and entities, and for those identified code will be generated.

One more thing.  Because I'm using a Silverlight RIA Class Library and my own web site (not one auto generated by the wizard), the web.config on my site must be modified to know how to serve the RIA service from the domain service class.  Getting this to work requires adding three entries to web.config:

There's is still some magic in all of this on how the service is exposed, but that's a story for another post.

Okay, I think that's enough for this post.  In the next post I'll show how to call this service from Silverlight.


blog comments powered by Disqus

About the author

I'm a .NET, XAML, and iOS polyglot that loves playing with new things and making cool and innovative stuff.  I am also a Mac junkie.

I am Principal Technologist for SunGard Global Services in NYC, in their Advanced Technologies practice, and I work extensively with SunGard's energy and financial customers.

Note the the posting on this blog are my own and do not represent the position, strategies or opinions of SGS.

Widget TwitterFeed not found.

The file '/widgets/TwitterFeed/widget.ascx' does not exist.X

Recent Comments


Month List