在ASP.NET MVC5上设置值之后,静态属性在每个请求中始终为null

我有一个静态类静态属性

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

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

 public ActionResult SomeActionInController(){ `` `` // this place always execute in every request if (null == Test.Tests) Test.Tests = "some value"; `` `` } 

但是我会在每个请求中得到null ,而不是本地调试, 只在服务器上

我看到很多人说: 静态属性值将保留在整个应用领域 ,但为什么现在这样呢? 有没有办法修复它? 谢谢。

我的服务器使用IIS 8.5Windows 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); } })); } ///  /// Setting Page Language ///  ///  ///  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"); } } } ///  /// Cache setting ///  ///  ///  ///  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会在每次请求时输出它(访问操作):

测试:null,这次是设置

测试:null,这次是设置

测试:null,这次是设置

UPDATE3

对于一些朋友的建议,我在Application_Start()中添加了一些代码

 Test.Tests = "Test"; 

所以现在,每个请求它都会成功获得价值。 但我在Action中将我的代码更改为:

 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

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

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

非常感谢这两个朋友。

如果您遇到同样的问题,请不要将每个文件放入或创建任何文件到“Bin”文件夹,或尝试编写它。 除非您希望应用程序被破坏,否则您可以这样做:-)

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

静力学并不神奇。 如果你没有设置它们,它们就没有价值。

在你的控制器动作中,你正在检查它是否已经设置(提示:如果你没有设置它,它没有设置):

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

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

 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方法(或者每个Application实例化一次)。

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