How can a .NET code know whether it is running within a web server application?

前端 未结 7 1506
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 02:49

I have a library code, which should be aware whether it is executed in the context of a web server or standalone application server.

The obvious that comes to mind i

7条回答
  •  被撕碎了的回忆
    2020-12-06 03:13

    Trying to solve another problem, I found a good solution for this one. There is a private method System.Configuration.SettingsPropertyValue.IsHostedInAspnet, which does exactly what I need. Being a private method, I do not want to call it (though I could using reflection), but its implementation is trivial:

    private bool IsHostedInAspnet()
    {
        return (AppDomain.CurrentDomain.GetData(".appDomain") != null);
    }
    

    (according to Reflector)

    Looks like there is a special key in the app domain data - ".appDomain", which is set when running in ASP.NET web server.

    I will stick to that.

提交回复
热议问题