title: Static property always null in every request after set value on ASP.NET MVC5
body: <p>I have a <strong>static class</strong> and <strong>static property</strong></p>
  
  <pre><code> public static class Test
   {
       public static string Tests { get; set; }
   }
  </code></pre>
  
  <p>Now the problem is, I have a Action in a Controller</p>
  
  <pre><code>public ActionResult SomeActionInController(){
           ``              ``
       //  this place always execute in every request
       if (null == Test.Tests)
            Test.Tests = "some value";
           ``              ``
  }
  </code></pre>
  
  <p>But I will get <strong>null</strong> in every requests, not local debug, <strong>only on the server</strong>. </p>
  
  <p>I saw so many people said : <strong>Static property value will keeping on whole application domain</strong>, But why this is happening now ? Is there any way to fixed it? Thank you.</p>
  
  <p>My Server use <strong>IIS 8.5</strong> with <strong>Windows Server 2012 R2</strong></p>
  
  <p><strong>Update1</strong></p>
  
  <p>There is no static constructor in the static class.</p>
  
  <p>if I send request to the Action, the null will happen every time, because I can see it use <strong>log4net</strong>.</p>
  
  <p>I already disable Idle time out and free time out, <strong>all set to 0</strong>. So there is no recycle problem.</p>
  
  <p>This is my Global.asax:</p>
  
  <pre><code>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(() =&gt;
              {
                  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);
                  }
              }));
  
          }
  
  
          /// &lt;summary&gt;
          /// Setting Page Language
          /// &lt;/summary&gt;
          /// &lt;param name="sender"&gt;&lt;/param&gt;
          /// &lt;param name="e"&gt;&lt;/param&gt;
          private void Application_AcquireRequestState(object sender, EventArgs e)
          {
              if (HttpContext.Current != null &amp;&amp; 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");
                  }
              }
          }
  
  
          /// &lt;summary&gt;
          /// Cache setting
          /// &lt;/summary&gt;
          /// &lt;param name="context"&gt;&lt;/param&gt;
          /// &lt;param name="arg"&gt;&lt;/param&gt;
          /// &lt;returns&gt;&lt;/returns&gt;
          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);
          }
  
      }
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>I will set it every time, because I use this code, So don't worry about it.</p>
  
  <pre><code>       if (string.IsNullOrEmpty(Test.Tests))
                      {
                          log.Debug("Test: null, this time it will be setting");
                          Test.Tests = "Test";
                      }
                      else
                      {
                          log.Debug("Test: " + Test.Tests);
                      }
  </code></pre>
  
  <p>And on the server, The log4net will output it when every request(access the action) :</p>
  
  <blockquote>
    <p>Test: null, this time it will be setting</p>
    
    <p>Test: null, this time it will be setting</p>
    
    <p>Test: null, this time it will be setting</p>
  </blockquote>
  
  <p><strong>Update3</strong></p>
  
  <p>For some friends advice I put some code in the <strong>Application_Start()</strong></p>
  
  <pre><code>Test.Tests = "Test";
  </code></pre>
  
  <p>So now, every request it will successful get the value.
  But I change my code to this in Action:</p>
  
  <pre><code>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);
                          }
  </code></pre>
  
  <p>Now every request the log4net will output like this:</p>
  
  <blockquote>
    <p>Test: Test </p>
    
    <p>Now the Test new Value = Test3</p>
    
    <p>Test: Test </p>
    
    <p>Now the Test new Value = Test3</p>
    
    <p>Test: Test </p>
    
    <p>Now the Test new Value = Test3</p>
  </blockquote>
  
  <p><strong>But that's not what I want</strong>. I want the static property can be read and modify in whole application domain, not only 1 time .</p>