设定值后,每一个请求空我有一个静态类和静态属性静态属性总是在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次。
什么是静态构造函数的样子?每次都会发生这种“空”吗?还是在代码运行之后,App Pool超时?你的Global ASAX看起来像静态构造函数是什么样的?你在哪里设置静态属性?在请求?你是否也在全球ASAX中设置它? – 2015-02-09 13:34:11
@GeorgeStocker,我更新了我的问题。 – qakmak 2015-02-09 13:45:58
@GeorgeStocker,请不要搁置它。我已经更新了我的问题。 – qakmak 2015-02-09 14:04:53