C# 在某些情况下,属性结束为null,但不包含静态变量
对不起,如果我没有解释清楚,但我似乎无法集中精力思考如何提出手头的问题 我们在web和windows窗体开发中使用了一些实用程序类。根据代码是在web中运行还是在windows窗体中运行,我们使用以下模式动态调用相应的方法 以下代码是精简版本,因此您可以更清晰地观察逻辑:C# 在某些情况下,属性结束为null,但不包含静态变量,c#,asp.net,properties,null,static-members,C#,Asp.net,Properties,Null,Static Members,对不起,如果我没有解释清楚,但我似乎无法集中精力思考如何提出手头的问题 我们在web和windows窗体开发中使用了一些实用程序类。根据代码是在web中运行还是在windows窗体中运行,我们使用以下模式动态调用相应的方法 以下代码是精简版本,因此您可以更清晰地观察逻辑: public static class Constants { private static ConstantsWinFormsWebCommonProvider _Provider; public stat
public static class Constants
{
private static ConstantsWinFormsWebCommonProvider _Provider;
public static ConstantsWinFormsWebCommonProvider Provider
{
get
{ /*in websites the static variables reset to null in case of Appdomain recycle
*so we check beforehand, this also serves for first time use in both: web and winforms */
//lazy loading, initializes when provider is required for the first time or if gets null because appdomain recycle...
if (_Provider != null) return _Provider;
_Provider = Fun.WebSite()? (ConstantsWinFormsWebCommonProvider)new ConstantsWebProvider() : new ConstantsWinFormsProvider();
Initialize();
return _Provider;
}
}
public static void Initialize()
{
Provider.Initialize();
}
public static string DataBaseName
{
get { return Provider.DataBaseName ; }
set { Provider.DataBaseName = value; }
}
}
public class ConstantsWinFormsWebCommonProvider
{
internal bool isInitialized { get; set; } //variable only used in the winform part for now, shouldnt affect the web part issue
internal string DataBaseName { get; set; }
}
public class ConstantsWebProvider: ConstantsWinFormsWebCommonProvider
{
public override void Initialize()
{
base.Initialize();
string errordetails= "";
if (!Fun.InitializeWeb(ref errordetails)) //fills all the Provider properties and connects, otherwise returns false
{
throw new Exception("Problem initializating web " + errordetails));
}
}
}
public class ConstantsWinFormsProvider: ConstantsWinFormsWebCommonProvider
{
public new string DataBaseName
{
get
{//lazy loading so it connects only when required
if (isInitialized) //(!string.IsNullOrEmpty(base.DataBaseName))
{
return base.DataBaseName ;
}
bool resul = Fun.InicializeWinForms();
if (resul) return base.DataBaseName ;
MessageBox.Show("Problem initializing");
return null;
}
set
{
Fun.WinFormsValueSavingSistemx(value); //example call, not related with the issue at hand
}
}
}
问题是,在web中偶尔会出现错误,因为Provider变量中的属性为null,但Provider本身不是null 理论上这是不可能的(对于web,我们在initialize()函数中初始化所有提供程序属性,如果appdomain回收导致属性变为null,则提供程序变量也应为null,从而在尝试访问其属性之一时再次调用initialize() 这个问题的原因和建议的解决方案是什么 编辑:
如果这提供了更多后见之明,那么现在的问题发生在一个aspx的第一个页面加载中(isPostback=false,它将在第一次访问该aspx时或者从另一个aspx再次访问Provider.DataBaseName)。此外,在记录在案的案例中,他们使用IE8和IE9…我以前在Web服务器上使用过一些静态方法,但我也遇到了同样的问题。我开了一张微软的票,他们说这是一个线程问题,他们的行为并不总是像WPF。可能需要研究线程/静态方法。@在我的例子中,发生这种情况的地方是在aspx的第一页加载中,代码中没有使用(显式)线程。是的,我实际上没有编写任何线程代码,我的问题是,处理多个客户机时是服务器。很抱歉,我可以提供更多的帮助。@Anon无论如何,谢谢你的提示。您是否从microsoft获得了一些用于检查问题的链接或到罚单的链接?