Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if .NET code is running in an ASP.NET process?

Tags:

c#

.net

asp.net

I have an instance of a general purpose class that will be executed both under ASP.NET and a stand alone program. This code is sensative to the process where it is being run - that is, there are certin methods that should not called if running under ASP.NET. How do you determine if the code is executing in an ASP.NET process?

The solution I am currently using is answered below.


I wish someone would add a comment as to why this question has gotten downvoted and/or propose a better way to ask it! I can only assume at least some folks have looked at the question and said "what an idiot, ASP.NET code is .NET code".

like image 531
jr. Avatar asked Oct 16 '08 18:10

jr.


1 Answers

HttpContext.Current can also be null within ASP.NET if you're using asynchronous methods, as the asynchronous task happens in a new thread that doesn't share the HttpContext of the original thread. This may or may not be what you want, but if not then I believe that HttpRuntime.AppDomainAppId will be non-null anywhere in an ASP.NET process and null elsewhere.

like image 82
stevemegson Avatar answered Sep 28 '22 02:09

stevemegson