ASP.Net Session

前端 未结 13 1289
清酒与你
清酒与你 2020-12-08 07:54

I am wanting to store the \"state\" of some actions the user is performing in a series of different ASP.Net webforms. What are my choices for persisting state, and what are

相关标签:
13条回答
  • 2020-12-08 08:09

    Anything you put in the session object stays there for the duration of the session unless it is cleaned up. Poor management of memory stored using inproc and stateserver will force you to scale out earlier than necessary. Store only an ID for the session/user in the session and load what is needed into the cache object on demand using a helper class. That way you can fine tune it's lifetime according to how often that data us used. The next version of asp.net may have a distributed cache(rumor).

    0 讨论(0)
  • 2020-12-08 08:11

    There is nothing inherently evil with session state.

    There are a couple of things to keep in mind that might bite you though:

    1. If the user presses the browser back button you go back to the previous page but your session state is not reverted. So your CurrentAccount might not be what it originally was on the page.
    2. ASP.NET processes can get recycled by IIS. When that happens you next request will start a new process. If you are using in process session state, the default, it will be gone :-(
    3. Session can also timeout with the same result if the user isn't active for some time. This defaults to 20 minutes so a nice lunch will do it.
    4. Using out of process session state requires all objects stored in session state to be serializable.
    5. If the user opens a second browser window he will expect to have a second and distinct application but the session state is most likely going to be shared between to two. So changing the CurrentAccount in one browser window will do the same in the other.
    0 讨论(0)
  • 2020-12-08 08:11

    Sessions are not evil, they serve an important function in ASP.NET application, serving data that must be shared between multiple pages during a user's "session". There are some suggestions, I would say to use SQL Session management when ever possible, and make certain that the objects you are using in your session collection are "serializable". The best practices would be to use the session object when you absolutely need to share state information across pages, and don't use it when you don't need to. The information is not going to be available client side, A session key is kept either in a cookie, or through the query string, or using other methods depending on how it is configured, and then the session objects are available in the database table (unless you use InProc, in which case your sessions will have the chance of being blown away during a reload of the site, or will be rendered almost useless in most clustered environments).

    0 讨论(0)
  • 2020-12-08 08:14

    I think using Session object is OK in this case, but you should remember Session can expire if there is no browser activity for long time (HttpSessionState.Timeout property determines in how many minutes session-state provider terminates the session), so it's better to check for value existence before return:

    public static Account GetCurrentAccount(HttpSessionState session)
    {
        if (Session[ACCOUNT]!=null)
            return (Account)Session[ACCOUNT];
        else
            throw new Exception("Can't get current account. Session expired.");
    }
    
    0 讨论(0)
  • 2020-12-08 08:14

    I think the "evil" comes from over-using the session. If you just stick anything and everything in it (like using global variables for everything) you will end up having poor performance and just a mess.

    0 讨论(0)
  • 2020-12-08 08:17

    Session as evil: Not in ASP.NET, properly configured. Yes, it's ideal to be as stateless as possible, but the reality is that you can't get there from here. You can, however, make Session behave in ways that lessen its impact -- Notably StateServer or database sessions.

    0 讨论(0)
提交回复
热议问题