Archive

Posts Tagged ‘Reflection’

Determine current execution context (ASP.NET or Winforms)

October 5, 2007 LeandroDG 2 comments

I’ve searched over the internet for this and couldn’t find anything.

A method I’m working on should have a different behavior when called in a Web Application (Webforms) and a Windows Application (Winforms).

This is what I came up with, using Reflection:

	if (Assembly.GetEntryAssembly() != null)
	{
		// Running on Winforms context
	}
	else
	{
		// Running on ASP.NET context
	}

There are probably other ways to do it, I’ve tested this one and it works great.

Update (2007-16-10):

Turns out there is an easier way to do this, just got it from a co-worker. Instead of Assembly.GetEntryAssembly() you can use HttpContext.Current, but you’ll have to be careful with threading in ASP .NET forms, as seen in http://www.odetocode.com/Articles/112.aspx (HttpContext.Current could be null while running on ASP .NET).

VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)