简体   繁体   English

如何在会话存储在状态服务器(Web Garden)中的多线程中使用会话变量

[英]How to use Session variables with multi threading where session is stored in state server (Web Garden)

I need to change the state management from inProc to StateServer in old application.我需要将旧应用程序中的状态管理从inProc 更改为 StateServer

There is method that execute within a new thread.有在新线程中执行的方法。

The code look like as follows.代码如下所示。

protected void btnTransaction_Click(object sender, EventArgs e)
{
    timerTrans.Enabled = true;
    timerTrans.Interval = 200;
    Thread thread = new Thread(new ParameterizedThreadStart(DoAllWork));
    thread.Start(dbConnectionString);
}

public void DoAllWork(object connectionString)
{
   DBAccess dba = new DBAccess(connectionString.ToString());
   Session["NextTransactionId"] = dba.getNewTransactionId();

   //Other work

   foreach(transaction t in transactions)
   {
       ShowTrasnactionStatus("Processing transaction "+t.id);
   }

   //Other work

   Session["FinalStatus"] = "All Transactions Completed";
   Session["TransactionStatus"] = "Done";
}

private void ShowTrasnactionStatus(string statusMsg)
{
    Session["TransactionStatus"] = statusMsg;
}

protected void timerTrans_Tick(object sender, EventArgs e)
{
    lblStatus.Text = Session["TransactionStatus"].ToString();
    
    if (Session["TransactionStatus"].ToString() == "Done")
    {
         //Final Stage of Transaction Processing 
         lblStatus.Text = "Getting final status ...";
         billEndTime = DateTime.Now;

         ShowFinalStatus();
         timerBilling.Enabled = false;
    }
}

When the web garden is enabled the usage of session variables to update the status does not work as expected.启用 Web 园后,使用会话变量来更新状态无法按预期工作。

  • I have tried using class variable instead of session variables.我曾尝试使用类变量而不是会话变量。
  • Used Cache instead of session.使用缓存而不是会话。

None of them worked for me.他们都没有为我工作。

Note: This code is from a existing application that was developed at least 8 years ago.注意:此代码来自至少 8 年前开发的现有应用程序。

I only want to run this in web garden without breaking the functionality.我只想在不破坏功能的情况下在网络花园中运行它。

I created a database table and store the values there instead of the session vars.我创建了一个数据库表并将值存储在那里而不是会话变量。 There I used a GUID+KEY to identify the correct value in DB.在那里我使用了 GUID+KEY 来识别 DB 中的正确值。

I created a method to delete the records in db when the process completes, in that way the searching cost in the DB is reduced.我创建了一个方法来在进程完成时删除 db 中的记录,这样可以降低在 DB 中的搜索成本。

Note: There is a considerable cost of writing to db and retrieving it.注意:写入 db 并检索它的成本相当高。

This may be not the perfect solution for this.这可能不是此问题的完美解决方案。 But this can be considered as a high-cost workaround.但这可以被视为一种高成本的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM