ASP.NET 应用程序生命周期 - 如何检查配置属性是否存在?

发布于 2024-10-19 01:14:24 字数 297 浏览 5 评论 0原文

我编写了一个单例类,它以一种很好的获取属性的方式公开 web.config 属性。

我想要一个 Load 方法来解析配置中的数据并设置公共属性,并且当配置键丢失或无法解析时我想抛出异常(以便它们记录在 EventLog 中)。

我尝试将 Load() 代码放置在 global.asax 的 Application_Start 中,但随后我想起这只会运行一次,或者直到应用程序重新启动为止。

放置用户“每次”启动/运行站点时所需运行的代码的最佳位置在哪里?如果无法加载某些配置属性,我基本上希望网站停止运行。

谢谢。

I've written a singleton class that exposes the web.config properties in a nice get property kind of way.

I want a Load method to parse the data in the config and set the public properties, and I want to throw exceptions (so they are logged in the EventLog) when a configuration key is missing or can't be parsed.

I tried placing the Load() code in Application_Start of the global.asax but then remembered this will only be run once, or until the application restarts.

Where is the best place to put code that you need to run 'everytime' your site is started/run by the user? I basically want the website to stop functioning if certain config properties cannot be loaded.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-10-26 01:14:24

当您更改 web.config 文件时,应用程序池已回收。这意味着下一次点击将导致您的 Application_Start 方法被调用。

更改以下文件也会
触发立即重启
应用程序池:

<前><代码>- web.config
- 机器配置
- 全局.asax
- bin 目录或其子目录中的任何内容

在此基础上,一旦您的配置发生更改,它将在用户下次访问站点时重新加载,这应该以最少的配置重新加载次数解决问题,而不是每次会话启动时都重新加载例子。因此,您可以这样做(在您的 global.asax 中):

static bool configValid = false;
void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext context = base.Context;
    HttpResponse response = context.Response;
    HttpRequest request = context.Request;

    // Redirect users to an alternate page if the current config is invalid
    // I happen to pass the Url they were attempting to access in the query string
    // that way you can give them a "try again" link
    if ((!configValid) && (!request.Url.ToString().Contains("BadConfig.aspx")))
    {
        response.Redirect("BadConfig.aspx?originalUrl=" + context.Server.UrlEncode(request.Url.ToString()));
    }
}

void Application_Start(object sender, EventArgs e) 
{
    // Load config and determine if it's valid, thus setting configValid to true/false
    //
    //
    configValid = false;

}

When you change your web.config file, the application pool is recycled. This means that the next hit will cause your Application_Start method to be called.

Altering the following files will also
trigger an immediate restart of the
application pool:

- web.config
- machine.config
- global.asax
- Anything in the bin directory or it's sub-directories

On that basis, as soon as your configuration is changed, it will be reloaded the next time a user hits the site, which should resolve the problem with the minimum number of configuration reloads, as opposed to reloading whenever a session starts for example. Therefore, you can do this (in your global.asax):

static bool configValid = false;
void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext context = base.Context;
    HttpResponse response = context.Response;
    HttpRequest request = context.Request;

    // Redirect users to an alternate page if the current config is invalid
    // I happen to pass the Url they were attempting to access in the query string
    // that way you can give them a "try again" link
    if ((!configValid) && (!request.Url.ToString().Contains("BadConfig.aspx")))
    {
        response.Redirect("BadConfig.aspx?originalUrl=" + context.Server.UrlEncode(request.Url.ToString()));
    }
}

void Application_Start(object sender, EventArgs e) 
{
    // Load config and determine if it's valid, thus setting configValid to true/false
    //
    //
    configValid = false;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文