views:

47

answers:

1

It seems that CompilationMode=Never doesn't permit Sessions to be properly wired.

It first complains that the EnbaleSessionState directive is not allowed on this page.

Explicitly assigning the System.Web.SessionState.IRequiresSessionState to the Page [1] avoids the null reference exceptions (around .Session access) but still doesn't persist or wire-up sessions correctly.

Has anyone successfully used ASP.NET Sessions with CompilationMode=Never?

Conceptually, why should these be disjoint??

[1] - http://msdn.microsoft.com/en-us/library/system.web.ui.compilationmode.aspx

A: 

I don't know the exact situation you are in but what you say is correct - Page + IRequiresSessionState = Session Available. Here is what you do. First define a base class for your pages which will receive session state:

public class BasePage : Page, IRequiresSessionState
{
}

Then in your NoCompile Page you do the following declaration:

<%@ Page Language="C#" CompilationMode="Never" Inherits="BasePage" %>

Works as expected. Session state is available. Now little more about your second question: "Conceptually, why should these be disjoint??". By default the Page

public class Page : TemplateControl, IHttpHandler
{
}

class doesn't implement IRequiresSessionState and thus have no session state. What ASP.NET does for you is compiling a class for you at runtime through which it provides the session - i.e. if I define a page called Default.aspx, with code behind class that implements Page, nowhere I explicitly implement IRequiresSessionState. But ASP.NET compiles ours Default.aspx UI into a class called:

public class default_aspx : Default, IRequiresSessionState, IHttpHandler
{
}

which now explicitly says that it wants session state to be delivered. This is because "EnbaleSessionState" in the @Page directive is by default set to True. Now when you say that default.aspx is a non compile unit by specifying CompilationMode="Never", then this class is never generated and you never get the Session State, which makes the use of "EnbaleSessionState" not meaningful and thus disabled.

Ivan Zlatanov