Silverlight Playground
about Silverlight and other Amenities

How to configure an AuthenticationDomainService using Ria Services Class Library

2009-11-06T11:36:01+01:00 by Andrea Boschin

Using a Ria Services Class library to collect DomainServices and DomainContexts in a solution is a good practice because everyone know the value of having classes separated in projects instead of having them in the web project.

If you try to create this kind of project and then you add an Authentication Domain Service with the July 2009 CTP of Ria Services you will find two major drawbacks. First of all you will notice the generated code does not contains a RiaContext class. RiaContext is the class responsible to handle the User and roles and configure the type of Authentication to use in the application.

To workaround to this problem you may simply write by hand this class and put it in the client side part of the Ria Services library project. The code can be copied by an AuthenticationDomainContext generated in the web project but it is very simple and I show you the code here:

   1: public sealed partial class RiaContext : System.Windows.Ria.RiaContextBase
   2: {
   3:     #region Extensibility Method Definitions
   4:  
   5:     partial void OnCreated();
   6:  
   7:     #endregion
   8:  
   9:     public RiaContext()
  10:     {
  11:         this.OnCreated();
  12:     }
  13:  
  14:     public new static RiaContext Current
  15:     {
  16:         get
  17:         {
  18:             return ((RiaContext)(System.Windows.Ria.RiaContextBase.Current));
  19:         }
  20:     }
  21:  
  22:     public new User User
  23:     {
  24:         get
  25:         {
  26:             return ((User)(base.User));
  27:         }
  28:     }
  29: }

With this class you can configure the Authentication Domain Context as you did with the generated class. So you have to add it to the App.config, in the ApplicationLifetimeObjects tag and then add a kind of authentication like the WindowsAuthentication and FormsAuthentication.

The second drawback is that if you run the project this way and then you call the Login() method (or any other method) of the RiaContext you will get an error:

The DomainContextType is null or invalid and there are no contexts generated from AuthenticationBase<T>

This probably come from the fact that the RiaContext is not correctly initializated so when the class search for a valid domaincontext to handle the Login call it fails because it does not find it. The tip to correct this behavior is to specify the DomainContext property in the Authentication you have choosed. Here is how to change the App.xaml file:

   1: <Application.ApplicationLifetimeObjects>
   2:     <slpgria:RiaContext>
   3:         <slpgria:RiaContext.Authentication>
   4:             <appsvc:FormsAuthentication>
   5:                 <appsvc:FormsAuthentication.DomainContext>
   6:                     <slpgdc:MyAuthenticationDomainContext />
   7:                 </appsvc:FormsAuthentication.DomainContext>
   8:             </appsvc:FormsAuthentication>
   9:         </slpgria:RiaContext.Authentication>
  10:     </slpgria:RiaContext>
  11: </Application.ApplicationLifetimeObjects>

As you can see you have simply to give an instance of the generated Authentication Domain context class to the FormsAuthentication (or Windows Authentication as well). The RiaContext need to have this instance to make the calls to the BL methods in the Domain Service.

I really do not know if this behavior will be corrected in the next releases of RiaServices. The solution is pretty simple but take me some hours to understand what are going wrong. So I hope this may help you to spare some time making your experiments with the current release.

Categories:   TIPS | Networking
Actions:   E-mail | del.icio.us | Permalink | Comments (7) | Comment RSSRSS comment feed

Comments

November 10. 2009 12:25

Hi Andrea,
I've never had such problem with July2009 drop, RiaContext was properly generated and all i need to add to support Authentication was:

<Application.ApplicationLifetimeObjects>
      <this:RiaContext>
         <this:RiaContext.Authentication>
            <appRia:FormsAuthentication />
         </this:RiaContext.Authentication>
      </this:RiaContext>
   </Application.ApplicationLifetimeObjects>

inside app.xaml.
Are you sure about your installation?

Corrado Cavalli

November 10. 2009 13:02

Hi Corrado,

I've the same problem on 2 installations and I found people having the same issue in the Silverlight Forums.

http://forums.silverlight.net/forums/t/112195.aspx

Are you using a ria Services Class library and adding the AuthenticationDomainContext inside of the server side library project?

Andrea Boschin

November 11. 2009 07:19

Hi Andrea,
I can't no longer try but my tests never involved a RIA Class library probably the bug raises only on such context.

Corrado Cavalli

November 30. 2009 01:50

exactly what i needed.  Thanks mate!

Fabiano

November 30. 2009 13:05

Thank you.

Aditya Voleti

February 7. 2010 21:06

Social comments and analytics for this post

This post was mentioned on Twitter by aboschin: New Post: How to configure an AuthenticationDomainService using Ria Services Class Library - http://ping.fm/OvPba

uberVU - social comments

March 11. 2010 22:02

great post, are you a professional writer?

Candy Renaud

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading