会话变量由于某种原因丢失

eth*_*hem 2 c# asp.net iis session-variables iis-7.5

我尝试建立一个电子商店。asp:imagebutton在单击此图像按钮时,我旁边的每个项目旁边都会检查会话变量是否session["basket"]存在(如果不存在),然后将值添加到列表(实体类)中,并将此列表添加到会话中。

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

问题:

由于某种原因,我突然松开了会话变量。我检查了自己的手表(时间),这是无法预料的,​​有时需要不到1分钟,有时需要3分钟,有时需要5分钟,等等。

为什么我松开会话变量?

我用Google搜索,发现-如果您使用Response.Redirect-没有错误的参数,或者您使用的是其他参数,则可能会发生这种情况UpdatePanel

我暂时在同一页面上丢失了变量。

整个想法都放在一个会话变量中,并在第二aspx页中进行检出并检索该会话变量……但这并不总是可行的,因为在大多数情况下,会话变量为空。有时它可行。

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)....

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....

please advice?

It's C#, ASPX 3.5, IIS7.5

I DON't have PAGE_LOAD in my ASPX page.

// 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);

    }
Run Code Online (Sandbox Code Playgroud)

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();
    }
Run Code Online (Sandbox Code Playgroud)

Jam*_*ack 5

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

仅供参考,只要您的网站上发生文件更改(例如您编辑ASPX文件,正在生成的图像或更改web.config),就会进行动态编译。

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