ASP.NET 业务层代码仍然存在,但我不确定它存储在哪里?
我现在对 ASP.NET 感到非常困惑。我相信,每次您检索该页面引用的网页业务模型时,都会创建该页面,并且在将显示发送给用户后,它都会再次被拆除。
一位同事向我展示了这个简单的 Web 应用程序,它似乎在页面获取之间存储业务层代码,而不使用会话、缓存、应用程序、视图状态、查询字符串或帖子(据我所知)。
代码如下:
<form id="form1" runat="server" enableviewstate="False">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" EnableViewState="False"></asp:Label>
<asp:Button ID="Button1"
runat="server" Text="Button" EnableViewState="False"
CausesValidation="False" />
</div>
</form>
页面加载:
PersistanceTest.BLL.Singleton s = PersistanceTest.BLL.Singleton.GetInstance;
Label1.Text = "Counter: " + s.GetCounter;
和 Singleton 类:
public class Singleton
{
private int counter;
private Singleton()
{
counter = 0;
}
private static Singleton theSingleton;
static public Singleton GetInstance
{
get
{
if(theSingleton == null)
{
theSingleton = new Singleton();
}
return theSingleton;
}
}
public int GetCounter
{
get
{
int c = counter;
counter++;
return c;
}
}
}
现在,当我加载页面时,每次按下按钮时计数器都会递增。但我不知道 Singleton 实例在框架中保存在哪里?我确信它在页面加载之间不会存在。有人可以解释一下吗?这让我发疯。
I'm royally confused about ASP.NET now. I believed that everytime you retrieved a webpage business model referred to by that page would be created and that after the display had been sent to the user it would all be torn back down again.
A colleague at work showed me this simple web app which seems to be storing business layer code between page fetches, without using session, cache, application, viewstate, querystrings, or post (as far as I can tell).
The code is as follows:
<form id="form1" runat="server" enableviewstate="False">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" EnableViewState="False"></asp:Label>
<asp:Button ID="Button1"
runat="server" Text="Button" EnableViewState="False"
CausesValidation="False" />
</div>
</form>
Page Load:
PersistanceTest.BLL.Singleton s = PersistanceTest.BLL.Singleton.GetInstance;
Label1.Text = "Counter: " + s.GetCounter;
And Singleton class:
public class Singleton
{
private int counter;
private Singleton()
{
counter = 0;
}
private static Singleton theSingleton;
static public Singleton GetInstance
{
get
{
if(theSingleton == null)
{
theSingleton = new Singleton();
}
return theSingleton;
}
}
public int GetCounter
{
get
{
int c = counter;
counter++;
return c;
}
}
}
Now when I load the page, the counter increments each time I press the button. But I have no idea where the Singleton instance is being persisted in the framework? I was sure it wouldn't exist between page loads. Can someone shed some light on this? It's driving me crazy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信像这样的单例存储在应用程序域(AppDomain)中。当您的应用程序重新启动时,所有存储的信息也将被重置。
您可以在此处阅读有关 AppDomains 的更多信息:http://odetocode.com/articles/305.aspx
I believe Singletons like that one are stored in the Application Domain (AppDomain). When your application restarts, all stored information will be reset as well.
You can read more about AppDomains here: http://odetocode.com/articles/305.aspx