2013-08-20 28 views
2

我有一個奇怪的問題,基本上我有一個購物車使用會話。當我使用IIS7部署網站時,看起來很好。我在一臺電腦上將產品添加到會議中,並顯示在我的購物籃中。當我從另一臺電腦訪問網站時,籃子裏有這個物品!! ??會話在其他計算機上可見嗎?

其我的理解是,每個用戶瀏覽器的會話實例是唯一的,這是正確的嗎?如果是這樣,我怎麼設法做到這一點?我知道它可能是一些愚蠢的東西,但我無法弄清楚,任何幫助都非常感謝!

我會車的代碼如下

#region Singleton Implementation 

     public static readonly ShoppingCart Instance; 
     static ShoppingCart() 
     { 
      // If the cart is not in the session, create one and put it there 
      // Otherwise, get it from the session 
      if (HttpContext.Current.Session["sCart"] == null) 
      { 
       Instance = new ShoppingCart(); 
       Instance.Items = new List<CartItem>(); 
       HttpContext.Current.Session["sCart"] = Instance; 
      } 
      else 
      { 
       Instance = (ShoppingCart)HttpContext.Current.Session["sCart"]; 
      } 

     } 

     protected ShoppingCart() { } 

     #endregion 
+0

你的會話單例實現是錯誤的。見例如http://stackoverflow.com/questions/6076459/static-singleton-in-asp-net-session。您不得將引用存儲在靜態變量中,您應該只返回它。 – IvanH

回答

5

你存儲單個靜態引用一個單一的全球ShoppingCart
這是一個可怕的想法。

每當你寫ShoppingCart.Instance時,它總是返回靜態構造函數中設置的原始值。

您需要擺脫單身人士,並始終使用會話。

2

正是由於public static readonly ShoppingCart Instance;

實例總是返回給大家,由於靜態相同(適用於應用程序級別)。

相關問題