2

次の 2 つのメソッドを持つ .Net Webservice があります。

[WebMethod(EnableSession = true)]
public void A()
{
   HttpSessionState session = Session;

   Thread thread = new Thread(B);
   thread.Start();
}

[WebMethod(EnableSession = true)]
public void B()
{
   HttpSessionState session = Session;
}

シナリオ 1) Bメソッドを直接呼び出すと、セッションが null ではない

シナリオ 2)しかし、Aを呼び出すと、Bで session と HttpContext.Current の両方が null になります。

なんで?2 番目のシナリオでBでセッションを有効にするにはどうすればよいですか? A のセッションにアクセスするにはどうすればよいですか? そのセッションを B に渡す必要がありますか? はいの場合、どのように?

メソッド B は、パラメータとしてセッションを持つべきではありません。

ありがとう、

4

3 に答える 3

0
[WebMethod(EnableSession = true)]
public void A()
{
   HttpSessionState session = Session;

   Action action = () => B_Core(session);
   Thread thread = new Thread(action);
   thread.Start();
}

[WebMethod(EnableSession = true)]
public void B()
{
   HttpSessionState session = Session;
   B_Core(session);
}
private void B_Core(HttpSessionState session)
{
    // todo
}
于 2012-07-14T11:02:38.953 に答える
0

新しいスレッドで B を開始しているためです。

http://forums.asp.net/t/1276840.aspx または http://forums.asp.net/t/1630651.aspx/1を参照してください。

于 2012-07-11T12:11:44.130 に答える
-1

グローバルフィールドを使用する必要があります:

/// <summary>
/// Holds the current session for using in threads.
/// </summary>
private HttpSessionState CurrentSession;

[WebMethod(EnableSession = true)]
public void A()
{
   CurrentSession = Session;

   Thread thread = new Thread(B);
   thread.Start();
}

[WebMethod(EnableSession = true)]
public void B()
{
  //for times that method is not called as a thread
  CurrentSession = CurrentSession == null ? Session : CurrentSession;

   HttpSessionState session = CurrentSession;
}
于 2012-07-14T05:04:00.240 に答える