In a core assembly, which is run both in a windows service, and in a web application, I need to store information per user session. The service will have a single user sessi
I'd go for
HostingEnvironment.IsHosted
Note that there is a slight overhead incurred when you're using a method from an assembly like this, even when you don't intend to use it. (System.Web will be loaded and several classes could be initialized and JITed.) Also, there's a hard dependency on System.Web now, so you can't use it in a limited framework setting (currently IIRC only with the Client Profile).
Another way (although not as neat and documented), is to check
Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
If this returns web.config (or a casing variant thereof), it's probably a web application. (Although you can setup any appdomain with a config file named web.config, this is not a likely scenario.) This avoids taking a dependency on System.Web.
However, HostingEnvironment.IsHosted is intended to indicate whether an appdomain is configured to run under ASP.NET.