Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Silverlight and WCF RIA Services (4 – RIA Services Libraries)

Blogs

Mike Taulty's Blog

Elsewhere

Archives

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;

image

and then on the follow up dialog;

image

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 )

image

New Silverlight Project ( File->Add New Project )

image

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

image

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 )

image

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

image

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;

image

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

  [EnableClientAccess()]
  public class MyDomainService : DomainService
  {
    [Invoke]
    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;

image

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;

<configuration>
  <system.web>
    <compilation debug="true"
                 targetFramework="4.0"/>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="DomainServiceModule"
           type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </modules>
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>


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.


Posted Fri, Jun 25 2010 3:53 PM by mtaulty

Comments

DotNetShoutout wrote Silverlight and WCF RIA Services (4 – RIA Services Libraries) - Mike Taulty
on Fri, Jun 25 2010 5:12 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Martin wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Sat, Jun 26 2010 12:26 AM

Hi Mike,

If you create a Silverlight Business Application (as opposed to a Silverlight Application) at the start, you don't need to edit the web.config at the end.

Cheers,

Martin

Martin wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Sat, Jun 26 2010 12:27 AM

Hi Mike,

If you create a Silverlight Business Application (as opposed to a Silverlight Application) at the start, you don't need to edit the web.config at the end.

Cheers,

Martin

mtaulty wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Tue, Jun 29 2010 10:14 AM

Hi Martin,

Yep, agree - there's quite a lot going on in that business application template though so I was avoiding it for this particular post :-)

Mike.

Sriram wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Wed, Jun 30 2010 9:23 PM

First time i stumbled upon your website , you are genius. will check your videos & site every day.

Mike Taulty's Blog wrote Silverlight and WCF RIA Services (5–Authentication)
on Fri, Jul 2 2010 2:51 PM

It’s a fairly common requirement that a business service authenticates a client and it’s usually (at

Community Blogs wrote Learning WCF RIA Services
on Sat, Jul 10 2010 8:34 PM

In my quest to learn WCF RIA Services I read a lot of articles and I thought I could share the most useful

Eugene wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Sun, Jul 11 2010 12:47 AM

Hi Mike,

I've been trying to get your example about RIA Service Library working, but I'm getting following exception:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.21022; .NET CLR 3.5.30729; InfoPath.2; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)

Timestamp: Sat, 10 Jul 2010 21:51:19 UTC

Message: Unhandled Error in Silverlight Application

Code: 4004    

Category: ManagedRuntimeError      

Message: System.ServiceModel.DomainServices.Client.DomainOperationException: Invoke operation 'Add' failed. The remote server returned an error: NotFound. ---> System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.

  at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)

  at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)

  at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)

  --- End of inner exception stack trace ---

  at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)

  at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)

  at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)

  --- End of inner exception stack trace ---

  at System.ServiceModel.DomainServices.Client.WebDomainClient`1.EndInvokeCore(IAsyncResult asyncResult)

  at System.ServiceModel.DomainServices.Client.DomainClient.EndInvoke(IAsyncResult asyncResult)

  at System.ServiceModel.DomainServices.Client.DomainContext.CompleteInvoke(IAsyncResult asyncResult)

  --- End of inner exception stack trace ---

  at System.ServiceModel.DomainServices.Client.OperationBase.Complete(Exception error)

  at System.ServiceModel.DomainServices.Client.InvokeOperation.Complete(Exception error)

  at System.ServiceModel.DomainServices.Client.DomainContext.CompleteInvoke(IAsyncResult asyncResult)

  at System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass38.<InvokeOperation>b__34(Object )    

Line: 56

Char: 13

Code: 0

URI: localhost/.../AuthClientTestPage.aspx

Would you have any suggestions what could be wrong?

Thank you very much,

Eugene.

Anonymous wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Sat, Jul 17 2010 1:20 PM

Just to point a bug in WCF RIA Services v1.0:

forums.silverlight.net/.../442690.aspx

irnbru wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Wed, Jul 21 2010 12:28 PM

Could you please explain in more details what went wrong before you fix the web configuration file? And what exactly did you change in the web configuration file in the "Configuring the Web Site" part? Thank you!

irnbru wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Wed, Jul 21 2010 12:49 PM

Sorry I forgot to ask you about the references. Do you know where I can find more information about them? Because it is good to know them, but it is even better to understand why and how they work...

Thank you :)

mtaulty wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Thu, Jul 22 2010 1:22 PM

Hi,

irnbru - what "went wrong" is that the website wouldn't by default have that content that I pasted into the "Configuring the Web Site" part. So, I pasted that into my web.config and all was well.

Mike.

Chris Arnold wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Mon, Jul 26 2010 7:42 PM

Hi Mike,

I followed your example but get a System.ServiceModel.DomainServices.Client.DomainOperationException: "Invoke operation 'Add' failed. The remote server returned an error: NotFound.".

I'm running the example from the VS2010 "web server" rather than IIS. Is that my problem?

Thanks.

KT wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Tue, Aug 3 2010 12:48 AM

To Chris Arnold:

Yes, I got the same error and it went away when re-configured VS to use IIS instead of Cassini.

Great series of articles Mike.  I don't know what the .NET community would do without you :)

Sander wrote re: Silverlight and WCF RIA Services (4 – RIA Services Libraries)
on Tue, Aug 10 2010 11:32 AM

Hi Mike,

Great article, just what I needed, thanks!