R – Sharepoint Master Pages and Session State

asp.netmosssession-statesharepoint

I’m using Heather Solomon’s minimal publishing master page, I’ve got a simple feature with an apsx page and a code behind file that does this

public class SSInformPage : Page
{
    protected Literal Literal1;

    protected override void OnLoad(EventArgs e)
    {
        //base.OnLoad(e);
        try
        {                
            HttpContext.Current.Session["Name"] = "Mr Ben";
            Literal1.Text = string.Format("{0} {1}", HttpContext.Current.Session["Name"].ToString(), Session.SessionID);
        }
        catch (Exception ex)
        {
            Literal1.Text = string.Format(ex.Message.ToString());
        }
    }

}

When this page loads in the browser I get the following error

Object reference not set to an instance of an object.

Yet if I switch to an out of the box master page (e.g. Black Vertical) the code works! I get the expected output of “Mr Ben {session ID}”

I’ve even created a new blank master page and copied the entire contents of the Black Vertical master page into it. I set the site to this new master page, I load the page expecting everything to work and receive the ‘Object reference…’ error. How does that work? It’s exactly the same master page yet the code only works if it’s an OOTB version.

I've enabled session state in central admin, the web.config file and the assembly is deployed to the GAC.

Update
From a suggestion on another forum it seems to be a trust issue, i was mistaken in thinking the solution deploys to the GAC, it was actually deploying to the Bin folder. I've tried to reset this but can't seem to get WSPBuilder to deploy to the solution to the GAC, although i've managed to get it to stop deploying to the web application's bin folder.

Does anyone know how to get a soltution to deploy to the GAC folder? this is my stsadm command

stsadm -o deploysolution -name Test.SessionState.Solution.wsp -immediate -url http://example -allowGacDeployment -allowCasPolicies -force

Udpate 2
No it's not a trust issue, I re-created my solution and ensures it deploys to the GAC. If I open a OOTB master page in sharepoint designer and click the save button it's will 'customise' the page. As soon as I do this my code stops working, if I then reset the master page back to the site definition my code starts working again!

I have a soltion file is anyone want to install this to see the behaviour.

Best Answer

I think i found the reason, it seems if a custom master page is wrapped in a solution this problem doesn't occur (most of the time).

Here's an article listing the issue and possible work arounds

https://stackoverflow.com/questions/981836/perform-find-and-replace-in-visual-studio-build-events