I have a .NET infra code running both within the IIS worker process and within a desktop client app. How can the .NET code determine whether it is running within an IIS worker process?
I know that I could check the name of the process (w3wp.exe, for instance), but I would like a more robust approach.
Thanks.
EDIT1
I wish to make a side note. This is not a production need. I need this information to enable certain scenarios useful during the development and testing phase. Specifically to ease the testing of secure vs non secure configurations.
You can use properties of the HttpRuntime class which will be null if the current hosting environment is not ASP.Net, e.g.:
This approach is more robust than using ‘HttpContext.Current != null’ as this will give false negatives under ASP.Net environments where the current thread is not a request thread e.g. it’s a ThreadPool thread used directly or indirectly as a result of using Task Parallel Library for instance.