是否可以在Application _Start中获得主机? [英] Is it possible to get the host in Application _Start?

查看:77
本文介绍了是否可以在Application _Start中获得主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定了多个主机名的网站.是否可以在Application_Start()中找到正在使用的主机?

I have a website with multiple host names bound to it. Is it possible to find out in Application_Start() which host is being used?

我知道我现在没有请求,所以我可能没有考虑,但是我知道访问该应用程序的每个主机都将在一个新的应用程序池下运行.因此,只是想知道我是否可以在IIS中查询任何可以告诉我当前应用程序池使用的主机名的信息?

I understand that I don't have a request at this point so I'm thinking probably not, however I know that each host through which the application is accessed will run under a new app pool. So just wondering if there's anything I can query in IIS that might tell me what hostname is being used with the current app pool?

推荐答案

是否可以在Application_Start()中找到正在使用的主机?

Is it possible to find out in Application_Start() which host is being used?

AFAIK不,不是.

AFAIK no, it is not.

根据您的评论,我想说您所追求的持久性机制是服务器端缓存选项之一( System.Web.Caching ).

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是这两种技术中的较新版本,并提供了抽象的

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.

您可以在第一次访问缓存时填充缓存(大概是在填充请求之后,而不是在Application_Start事件中),并将域名用作缓存密钥的一部分(或全部),用于查找数据.

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);

如果重要,则可以使用类似于在ASP.NET中进行微缓存(或仅批发使用该解决方案),因此当缓存过期时,数据源不会收到多个请求.

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.

更多信息: http://bartwullems.blogspot.com /2011/02/caching-in-net-4.html

这篇关于是否可以在Application _Start中获得主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆