System.Reflection

  • Thread starter Thread starter JV
  • Start date Start date
J

JV

Is there a way to determine that an assembly is running under an ASP.NET web
application as opposed to a Windows application?

For example, in an ASP.NET application if I call
Assembly.GetExecutingAssembly(), can I determine that the current assembly
is a web application?
 
JV said:
Is there a way to determine that an assembly is running under an ASP.NET web
application as opposed to a Windows application?

For example, in an ASP.NET application if I call
Assembly.GetExecutingAssembly(), can I determine that the current assembly
is a web application?
You can check what class it's derived from, eg WinForm vs WebForm
 
JV,

I think a good way to test whether or not you are running in an ASP.NET
application is to access the static Current property on the HttpContext
class. If it is not running in ASP.NET, then the property should return
null.

Hope this helps.
 
Be aware if your solution doesn't have a reference to System.Web.dll, it
will throw an exception if you access System.Web.HttpContext.

bill

Nicholas Paldino said:
JV,

I think a good way to test whether or not you are running in an ASP.NET
application is to access the static Current property on the HttpContext
class. If it is not running in ASP.NET, then the property should return
null.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

JV said:
Is there a way to determine that an assembly is running under an ASP.NET
web application as opposed to a Windows application?

For example, in an ASP.NET application if I call
Assembly.GetExecutingAssembly(), can I determine that the current assembly
is a web application?
 
bill:

I think you meant to say "yield a compiler error", not "throw an
exception exception"....
 
Thanks for making me think about my response. <grin> I actually didn't
even mean exception, infact after reading my response, I think I just
randomly typed something in that sounded good...

Not all threads in a asp.net application have a HttpContext.Current attached
to it. You can use the HttpContext.Current if you want to know if the
calling thread ( current context) is currently processing a request through
the Http Pipeline, but it is not a reliable indicator that the calling
assembly is a windows application, or a web app. Timers, callbacks, user
created threads, etc will have a null value and can be from either.

Thanks for straightening me out Scott!

bill
 
Back
Top