关于httpcontext存在时期
  在一次实际开发过程中,需要同步服务器上的用户,用户同步是通过XML文件来获取,当时还有一个配置文件也是xml文件放在本地,这个时候就需要使用到Server.MapPath功能或HttpContext.current.server.mappath功能,把相对路径转变成绝对路径,以用来对XML文件的读取、删除操作。本来这个功能非常简单,但是由于用户同步需要在规定间隔内自动同步,这个时候我用了timer时间触发器,在Global.asax.cs里代码如下:
 1 protected void Application_Start(Object sender, EventArgs e)
protected void Application_Start(Object sender, EventArgs e)
2 {
        {
3 
            
4 
            
5 Timer time = new Timer(1000 * 60 * 2);
            Timer time = new Timer(1000 * 60 * 2);
6 time.Elapsed +=new ElapsedEventHandler(time_Elapsed);
            time.Elapsed +=new ElapsedEventHandler(time_Elapsed);
7 time.AutoReset = true;
            time.AutoReset = true;
8 time.Enabled = true;
            time.Enabled = true;    
9
10 
    
11
12 }
        }
 protected void Application_Start(Object sender, EventArgs e)
protected void Application_Start(Object sender, EventArgs e)2
 {
        {3
 
            4
 
            5
 Timer time = new Timer(1000 * 60 * 2);
            Timer time = new Timer(1000 * 60 * 2);6
 time.Elapsed +=new ElapsedEventHandler(time_Elapsed);
            time.Elapsed +=new ElapsedEventHandler(time_Elapsed);7
 time.AutoReset = true;
            time.AutoReset = true;8
 time.Enabled = true;
            time.Enabled = true;    9

10
 
    11

12
 }
        }时间间隔处理事件如下:
1 private void time_Elapsed(object sender, ElapsedEventArgs e)
private void time_Elapsed(object sender, ElapsedEventArgs e)
2 {
        {
3 
            
4 web.User.SSOUtility su = new web.User.SSOUtility();
            web.User.SSOUtility su = new web.User.SSOUtility();
5 su.SynUser();
            su.SynUser();
6 
            
7 }
        }
 private void time_Elapsed(object sender, ElapsedEventArgs e)
private void time_Elapsed(object sender, ElapsedEventArgs e)2
 {
        {3
 
            4
 web.User.SSOUtility su = new web.User.SSOUtility();
            web.User.SSOUtility su = new web.User.SSOUtility();5
 su.SynUser();
            su.SynUser();6
 
            7
 }
        }而此时在SSOUtility.cs文件里设置读取配制文件和下载到本地的同步用户文件
1 private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");
private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");
2 private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");
        private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");
 private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");
private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");2
 private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");
        private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");通过一个Button来触发SynUser()事件,一切运行正常,但到通过Timer来控制时,发现数据不能同步,通过调试发现,原来程序一运行到
1 private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");
private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");
2 private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");
        private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");
就出错,查了半天才发现原来是httpcontext这个上下文HTTP请求信息在时间间隔触发时,根本就不起作用,也就是说不能获取上下文HTTP请求对象,后来把这二句读取文件和语句放到Global.asax.cs上来,并修改SSOUtility  构造函数用来实例化对象时,传入二个文件的绝对地址。如下: private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");
private static string UserXmlFile = HttpContext.Current.Server.MapPath("SBWLogin/user.xml");2
 private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");
        private static string SSOConfigFile = HttpContext.Current.Server.MapPath("SBWLogin/SSOConfig.xml");1 private void time_Elapsed(object sender, ElapsedEventArgs e)
private void time_Elapsed(object sender, ElapsedEventArgs e)
2 {
        {
3 
            
4 web.User.SSOUtility su = new web.User.SSOUtility(UserXmlFile, SSOConfigFile);
            web.User.SSOUtility su = new web.User.SSOUtility(UserXmlFile, SSOConfigFile);
5 su.SynUser();
            su.SynUser();
6 
            
7 }
        }
 private void time_Elapsed(object sender, ElapsedEventArgs e)
private void time_Elapsed(object sender, ElapsedEventArgs e)2
 {
        {3
 
            4
 web.User.SSOUtility su = new web.User.SSOUtility(UserXmlFile, SSOConfigFile);
            web.User.SSOUtility su = new web.User.SSOUtility(UserXmlFile, SSOConfigFile);5
 su.SynUser();
            su.SynUser();6
 
            7
 }
        }这时再运行程序,一切OK。
以前总以为只要我在vs.net 2003里能出现httpcontext.current这些提示,并且编译通过就应该没有问题了,经过这次的经历,才发现httpcontext通过页面触发没有问题,直接从Global.asax.cs调用也行,但只能执行一次,但从Global.asax.cs的timer时间间隔事件中触发时,其实例化对象获取失败。
终于写完了,这可是我的处女作啊,本人经验有限,可能会出现一些不当之处,敬请谅解,还希望高手能够指正我的错误,多给点意见,也能让自己多学点东西
 
                    
                 
        
 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号