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 Public Class MyDataServiceFactory Public Sub New() 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 Public InnerHandler As IHttpHandler Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Imports System.Web.SessionState
Implements IHttpHandlerFactory
Private moHandlerFactory As IHttpHandlerFactory
moHandlerFactory = New System.Web.Ria.DataServiceFactory
End Sub
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 Sub New(ByVal handlerToWrap As IHttpHandler)
InnerHandler = handlerToWrap
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return InnerHandler.IsReusable
End Get
End Property
' You can access context.Session here if you want to...
InnerHandler.ProcessRequest(context)
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.
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)
{
_handlerFactory.ReleaseHandler(((SessionDataServicesHandler)handler).InnerHandler);
}
#endregion
}
//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…
InnerHandler.ProcessRequest(context);
}
#endregion
}
}
Sweet! Thanks for that.
I want to do this but sadly I don’t know how to use that sample… a working sample would have been nice.