2016-05-31 111 views
1

我沒有維護線程或會話,但是由於創建了一個新線程,定義的變量在回發期間丟失。思考?ASP.NET Postback創建一個新線程和一個新會話

每次單擊button1或button2時,currentthreadid和sessionid都會更改。我更關心正在創建的新線程。請幫助 !

這裏是我的WebForm1.aspx的代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UpdatePanelTest2.WebForm1" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"></asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
      <ContentTemplate> 
       <asp:Label ID="Label1" runat="server" Text="Before Click" /> 
       <asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" CausesValidation="false"/> 
      </ContentTemplate> 
      <Triggers> 
       <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" /> 
      </Triggers> 
     </asp:UpdatePanel> 
     <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click"/> 
    </form> 
</body> 
</html> 

這裏是我的WebForm1.aspx.cs中的代碼

namespace UpdatePanelTest2 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

      string currentthreadid = AppDomain.GetCurrentThreadId().ToString(); 
      string sessionid = Session.SessionID; 

      if (IsPostBack) { } 
      if (IsAsync) { } 
      if (IsPostBackEventControlRegistered) { } 

      if (ScriptManager1.IsInAsyncPostBack) { } 
     } 

     protected void Page_Init(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      this.Label1.Text = "After Click"; 
     } 

     protected void Button2_Click(object sender, EventArgs e) 
     { 

     } 
    } 
} 

我的web.config文件

<?xml version="1.0"?> 

<!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> 

<configuration> 
    <system.web> 
     <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

</configuration> 

UPDATE

的原因,我需要的是同一個線程用於回傳要求,因爲我保持一定的變量,然後再利用這些變量對當前池線程

代碼:

private static readonly ThreadLocal<PageContext> _context = new ThreadLocal<PageContext>(typeof(PageContext), "_context", true); 
     private string[] _path; 

     public static PageContext ForCurrentThread 
     { 
      get { return _context.Value ?? (_context.Value = new PageContext()); } 
     } 

public void Initialize(HttpRequest _request) 
     { 
somestring = null; 
} 

public string somestring { get; set; } 

當頁面加載第一次,somestring被賦予一個值,其他的類然後使用相同的上下文並重新使用somestring的值。如果爲回發請求創建了新線程,這不起作用。

+0

無法理解您的問題。您只想存儲一次值? –

+0

還有一個類似的線程 - 請按照下面的鏈接。 http://stackoverflow.com/questions/222999/how-to-persist-variable-on-postback – meghlashomoy

+0

你想保持什麼變量? – smoksnes

回答

2

這是設計。 SessionID將不會保存,直到您真正使用它。

https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.sessionid.aspx

當使用基於cookie的會話狀態,ASP.NET不用於會話的數據,直到會話對象用於分配 存儲。作爲 結果,會爲每個頁面請求生成新的會話ID,直到訪問會話對象 。

如果您添加到您的代碼中的SessionID將保持不變:

Session["foo"] = 1; 

ASP.Net並不能保證你得到每個請求的相同的線程。該應用程序與多個線程一起工作,它將從線程池中獲取線程並使用它來處理請求。

我會說這沒什麼可擔心的。

我會避免存儲特定線程的東西。沒有辦法知道你是否會在下一個請求中獲得相同的線程。不應該爲特定用戶存儲值(例如會話)嗎?

public static PageContext ForCurrentSession 
{ 
    get 
    { 
     if(Session["PageContext"] == null) 
     { 
      Session["PageContext"] = new PageContext(); 
     } 
     return Session["PageContext"] as PageContext; 
    } 
} 
+0

謝謝!我認爲這整個方法是錯誤的...如果我使用會話,然後在負載平衡的環境中,我需要一個數據庫。我會改變它的工作方式。文本變量作爲餅乾和靜態集合/列表... –

+0

是的,我認爲這是一個很好的idéa。 Hanselman有一個關於Session和多個服務器的好博客。 http://www.hanselman.com/blog/LoadBalancingAndASPNET.aspx – smoksnes

+0

ASP.Net甚至不保證單個請求將被單個線程處理 - 它可以在運行通過頁面生存期事件時在線程之間跳轉,並且如果請求處理程序是異步的(傳統風格或'async' /'await')。 –

相關問題