Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7500855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:19:59+00:00 2026-05-29T20:19:59+00:00

When I create an empty Session_Start handler in Global.asax.cs it causes a significant hit

  • 0

When I create an empty Session_Start handler in Global.asax.cs it causes a significant hit when rendering pages to the browser.

How to reproduce:

Create an empty ASP.NET MVC 3 web application (I am using MVC 3 RC2).
Then add a Home controller with this code:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    return View();
  }
  public ActionResult Number(int id)
  {
    return Content(id.ToString());
  }
}

Next create a view Home/Index.cshtml and place the following in the BODY section:

@for (int n = 0; n < 20; n++)
{ 
  <iframe src="@Url.Content("~/Home/Number/" + n)" width=100 height=100 />
}

When you run this page, you’ll see 20 IFRAMEs appear on the page, each with a number inside it. All I’m doing here is creating a page that loads 20 more pages behind the scenes. Before continuing, take note of how quickly those 20 pages load (refresh the page a few times to repeat the loads).

Next go to your Global.asax.cs and add this method (yes, the method body is empty):

protected void Session_Start()
{
}

Now run the page again. This time you’ll notice that the 20 IFRAMEs load much slower, one after the other about 1 second apart. This is strange because we’re not actually doing anything in Session_Start … it’s just an empty method. But this seems to be enough to cause the slowdown in all subsequent pages.

Does anybody know why this is happening, and better yet does anybody have a fix/workaround?

Update

I’ve discovered that this behavior only occurs when the debugger is attached (running with F5). If you run it without the debugger attached (Ctrl-F5) then it seems to be ok. So, maybe it’s not a significant problem but it’s still strange.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:20:00+00:00Added an answer on May 29, 2026 at 8:20 pm

    tl;dr: If you face this problem with Webforms and don’t require write access to session state in that particular page, adding EnableSessionState="ReadOnly" to your @Page directive helps.


    Apparently, the existance of Session_Start alone forces ASP.NET to execute all requests originating from the same Session sequentially. This, however, can be fixed on a page-by-page basis if you don’t need write access to the session (see below).

    I’ve created my own test setting with Webforms, which uses an aspx page to deliver images.1

    Here’s the test page (plain HTML, start page of the project):

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title></title></head>
    <body>
        <div>
            <img src="GetImage.aspx?text=A" />
            <img src="GetImage.aspx?text=B" />
            <img src="GetImage.aspx?text=C" />
            <img src="GetImage.aspx?text=D" />
            <img src="GetImage.aspx?text=E" />
            <img src="GetImage.aspx?text=F" />
            <img src="GetImage.aspx?text=G" />
            <img src="GetImage.aspx?text=H" />
            <img src="GetImage.aspx?text=I" />
            <img src="GetImage.aspx?text=J" />
            <img src="GetImage.aspx?text=K" />
            <img src="GetImage.aspx?text=L" />
        </div>
    </body>
    </html>
    

    Here’s the aspx page (GetImage.aspx):

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetImage.aspx.cs" Inherits="CsWebApplication1.GetImage" %>
    

    And the relevant parts of the codebehind (GetImage.aspx.cs, using and namespace skipped):

    public partial class GetImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Debug.WriteLine("Start: " + DateTime.Now.Millisecond);
            Response.Clear();
            Response.ContentType = "image/jpeg";
    
            var image = GetDummyImage(Request.QueryString["text"]);
            Response.OutputStream.Write(image, 0, image.Length);
            Debug.WriteLine("End: " + DateTime.Now.Millisecond);
        }
    
        // Empty 50x50 JPG with text written in the center
        private byte[] GetDummyImage(string text)
        {
            using (var bmp = new Bitmap(50, 50))
            using (var gr = Graphics.FromImage(bmp))
            {
                gr.Clear(Color.White);
                gr.DrawString(text,
                    new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular, GraphicsUnit.Point),
                    Brushes.Black, new RectangleF(0, 0, 50, 50),
                    new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
                using (var stream = new MemoryStream())
                {
                    bmp.Save(stream, ImageFormat.Jpeg);
                    return stream.ToArray();
                }
            }
        }
    }
    

    Test runs

    • Run 1, unmodified: The page loads fast, the output window shows a random mix of Start and Ends, which means that the requests get processed in parallel.

    • Run 2, add empty Session_Start to global.asax (need to hit F5 once in the browser, don’t know why this is): Start and End alternate, showing that the requests get processed sequentually. Refreshing the browser multiple times shows that this has performance issues even when the debugger is not attached.

    • Run 3, like Run 2, but add EnableSessionState="ReadOnly" to the @Page directive of GetImage.aspx: The debug output shows multiple Starts before the first End. We are parallel again, and we have good performance.


    1 Yes, I know that this should be done with an ashx handler instead. It’s just an example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create a empty .mdb file? I'm using ADO.NET and C#. Thanks!
What's the simplest/canonical way to create an empty file in C#/.NET? The simplest way
If I create a cookie (in ASP.NET) like this cookie = new HttpCookie(MyCookie); cookie[foo]
I'm using the following code in an ASP.NET page to create a record, then
I am using ASP.NET MVC 2 Beta. I can create a wizard like workflow
What is the most efficient way to create empty ListBuffer ? val l1 =
How can I create an empty one-dimensional string array?
I need to create an empty .mdb file, so that I can then run
Both operations create an empty file and return the filename but mkstemp leaves the
How can I create an empty file at the DOS/Windows command-line? I tried: copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.