简体   繁体   English

我应该使用会议吗?

[英]Should I use sessions?

I am designing an online time tracking software to be used internally. 我正在设计一个供内部使用的在线时间跟踪软件。 I am fairly new to c# and .NET though I have extensive PHP experience. 尽管我有丰富的PHP经验,但我对c#和.NET还是比较陌生。

I am using Windows Forms Authentication, and once the user logs in using that, I create a Timesheet object (my own custom class). 我正在使用Windows窗体身份验证,一旦用户使用Windows身份验证登录,便会创建一个时间表对象(我自己的自定义类)。

As part of this class, I have a constructor that checks the SQL DB for information (recent entries by this user, user preferences, etc.) 作为此类的一部分,我有一个构造函数,该构造函数检查SQL DB的信息(该用户的最新条目,用户首选项等)。

Should I be storing this information in a session? 我应该在会话中存储此信息吗? And then checking the session object in the constructor first? 然后首先在构造函数中检查会话对象? That seems the obvious approach, but most examples I've looked at don't make much use of sessions. 这似乎是显而易见的方法,但是我看过的大多数示例都没有太多使用会话。 Is there something I don't know that others do (specifically related to .NET sessions of course)? 有我不知道其他人会做的事情(当然与.NET会话有关)吗?

EDIT: I forgot to mention two things. 编辑:我忘了提到两件事。
1. My SQL DB is on another server (though I believe they are both on the same network, so not much of an issue) 1.我的SQL DB在另一台服务器上(尽管我相信它们都在同一网络上,所以没有太大的问题)
2. There are certain constants that the user will not be able to change (only the admin can modify them) such as project tasks. 2.有些常量用户将无法更改(只有管理员可以修改它们),例如项目任务。 These are used on every page, but loaded the first time from the DB. 这些用于每个页面,但是是第一次从数据库加载。 Should I be storing these in a session? 我应该将它们存储在会话中吗? If not, where else? 如果没有,还有什么地方?
The only other way I can think of is a local flat file that updates each time the table of projects is updated, but that seems like a hack solution. 我能想到的唯一另一种方法是本地平面文件,该文件在项目表每次更新时都会更新,但这似乎是一个hack解决方案。 Am I trying too hard to minimize calls to the DB? 我是否在尽力减少对数据库的调用?

There is a good overview on ASP.NET Session here: ASP.NET Session State . 此处对ASP.NET会话有一个很好的概述: ASP.NET会话状态

If you don't have thousands of clients, but need "some state" stored server-side, this is very easy to use and works well. 如果您没有成千上万的客户端,但需要在服务器端存储“某种状态”,则此方法非常易于使用且效果很好。 It can also be stored in the database in multi server scenarios, without changing a line in your code, just by configuration. 它也可以在多服务器方案中存储在数据库中,而无需通过配置更改代码中的任何一行。

My advise would be not to store "big", or full object hierarchies in there, as storing in a session (if the session is shared among servers in a web farm in a database for example) can be somewhat costy. 我的建议是不要在其中存储“大”或完整的对象层次结构,因为在会话中进行存储(例如,如果会话在数据库中的Web场中的服务器之间共享)可能会比较昂贵。 If you plan to have only one server, this is not really a problem, but you have to know that you won't be able to easily move to a multiple server mode easily. 如果您计划仅拥有一台服务器,这并不是真正的问题,但是您必须知道,您将无法轻松地迁移到多服务器模式。

The worst thing to do is follow the guys who just say "session is bad, whooooo!", don't use it, and eventually rewrite your own system. 最糟糕的事情是跟随那些只说“会话不好,糟糕!”的家伙,不要使用它,最终重写自己的系统。 If you need it, use it :-) 如果需要,请使用它:-)

I would shy away from session objects. 我会回避会话对象。 And actually I would say look into .net MVC as well. 实际上,我也想说一下.net MVC。

The reason I don't use the session is because I feel it can be a crutch for some developers. 我不使用会话的原因是因为我认为它可能对某些开发人员来说是一个拐杖。

I would save all of the information that you would have put into a session into a db. 我会将您将放入会话的所有信息保存到数据库中。 This will allow for better metrics tracking, support for Azure (off topic but worth mentioning) and is cleaner imo. 这将允许更好的指标跟踪,对Azure的支持(不在主题范围内,但值得一提),并且更干净。

ASP developers know session state as a great feature, but one that is somewhat limited. ASP开发人员知道会话状态是一项很棒的功能,但是这一功能有些局限。 These limitations include: 这些限制包括:

ASP session state exists in the process that hosts ASP; ASP会话状态存在于承载ASP的过程中; thus the actions that affect the process also affect session state. 因此,影响流程的操作也会影响会话状态。 When the process is recycled or fails, session state is lost. 当进程被回收或失败时,会话状态将丢失。 Server farm limitations. 服务器场限制。 As users move from server to server in a Web server farm, their session state does not follow them. 当用户在Web服务器场中的服务器之间移动时,他们的会话状态不会跟随他们。 ASP session state is machine specific. ASP会话状态是特定于计算机的。 Each ASP server provides its own session state, and unless the user returns to the same server, the session state is inaccessible. 每个ASP服务器都提供自己的会话状态,除非用户返回到同一台服务器,否则无法访问该会话状态。
http://msdn.microsoft.com/en-us/library/ms972429.aspx http://msdn.microsoft.com/en-us/library/ms972429.aspx

Look into the HttpContext.User (IPrincipal) property. 查看HttpContext.User (IPrincipal)属性。 this is where user information is stored in the request. 这是用户信息存储在请求中的位置。

One of the main problems with Session is, that by default, it is stored in memory. Session的主要问题之一是默认情况下将其存储在内存中。 If you have many concurrent users that store data in the session this could easily lead to performance problems. 如果您有许多并发用户在会话中存储数据,这很容易导致性能问题。

Another thing is that application recycle will empty your in memory session which could lead to errors. 另一件事是,应用程序回收将清空您的内存会话,这可能会导致错误。

Off course you can move your session to SqlServer or a StateServer but then you will lose on performance. 当然,您可以将会话移至SqlServer或StateServer,但那样会降低性能。

Most people avoid session state simply because people like to avoid state in general. 大多数人之所以避免会话状态,仅仅是因为人们通常喜欢避免状态。 If you can find an algorithm or process which works all the time regardless of the previous state of an object, that process tends to be more fool proof against future maintenance and more easily testable. 如果您找到了一个始终可以工作的算法或过程,而不管对象的先前状态如何,那么该过程将更容易证明将来是否需要维护,并且更易于测试。

I would say for this particular case, store your values in the database and read them from there any time you need that information. 我想说,对于这种特殊情况,将您的值存储在数据库中,并在需要该信息时从那里读取它们。 Once you have that working, take a look at the performance of the site. 工作完成后,请查看网站的性能。 If it's performing fine then leave it alone (as this is the simplest case to program). 如果它运行良好,则不要理会它(因为这是最简单的编程方法)。 If performance is an issue, look at using the IIS Cache (instead of session) or implementing a system like CQRS. 如果性能是一个问题,请考虑使用IIS缓存(而不是会话)或实现类似CQRS的系统。

Session State Disadvantage 会话状态劣势

Session-state variables stay in memory until they are either removed or replaced, and therefore can degrade server performance. 会话状态变量会保留在内存中,直到被删除或替换,因此可能降低服务器性能。 Session-state variables that contain blocks of information, such as large datasets, can adversely affect Web-server performance as server load increases. 包含信息块(例如大型数据集)的会话状态变量会随着服务器负载的增加而对Web服务器性能产生不利影响。 Think what will happen if you significant amount of users simultaneously online. 想想如果您同时在线有大量用户,将会发生什么情况。

NOTE :- I haven't mentioned the advantages because they are straightforward which are : Simple implementation, Session-specific events, Data persistence, Cookieless support etc. 注意:-我没有提到这些优点,因为它们很简单,即:简单的实现,特定于会话的事件,数据持久性,无Cookie支持等。

The core problem with sessions are scaleability. 会话的核心问题是可伸缩性。 If you have a small application, with a small number of users, that will only ever be on one server, then it may be a good route for you to save small amounts of data - maybe just the user id - to allow quick access to the preferences etc. 如果您的应用程序很小,用户数量很少,只能在一个服务器上使用,那么这可能是保存少量数据(也许只是用户ID)以允许快速访问的好方法。偏好等

If you MAY want multiple web servers, or the application MAY grow, then don't use session. 如果您可能需要多个Web服务器,或者应用程序可能会增长,则不要使用会话。 And only use it for small pieces of information. 并且仅将其用于少量信息。

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

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