Is it possible to find out in Application_Start() which host is being used?
AFAIK no, it is not.
Based on your comments, I would say the persistence mechanism you are after is one of the server-side caching options (System.Runtime.Caching
or System.Web.Caching
).
System.Runtime.Caching
is the newer of the 2 technologies and provides the an abstract ObjectCache type that could potentially be extended to be file-based. Alternatively, there is a built-in MemoryCache type.
Unlike using a static method, caches will persist state for all users (and all domains) based on a timeout (either fixed or rolling), and can potentially have cache dependencies that will cause the cache to be immediately invalidated. The general idea is to reload the data from a store (file or database) after the cache expires. The cache protects the store from being hit by every request - the store is only hit after the timeout is reached or the cache is otherwise invalidated.
You can populate the cache the first time it is accessed (presumably after the request has already been populated, not in the Application_Start
event), and use the domain name as part of (or all of) the cache key that is used to look up the data.
public DataType GetData(string domainName)
{
// Use the domain name as the key (or part of the key)
var key = domainName;
// Retrieve the data from the cache (System.Web.Caching shown)
DataType data = HttpContext.Current.Cache[key];
if (data == null)
{
// If the cached item is missing, retrieve it from the source
data = GetDataFromDataSource();
// Populate the cache, so the next request will use cached data
// Note that the 3rd parameter can be used to specify a
// dependency on a file or database table so if it is updated,
// the cache is invalidated
HttpContext.Current.Cache.Insert(
key,
data,
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(10),
System.Web.Caching.CacheItemPriority.NotRemovable);
}
return data;
}
// Usage
var data = GetData(HttpContext.Current.Request.Url.DnsSafeHost);
If it is important, you can use a locking strategy similar to Micro Caching in ASP.NET (or just use that solution wholesale) so the data source does not receive more than one request when the cache expires.
In addition, you can specify that items are "Not Removable", which will make them survive when an application pool is restarted.
More info: http://bartwullems.blogspot.com/2011/02/caching-in-net-4.html