Mends.One

Global.asax.cs events not firing after website publish

Asp.net, Global Asax

I've been looking around online for an answer to this for hours, so I'm hoping someone here can help.

I have a WebSite in VS 2010 (.Net 4.0 framework, C #) that has a Global.asax file in the root directory and a Global.asax.cs file in the App_Code folder. (Note that this is a WebSite, not a web application). When running this WebSite from my local development machine, everything runs as expected - more specifically the Session_Start event in the Global.asax.cs file fires and runs some code at the start of every session to get the user's logon credentials, which determine what they are allowed to see/edit in the application based on their security level.

However, once I publish this WebSite onto a Windows 2003 R2 server, none of the events in the Global.asax.cs file fire. (Only the Session_Start event has needed code in it, but I put code in Application_Start as well, just to see if it would fire - it did not).

Here's the Global.asax code:

<%@ Application CodeBehind="~\App_Code\Global.asax.cs" Inherits="Global" Language="C#" %>

Here's the Global.asax.cs code:

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

/// <summary>
/// Summary description for Global
/// </summary>
public partial class Global : System.Web.HttpApplication
{

    public Global()
    {

    // TODO: Add constructor logic here

    }

    protected void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
    }

    protected void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown
    }

    protected void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        try
        {
            Session["SessionSink"] = new SessionSink();             
            this.GetUserName();                              
        }
        catch (Exception ex)
        {
            string msg = "An error occurred in Global.asax::Session_Start";
            ExceptionWriter ew = new ExceptionWriter();
            ew.LogError(msg, ex);
        }
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 

    }
}

Some notes based on other answers/discussions I've read: Once published, PrecompiledApp.config DOES appear in the website's folder. In addition, App_global.asax.compiled and App_global.asax.dll appear in the bin folder.

Anyone have any idea what the issue could be? Why does this work from my local machine, but not once it's published to a server? Is there an IIS setting that I'm missing?

This is my first time using a asp.net "website", so any help would be appreciated. Thanks.

5
U
user2163572
Jump to: Answer 1

Answers (1)

You can circumvent that by not using the global.asax.cs at all; i.e. delete global.asax.cs and modify global.asax to

<%@ Application Language="C#" Inherits="System.Web.HttpApplication" %>
<script runat="server">
  public override void Init() {
    base.Init();
    var module = (SessionStateModule)Modules["Session"];
    module.Start += new EventHandler((sender, args) => {
        // create session sink here
    });
  }
</script>
0
O
Ondrej Svejdar

Related Questions