会话变量由于某种原因而丢失 [英] session variables lost for some reason

查看:120
本文介绍了会话变量由于某种原因而丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置电子商店。在每个项目旁边我有一个 asp:imagebutton 当点击这个图像按钮时我正在检查会话varialbe session [basket] 是否存在然后我在列表(实体类)中添加值,并在会话中添加此列表。

I try to setup a E-shop. Next to every item I've an asp:imagebutton when this imagebutton is clicked I'm checking whehter the session varialbe session["basket"] exists or not if not then I add the values in a list (entity class) and I add this list in the session.

如果会话不为空,那么我将会话中的值检索到List并更改列表,然后将列表添加回会话。

if the session is not empty then I retrieve the values from session into List and change the list and then add the list back to session.

问题:

出于某种原因,我突然松开会话变量。我检查了我的手表(时间),这是不可预测的,有时它需要不到1分钟,有时3分钟,有时5分钟等....

For some reason I loose the session variable, suddenly. I checked on my watch (time) and it's unpredicatble sometimes it takes less than 1 minute, sometimes 3 minutes and sometimes 5 minutes etc....

为什么我会松动会话变量?

why do I loose the session variable?

我用Google搜索,我发现 - 如果您使用 Response.Redirect - w / o false会发生参数,或者如果你在 UpdatePanel 等等。

I googled and I've found - it can happen if you use Response.Redirect - w/o false parameter, or if you're in an UpdatePanel etc.

我在同一页面中丢失变量暂时的。

I'm loosing the variable in the same page for the moment.

整个想法放在一个会话变量中,并在第二个 aspx 页面中检查并检索会话变量。 ..但这并不总是有效,因为大多数情况下会话变量变空。有时候它有效。

The whole idea is put in a session variable and do checkout and retrieve the session variable in second aspx page... but this is not always working, because most of the case the session variables becomes empty. And sometimes it works.

有人可以建议吗?我需要检查什么和在哪里?
在某些网站页面(google)中,他们建议使用缓存,但缓存是基于应用程序的,因此每个人都会检索相同的值。

can someone advice ? what and where do I need to check? In some website pages (google) they advice to use caching, but caching is application based, so everybody will retrieve the same value.

在我的页面中用户(经过身份验证的或任意的用户),换句话说,任何没有登录的用户都应该订购(我会发票预付款)....

In my page any user (authenticated or anynomous user) in other words any user without login should able to order (I'll send invoice to pay upfront)....

我是我没有使用webfarm,也不使用web garden ...我只是检查了IIS - 网站 - 会话状态 - 它正在进行中,cookie设置=使用cookies,name = asp.net_sessionid,超时= 20 ....

I''m not using webfarm, nor web garden... I just checked the IIS - website - session state - It's in process, cookie settings = use cookies, name = asp.net_sessionid, time-out = 20....

请咨询?

这是 C# ASPX 3.5 IIS7.5

我没有PAGE_LOAD in我的ASPX页面。

I DON't have PAGE_LOAD in my ASPX page.

//我把sessoin = null的唯一地方是一个linkbutton,其余的我没有在会话中添加null [basket] ....

// the only place I put the sessoin=null is a linkbutton, for the rest I don't put null in session["basket"]....

protected void lnkDeleteAllSelected_Click(object sender, EventArgs e)
    {
        Session["Basket"] = null;
        ReloadBasketItems();

    }

 protected override void OnInit(EventArgs e)
    {

        base.OnInit(e);
        //System.Diagnostics.Debugger.Break();

        lvJuridisch.ItemDataBound += new EventHandler<ListViewItemEventArgs>(this.lv_ItemDataBound);
        lvJuridisch.DataBound += new EventHandler(lv_DataBound);

    }

imgButtonAddtoBasket - >在asp中定义为asp:imagebutton :listview

imgButtonAddtoBasket -> is defined as asp:imagebutton in the asp:listview

 protected void imgButtonAddtoBasket_Click(object sender, ImageClickEventArgs e)
    {
        ListViewDataItem lvi = ((sender as ImageButton).NamingContainer) as ListViewDataItem;
        DataKey currentDataKey = (lvi.NamingContainer as ListView).DataKeys[lvi.DataItemIndex];
        WebShopInfo SingleItem = new WebShopInfo();
        SingleItem.cd_type_pub = currentDataKey[0].ToString();
        SingleItem.no_pub = currentDataKey[1].ToString();
        SingleItem.no_suite_pub = Convert.ToInt32(currentDataKey[2]);
        SingleItem.cd_langue = Convert.ToChar(currentDataKey[3]);
        SingleItem.lb_titre_red = (lvi.FindControl("HiddenfieldProductRed") as HiddenField).Value;

        SingleItem.m_price = Convert.ToDecimal((lvi.FindControl("hiddenField_M_Price") as HiddenField).Value);
        SingleItem.nm_price = Convert.ToDecimal((lvi.FindControl("hiddenField_NM_Price") as HiddenField).Value);
        SingleItem.mt_pourc_tva = Convert.ToDecimal((lvi.FindControl("hfBTW") as HiddenField).Value);


        List<WebShopInfo> lws = new List<WebShopInfo>();
        if (Session["Basket"] == null)
        {

            //Session is empty so add listview to the session....
            //Session.Timeout = 20;  -- I tried this but this is not working too...
            lws.Add(SingleItem);
            Session["Basket"] = lws;
        }
        else
        {
            //Session is not empty so get asp:listview from the session.
            lws = Session["Basket"] as List<WebShopInfo>;

            WebShopInfo wsi = lws.Where(a => a.cd_type_pub == SingleItem.cd_type_pub &&
                                            a.no_pub == SingleItem.no_pub &&
                                            a.no_suite_pub == SingleItem.no_suite_pub &&
                                            a.cd_langue == SingleItem.cd_langue).SingleOrDefault<WebShopInfo>();
            if (wsi != null)
                lws.Remove(wsi);

            if (SingleItem.Count > 0)
                lws.Add(SingleItem);
            Session["Basket"] = lws;
        }

        ReloadBasketItems();
    }


推荐答案

听起来你的appdomain正在回收。您的 web.config 编译元素是否设置在 debug 模式?在调试模式下,ASP.NET每15次动态编译重置appdomain。当发生这种情况时,如果您的会话存储在内存中( InProc 模式 - 默认值),它们将会丢失。

Sounds like your appdomain is being recycled. Is your web.config compilation element set in debug mode? In debug mode, ASP.NET resets the appdomain every 15 dynamic compilations. When that happens, if your sessions are stored in memory (InProc mode - the default), they'll be lost.

仅供参考,每当您的网站上发生文件更改时,都会发生动态编译,例如编辑ASPX文件,生成图像或更改web.config。

FYI, a dynamic compilation will occur whenever a file change happens on your site, like you editing an ASPX file, or an image being generated, or a web.config change.

因此要么将网站置于发布模式(< compilation debug =false... /> ),要么增加 numRecompilesBeforeAppRestart web.config 中的15开始的值,或使用非易失性会话存储模式 - 例如SQL会话状态。

So either put the site into Release mode (<compilation debug="false" ... />), or increase the numRecompilesBeforeAppRestart value from 15 in web.config, or use a non-volatile session storage mode - e.g. SQL Session State.

这篇关于会话变量由于某种原因而丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆