在哪里为ASP.NET Web应用程序实现“启动时"代码? [英] Where do I implement 'on startup' code for an ASP.NET web application?

查看:49
本文介绍了在哪里为ASP.NET Web应用程序实现“启动时"代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为Web应用程序运行一个称为启动时"的小功能,以检查Web服务器的系统时间是否与DB服务器相同,如果不是,则记录错误. Web服务器应该在时间服务器上同步,但是我想检测是否不会发生.

Application_Start是正确的位置吗?如果不是,我应该在哪里做?

I need to run a small function that gets called ''on startup'' for a web application, to check if the web server''s system time is the same as the DB server, and log an error if not. The web server is supposed to sync on a time server, but I want to detect if this doesn''t occur.

Is Application_Start the right place for this? If not, where should I do it?

推荐答案

是的,Application_Start(位于Global.asax中)是放置它的地方.但是请注意,当在指定的时间内没有请求时,Web服务器将关闭应用程序.
在下一个请求时,应用程序将再次重新启动(& Application_Start调用).
因此,如果经常调用Application_Start中的代码,不要感到惊讶.
Yes, Application_Start (in Global.asax) is the place for it. But note that the web server shuts down the application when there are no requests for a specified period of time.
The application will be restarted (& Application_Start called) again on the next request.
So don''t be surprised if your code in Application_Start is called often.


我认为IIS 7中的默认超时为20分钟.
您可以通过以下方法来更改它:转到连接"窗格->应用程序池->高级设置(用于托管您的应用程序的池)->空闲超时

您甚至可以将其禁用&它永远不会关机.

但这只是为了解决您的问题而插入它们是一个坏主意.

我建议您将先前的运行时间保存在App_Data&中的一个简单文件中.检查它是否至少经过24小时(在Application_Start上)

-编辑-
PS:您也无法在许多共享的托管服务提供商上访问这些东西:)
I think the default timeout is 20 minutes in IIS 7
You can alter it by going to the Connections pane ->Application Pools ->Advanced settings (for the pool which hosts your app)-> Idle Timeout

You can even disable it & it''ll never be shutdown.

But it''s a bad idea for you to meddle them just for the problem you have.

I suggest you keep the previous run time in a simple file in App_Data & check it to see if at least 24 hrs is passed (on Application_Start)

--Edited--
PS: You also don''t get to access these things on many shared hosting providers :)


这篇关于在哪里为ASP.NET Web应用程序实现“启动时"代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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