Silverlight and WCF RIA Services (4 – RIA Services Libraries)

I wanted to write a little about some authentication experiments with WCF RIA Services but I got a little distracted on the way and thought I’d write it up a bit of a preamble about the “WCF RIA Services Class Library”.

The easiest way of creating a RIA Services Silverlight client is to do File->New Project in Visual Studio;


and then on the follow up dialog;


which gives you two projects – the Silverlight client project and the hosting web application project.

It also creates a RIA Services link from the Silverlight client project to the Web project such that the code-generation process will generate code directly into your Silverlight project.

That’s ok but it lacks flexibility in that;

  1. if you had a second Silverlight client that wanted to consume the same domain services then you don’t really want to have the code for those domain services generated into both client projects
  2. if you had a second web site that wanted to expose the same domain service then you probably want that domain service packaged up into a library in order to reference it in multiple places

The “RIA Services Class Library” project helps with this.

I wanted to experiment with Authentication a little and so it made sense for me to want to have my ultimate solution deployed in IIS ( rather than Cassini ) so the route that I took is;

New Website in IIS ( File->New WebSite )


New Silverlight Project ( File->Add New Project )


with appropriate settings ( note – no RIA Services link here );


and so now I have a Silverlight application hosted by my website. Next, I’ll add;

WCF RIA Services Class Library ( File->Add New Project )


and so now I have 4 projects in play ( with two Class1.cs files to delete Smile );


Set up References

I now need to tie the right projects together. My Silverlight project needs to reference my AuthRiaLibrary project and my website needs to reference my AuthRIALibrary.Web project as in the previous picture above so that’s easy enough.

Add a Domain Service

I’m then at the point where I can add a DomainService to my AuthRIALibrary.Web project via the “Add New Item” dialog;


and then adding the minimal code to make that into a “service”;

  public class MyDomainService : DomainService
    public int Add(int x, int y)
      return (x + y);

and then building causes code generation to emit code into my AuthRIALibrary project and ( because it’s referenced from my AuthClient project ) but I also need to ensure that my AuthClient project references System.ServiceModel.DomainServices.Client;


and then I can write some code in my client such as;

 AuthRIALibrary.Web.MyDomainContext ctx = new AuthRIALibrary.Web.MyDomainContext();

      InvokeOperation<int> op = ctx.Add(10, 20);

      op.Completed += (sender, args) =>
          if (op.Error == null)
            int result = op.Value;

but it won’t work because the configuration on my website isn’t right.

Configuring the Web Site

I need to make sure that the RIA Services bits are linked into the configuration on my website so I need to pop open web.config and alter it;

    <compilation debug="true"
    <modules runAllManagedModulesForAllRequests="true">
      <add name="DomainServiceModule"
           type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" />

and I can run my tiny bit of code and call the service. There’s a need here to ensure that the website has access to the right assemblies for RIA Services which ( from the docs ) looks to involve making sure you’ve done a;

msiexec /i RiaServices.msi SERVER=TRUE

to drop them into the GAC or that you deploy the relevant assemblies in the bin folder of each web site that needs them.