HttpContext.Current 在IIS 7 Application_Start 无效

9 篇文章 0 订阅
9 篇文章 0 订阅

核心提示:

IIS 7 的集成模式在 设计上发生了变化,Application_Start 事件中,HttpContext.Current 是未被初始化的。

(原文链接 http://ddbiz.com/?p=139)

 

如下的.asax 代码在IIS 6 中运行正常,但在 IIS 7的集成模式下,是无效的[加粗部分]

 

 

    void Application_Start(object sender, EventArgs e)

    {

        //在应用程序启动时运行的代码

        log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("~/bin/log4net.config")));

        ILog log = LogManager.GetLogger("web");

        log.Info("Application Started");

 

        string hostname = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];

        Int32 checkInterval = new ServiceFactory().GetSiteService().Setting4EmailCheckInterval(hostname);

        emailTimer = new Timer(new TimerCallback(ScheduleEmailSender),

            hostname,

            2 * 60 * 1000,  //系统启动可能需要1分钟时间,初始化很慢,此处使用2分钟设定

            checkInterval * 1000);

         */ 

    }

 

 

相似的,Application_Start 在IIS 7的集成模式下, Context, Application, Session 都是未初始化的。因此要实现原来的功能,只有两种方式:

1. IIS 7 运行在兼容的经典模式下

2. 原来的初始化放在 Application_BeginRequest 中。如上面的代码,修改成如下:

 

   void Application_BeginRequest()

   {

        FirstRequestInitialization.Initialize(Context);

        ......

    } 

 

 

    internal class FirstRequestInitialization

    {

        private static bool s_InitializedAlready = false;

        private static Object s_lock = new Object();

 

        private static Timer scheduleTimer = null;

 

        // Initialize only on the first request

        public static void Initialize(HttpContext context)

        {

            if (s_InitializedAlready)

            {

                return;

            }

            lock (s_lock)

            {

                if (s_InitializedAlready)

                {

                    return;

                }

 

                string hostname = context.Request.ServerVariables["SERVER_NAME"];

                Int32 checkInterval = new ServiceFactory().GetSiteService().Setting4EmailCheckInterval(hostname);

                emailLoger.InfoFormat("{0}, Starting schedule email sender checker, interval={1}", DateTime.Now, checkInterval);

                scheduleTimer = new Timer(new TimerCallback(ScheduleEmailSender),

                    hostname,

                    2 * 60 * 1000,  //系统启动可能需要1分钟时间,初始化很慢,此处使用2分钟设定

                    checkInterval * 1000);

 

 

                // Perform first-request initialization here ...

                s_InitializedAlready = true;

            }

        }

(原文链接 http://ddbiz.com/?p=139)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值