简体   繁体   English

c#+ WebForms + static - 最佳实践是什么?

[英]c# + WebForms + static — what are the best practices?

I'm definitely not a fan of WebForms, I prefer in the .NET world ASP.NET MVC. 我绝对不是WebForms的粉丝,我更喜欢.NET世界的ASP.NET MVC。

Regardless, I'm working on a small part of a very large legacy WebForms application. 无论如何,我正在研究一个非常大的遗留WebForms应用程序的一小部分。

I'm integrating Korzh.com's EasyQuery.NET. 我正在整合Korzh.com的EasyQuery.NET。 to allow end users to create their own SQL queries based on pre-defined models made user friendly with aliases. 允许最终用户根据预定义的模型创建自己的SQL查询,使用户友好的别名。

This is relevant because Korzh's demo uses Global.asax for its model and 这是相关的,因为Korzh的演示使用Global.asax作为其模型和
query class along with Session. 查询类和Session。

Because the legacy WebForms application is very large, Global.asax in not used 因为遗留的WebForms应用程序非常大,所以没有使用Global.asax
for page specific items. 用于页面特定项目。

My solution was to use private static instead. 我的解决方案是使用私有静态。 static works well in desktop 静态在桌面上运行良好
applications but seems at the very least likely to cause some grief in WebForms applications. 应用程序,但似乎至少可能导致WebForms应用程序的一些悲伤。

I've discovered that !IsPostBack is not too reliable and it seems to me that 我发现了!IsPostBack不太可靠,在我看来
in WebForms the best practice may be to use Session. 在WebForms中,最佳实践可能是使用Session。 The problem with 这个问题
Session is that it seems to be passed to the client with the HTML and can grow 会话是它似乎通过HTML传递给客户端并且可以增长
very large in kilobytes. 非常大的千字节。

QUESTIONS: 问题:

Since static variables reside on the IIS server when used with WebForms, does every user of a WebForms application share the same static variable address space? 由于静态变量与WebForms一起使用时驻留在IIS服务器上,因此WebForms应用程序的每个用户是否共享相同的静态变量地址空间? (I think the answer is yes). (我认为答案是肯定的)。

What are the best practices/guidelines for using/not using static variables with ASP.NET WebForms applications? 在ASP.NET WebForms应用程序中使用/不使用静态变量的最佳实践/指南是什么?

Thank you. 谢谢。
Regards, 问候,
Gerry (Lowry) 格里(洛瑞)

PS: I could not find answers PS:我找不到答案
via Google or searching SO. 通过Google或搜索SO。

In ASP.NET, static instances will live for the lifetime of the application, that being the web application itself, until it is recycled, or shutdown, eg: 在ASP.NET中,静态实例将在应用程序的生命周期中存在,即Web应用程序本身,直到它被回收或关闭,例如:

public class Global : HttpApplication {
    public static string MyString
}

Because of this, the static property is accessible for all requests made to the application. 因此,对应用程序的所有请求都可以访问静态属性。 Not the place to be storing page-specific items. 不是存储页面特定项目的地方。 There are quite a few storage mechanisms available: 有很多可用的存储机制:

  1. HttpRuntime.Cache and HttpContext.Cache, both point to the same cache instance, and items exist for the lifetime of the application (so has the same issues as static instances). HttpRuntime.Cache和HttpContext.Cache都指向相同的缓存实例,并且在应用程序的生命周期中存在项目(因此与静态实例具有相同的问题)。

  2. HttpContext.Items, a request specific collection of items. HttpContext.Items,特定于请求的项目集合。 Each request made to the application will have its own collection of items. 对应用程序发出的每个请求都有自己的项目集合。

  3. HttpSessionState session, persisted for the length of the user visit, or whenever it times out. HttpSessionState会话,持续用户访问的长度,或者每当超时时。 This can be configured 4 ways: 这可以通过4种方式配置:

    3.a. 3.A. InProc, session objects are stored in memory by the worker process itself. 在InProc中,会话对象由工作进程本身存储在内存中。 Fast accessing cache, doesn't require serialisation, but if the application recycles, the session data is lost. 快速访问缓存,不需要序列化,但如果应用程序循环使用,会话数据将丢失。

    3.b. 3.B. SqlServer, session objects are serialised and stored in a Sql Server database. SqlServer,会话对象被序列化并存储在Sql Server数据库中。 Requires all session-stored items to be serialisable. 要求所有会话存储的项目都是可序列化的。 Session objects persist even when an application recycles. 即使应用程序循环使用,会话对象仍然存在。

    3.c. 3.C. StateServer, session objects are stored in a seperate process, and persists data through application recycles. StateServer,会话对象存储在单独的进程中,并通过应用程序循环来保存数据。

    3.d. 3.D. Custom session provider, that's up to you.... 自定义会话提供商,这取决于你....

  4. ViewState, this is where data is persisted to the client-side and is posted back to the server to rebuild control states between page views. ViewState,这是将数据持久保存到客户端并发回服务器以重建页面视图之间的控制状态的位置。

I would avoid using static instances and the HttpRuntime cache for anything user related. 我会避免使用静态实例和HttpRuntime缓存来处理与用户相关的任何事情。 Use these mechanisms for shared, common information, such as configuration, caching, etc. Session is likely the place you want to store things on a per-user basis. 将这些机制用于共享的公共信息,例如配置,缓存等。会话可能是您希望按用户存储内容的地方。 If you are looking for a per-page solution, its a lot simpler, because you simply make the variables part of the page structure itself, as properties or fields. 如果您正在寻找每页解决方案,那么它更简单,因为您只需将变量本身作为属性或字段的一部分。 You just have to manage the initialisation of these fields. 您只需管理这些字段的初始化。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM