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.
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):
Here's the aspx page (GetImage.aspx):
And the relevant parts of the codebehind (GetImage.aspx.cs,
using
andnamespace
skipped):Test runs
Run 1, unmodified: The page loads fast, the output window shows a random mix of
Start
andEnd
s, which means that the requests get processed in parallel.Run 2, add empty
Session_Start
toglobal.asax
(need to hit F5 once in the browser, don't know why this is):Start
andEnd
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 ofGetImage.aspx
: The debug output shows multipleStart
s before the firstEnd
. 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.
Can't tell you what your debugger is doing (intellitrace? detailed logging? first-chance exceptions?), but you're still in the hands of the sessions ability to handle concurrent requests.
Source: ASP.NET Session State Overview, my emphasis