RIAServices – Proudly using SessionState within your DomainServices




As mentioned in my previous article How to build huge dynamically loading cross platform Silverlight Business Applications, I’m experimenting with the idea of a hybrid web application that has some parts implement as webforms, others as Silverlight.

A problem I encountered was that RIAService calls did not necessarily access to the same information as my webforms did.  An example is “SessionState”.  In the case of my company’s framework, the authentication system must be invoked prior to anything else within my domain services.  In addition the authentication system needs access to SessionState.


To do this I thought I could inherit from DataServiceFactory and add what I needed.  Unfortunately the class is marked as not inheritable.  The way around this is to encapsulate the class.

In the code sample below you can see an implementation of MyDataServiceFactory that encapsulates DataServiceFactory and returns an encapsulated HttpHandler that flags to ASP.Net that sessionstate is needed.


Imports System.Web
Imports System.Web.SessionState

Public Class MyDataServiceFactory
    Implements IHttpHandlerFactory
    Private moHandlerFactory As IHttpHandlerFactory

    Public Sub New()
        moHandlerFactory = New System.Web.Ria.DataServiceFactory
    End Sub

    Public Function GetHandler(ByVal context As System.Web.HttpContext, ByVal requestType As String, ByVal url As String, ByVal pathTranslated As String) As System.Web.IHttpHandler Implements System.Web.IHttpHandlerFactory.GetHandler

        Return New MyDataServicesHandler(moHandlerFactory.GetHandler(context, requestType, url, pathTranslated))

    End Function

    Public Sub ReleaseHandler(ByVal handler As System.Web.IHttpHandler) Implements System.Web.IHttpHandlerFactory.ReleaseHandler
        moHandlerFactory.ReleaseHandler(DirectCast(handler, MyDataServicesHandler).InnerHandler)
    End Sub
End Class
Public Class MyDataServicesHandler
    Implements IHttpHandler
    Implements IRequiresSessionState ' Let ASP.Net know that you need to use the session object

    Public InnerHandler As IHttpHandler
    Public Sub New(ByVal handlerToWrap As IHttpHandler)
        InnerHandler = handlerToWrap
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Return InnerHandler.IsReusable
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
        ' You can access context.Session here if you want to...
    End Sub
End Class


Of course to use this, you need to modify the web.config and change the two references to DataServicesFactory to point to your new class instead, which should be hosted in it’s own class library.


Share this post :


3 responses »

  1. Here’s a c# port…

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.SessionState;

    namespace BananaCorp.BananaApp.Web
    public class SessionDataServiceFactory : IHttpHandlerFactory
    private IHttpHandlerFactory _handlerFactory;

    public SessionDataServiceFactory ()
    _handlerFactory = new System.Web.Ria.DataServiceFactory();

    #region IHttpHandlerFactory Members

    IHttpHandler IHttpHandlerFactory.GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
    return new SessionDataServicesHandler(_handlerFactory.GetHandler(context, requestType, url, pathTranslated));

    void IHttpHandlerFactory.ReleaseHandler(IHttpHandler handler)


    //Let ASP.Net know that you need to use the session object
    public class SessionDataServicesHandler : IHttpHandler, IRequiresSessionState
    public IHttpHandler InnerHandler;
    public SessionDataServicesHandler (IHttpHandler handlerToWrap)
    InnerHandler = handlerToWrap;

    #region IHttpHandler Members

    bool IHttpHandler.IsReusable
    get { return InnerHandler.IsReusable; }

    void IHttpHandler.ProcessRequest(HttpContext context)
    //You can access context.Session here if you want to…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s