2015-02-09 61 views
2

设定值后,每一个请求空我有一个静态类静态属性静态属性总是在ASP.NET MVC5

public static class Test 
{ 
    public static string Tests { get; set; } 
} 

现在的问题是,我有一个控制器

一个行动
public ActionResult SomeActionInController(){ 
     ``    `` 
    // this place always execute in every request 
    if (null == Test.Tests) 
      Test.Tests = "some value"; 
     ``    `` 
} 
只在服务器上但

我会得到在每一个请求,而不是本地调试,

我看到很多人说:静态属性值会保留在整个应用域,但为什么现在这是发生?有什么方法可以修复它吗?谢谢。

我的服务器使用IIS 8.5的Windows Server 2012 R2

UPDATE1

有静态类中没有静态构造函数。

如果我向Action发送请求,每次都会发生null,因为我可以看到它使用log4net

我已经禁用空闲超时和空闲超时,全部设置为0。所以没有回收问题。

这是我的Global.asax:

public class MvcApplication : System.Web.HttpApplication 
    { 
     private readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(MvcApplication)); 

     protected void Application_Start() 
     { 
      Elmah.Mvc.Bootstrap.Initialize(); 
      AreaRegistration.RegisterAllAreas(); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 


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


      // Only keep Razor 
      ViewEngines.Engines.Clear(); 
      ViewEngines.Engines.Add(new RazorViewEngine()); 



      System.Threading.Tasks.Task.Factory.StartNew(new Action(() => 
      { 
       try 
       { 
        System.Threading.Thread.Sleep(1000 * 100 * 1); 

        // Send a fake request for warm up the website, when after it recycle 
        WebClient webClient = new WebClient(); 
        using (Stream stream = webClient.OpenRead(ConfigurationManager.AppSettings["InitialPath"])) 
        { 
         if (stream.CanRead) 
         { 
          log.Debug("Success warm up when recycle"); 
         } 
         else 
         { 
          log.Debug("warm up failed"); 
         } 
        } 



       } 
       catch (WebException ex) 
       { 
        log.Debug(ex); 
       } 
       catch (Exception ex) 
       { 
        log.Error(ex); 
       } 
      })); 

     } 


     /// <summary> 
     /// Setting Page Language 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void Application_AcquireRequestState(object sender, EventArgs e) 
     { 
      if (HttpContext.Current != null && HttpContext.Current.Session != null) 
      { 
       if (AdminLanguage == 1) 
       { 
        Thread.CurrentThread.CurrentCulture = new CultureInfo("xx-xx"); 
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("xx-xx"); 
       } 
       else 
       { 
        Thread.CurrentThread.CurrentCulture = new CultureInfo("xx-xx"); 
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("xx-xx"); 
       } 
      } 
     } 


     /// <summary> 
     /// Cache setting 
     /// </summary> 
     /// <param name="context"></param> 
     /// <param name="arg"></param> 
     /// <returns></returns> 
     public override string GetVaryByCustomString(HttpContext context, string arg) 
     { 
      // cache from DevTrends.MvcDonutCaching 
      if (arg == "NavStatic") 
      { 
       return "NavStatic=" + HttpContext.Current.Session.SessionID; 
      } 

      return base.GetVaryByCustomString(context, arg); 
     } 

    } 

更新2

我每次都会设置它,因为我用这个代码,所以不要担心。

 if (string.IsNullOrEmpty(Test.Tests)) 
        { 
         log.Debug("Test: null, this time it will be setting"); 
         Test.Tests = "Test"; 
        } 
        else 
        { 
         log.Debug("Test: " + Test.Tests); 
        } 

和服务器上,log4net的将输出它时,每一个请求(进入动作):

测试:空,这时候就会被设置

测试:空,这个时候就会被设置

测试:空,这时候就会被设置

UPDATE3

对于一些朋友建议我把一些代码在的Application_Start()

Test.Tests = "Test"; 

所以,现在,每个请求将成功获得的价值。 但我改变我的代码,这个动作:

if (string.IsNullOrEmpty(Test.Tests)) 
         { 
          log.Debug("Test: null, this time it will be setting"); 
          Test.Tests = "Test"; 
         } 
         else 
         { 
          log.Debug("Test: " + Test.Tests); 
          Test.Tests = "Test3"; 
          log.Debug("Now the Test new Value = " + Test.Tests); 
         } 

现在每个请求log4net的会喜欢这样的输出:

测试:测试

现在测试新值= Test3的

测试:测试

现在测试新值= Test3

测试:测试

现在测试新值= Test3的

但是,这不是我想要的。我希望静态属性可以在整个应用程序域中读取和修改,不仅是1次。

+2

什么是静态构造函数的样子?每次都会发生这种“空”吗?还是在代码运行之后,App Pool超时?你的Global ASAX看起来像静态构造函数是什么样的?你在哪里设置静态属性?在请求?你是否也在全球ASAX中设置它? – 2015-02-09 13:34:11

+0

@GeorgeStocker,我更新了我的问题。 – qakmak 2015-02-09 13:45:58

+1

@GeorgeStocker,请不要搁置它。我已经更新了我的问题。 – qakmak 2015-02-09 14:04:53

回答

1

George StockerDave Becker讨论后,并保持调试。最后找到问题的根源是:仅仅因为我在创建了log4net日志文件进入网站“Bin”文件夹。然后当每个请求进来时,log4net写入日志,并且IIS检测到有一些文件被更改,然后执行Application_End()。全没了。

非常感谢这2个朋友。

如果你有同样的问题,不要每个放置或创建任何文件“Bin”文件夹,或试图写它。除非你想要应用程序销毁你可以这样做:-)

0

这听起来像你想在ASP.NET MVC中的全局变量。幸运的是,你的问题已经解决了with this Stack Overflow question

静力学不是神奇的。如果你不设置它们,它们将没有价值。

在你的控制器动作,你检查,看看是否它的设置,但(提示:如果您没有设置它已经,这不是设置):

if (null == Test.Tests) 
{ 
    Test.Tests = "some value"; 
} 

所以,你在哪里设置Test.Tests?如果您希望为每个控制器操作设置它,则需要确保在控制器操作运行时设置它。一个伟大的地方是在你的Application_Start方法您的global.asax.cs的:

public class MvcApplication : System.Web.HttpApplication 
{ 

    protected void Application_Start() 
    { 
     Tests.Test = "This is set"; 
    } 

} 
public static class Tests 
{ 
    public static string Test { get; set; } 
} 

控制器被实例化在每次请求;每个App Pool Recycle调用一次Application_Start方法(或每个应用程序实例化一次)。

您的Tests.Test为空的原因是您从未设置其值。

+0

我会在每个请求上设置它。因为我已经告诉过你我有log4net。我会更新我的问题。 – qakmak 2015-02-09 13:55:11

+0

@qakmak Log4Net与你的静态类有什么关系?如果您在Global中设置了该值,那么当您的请求加快时,它就会在那里。我觉得你要抛弃一些重要的细节,这将使所有这些都变得有意义。 – 2015-02-09 13:59:20

+0

log4net用于显示调试信息。相信我如果我知道这一点,我不会浪费我的这2天...... – qakmak 2015-02-09 14:02:42