RIA Services How to Load Only If Authenticated
Sunday, November 22, 2009 at 10:01AM One of the things I ran into in converting to the PDC release of WCF RIA Services is around error handling in the Domain Data Source. By default the PDC release handles error handling a little different than prior release in a good way. Previously, if you encountered an error and didn’t explicitly provide error handling it would silently sweep the error under the carpet. This worked great for demo applications because you could typically not worry about it. For real applications though this lead to not knowing what was wrong and the user left wondering why nothing was showing.
Now in the PDC release the error will be raised unless you handle it. This means that if there are times where you expect to encounter an error you need to make accommodations otherwise an unhandled exception will be raised. A good example of this is if you have marked your Domain Service as requiring authentication by adding the RequiresAuthentication attribute. This attribute will ensure that nobody accesses your Domain Service that isn’t authenticated. The result though is now if your application attempts to make a call to the Domain Service before authentication is completed you will get an error.
Where I hit this was I had a couple of Domain Data Source controls in one application that was set to auto load for the main page. There’s a few different ways to handle this but I wanted to share on that I was exploring to make it easy to handle this type of situation. The behavior I want is to have the Domain Data Source auto load but only if the user is authenticated. I could do this by turning off the auto load and manually calling the load once the user is authenticated. Another option is I could make Domain Data Source a little more smart to handle these by adding a OnlyIfAuthenticated property that would be checked during the loaded event.
I’m able to do this because I have a class SLRIADomainDataSource that inherits from DomainDataSource and allows me a place to put my custom logic that I want to be on every use of the domain data source in my application. You could also accomplish this same type of thing using behaviors. Both approaches are viable they just depend on how you enable the feature to work. One you add a property, the other you add the markup necessary to attach the behavior to the control. Behaviors require you to know the class exist where the inherited class the property just shows up in the property designer making it easy to enable. Behaviors are nice because they allow you to add logic to another control that you don’t own or is sealed.
So the following is the class were working with – notice it inherits from DomainDatasource. Also we have already hooked into the LoadingData event to allow us get control when the control is about to load data. The LoadingData method is called prior to the query happening so it’s the ideal place if we want to cancel it being performed. This is also where you would hook in to handle other pre-query type errors related to query composition.
public class SLRIADomainDataSource : DomainDataSource
{
public SLRIADomainDataSource()
{
this.LoadingData += new EventHandler<LoadingDataEventArgs>
(SLRIADomainDataSource_LoadingData);
}
}
Next, we are going to implement the handler for the LoadingData event. In this handler we will check OnlyIfAuthenticated to see if we have configured the Domain Data Source to only load if the user is authenticated. From the WebContextBase class we can access the status of the user to see if they are authenticated yet. If they aren’t authenticated we simply mark the query canceled in the event arguments.
void SLRIADomainDataSource_LoadingData(object sender, LoadingDataEventArgs e)
{
if (OnlyIfAuthenticated)
{
if (!WebContextBase.Current.Authentication.User.Identity.IsAuthenticated)
{
e.Cancel = true;
return;
}
}
}
In case your wondering WebContextBase used to be RiaContextBase in the prior CTP release of RIA Services and has just been renamed.
So with this additional logic the page developer doesn’t have to worry about special code each time to decide if the query should be started. You also won’t get un-expected errors if the domain data source tries to call the service when the user is not authenticated. The error handling in the new release of RIA Services is a welcome change to prevent silent errors from happening. It is also a little more consistent and intuitive in how the errors are presented so you don’t have to hook into as many places.
Reader Comments