0

I would like to have only 1 session for each user. But the system generate new one session when I run ajax query. There were no problems when I tested it on debug mode on my computer. Problems rised only in client site on distributed application.

ASP page:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Text1" type="text" /><br />
<asp:Button ID="btn_ASP" runat="server" onclick="btn_ASP_Click" Text="Asp" />
<input id="btn_JS" type="button" value="JS" onclick="GetUsersList();" /><br />

Asp button which generates 'ConnectCount' Session element:

protected void btn_ASP_Click(object sender, EventArgs e)
{
    int count;
    if (HttpContext.Current.Session["ConnectCount"] == null)
        count = 1;
    else
        count = (int)HttpContext.Current.Session["ConnectCount"] + 1;
    HttpContext.Current.Session["ConnectCount"] = count;
    TextBox1.Text = count.ToString();          
}

Web Service code:

[WebMethod(EnableSession = true)]
[ScriptMethod]
public string Get_UsersList2()
{
    int count;
    if (HttpContext.Current.Session["ConnectCount"] == null)
        count = 1;
    else
        count = (int)HttpContext.Current.Session["ConnectCount"] + 1;
    HttpContext.Current.Session["ConnectCount"] = count;
    return count.ToString();
}

Ajax query to Web Service

function GetUsersList() {            
    $.ajax({
        url: '/WbSrv_ACTR.asmx/Get_UsersList2',
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: '{}',
        success: function (data, st) {
            if (st == "success") {
                if (data.d != "") {
                    text1.value = data.d;
                }
                else {
                }
            }
        },
        error: function () {
            alert("Cannot get list of users");
        }
    });
}

Code in buttons just adds 1 to Session["ConnectCount"].

So when I click 5 times on Asp button and 1 time on JQ button (on local IIS - testing):

result: asp = 5, jq = 6 (correct)

do the same operations on server IIS 7.5:

result: asp = 5, jq = 1 (incorrect)

Server uses other session. For testing - I've added some elements in Session via ASP button and couldn't find them in Web Service (Session was empty there but SessionID was the same)

Could anyone have solution or workaround?

Web config:

<system.web>
  <compilation debug="true" targetFramework="4.0">
    <assemblies>
      <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </assemblies>
  </compilation>
  <customErrors mode="Off" />
  <authorization>
      <deny users="?" />
  </authorization>
</system.web>
<system.webServer>
    <security>
        <authentication>
            <windowsAuthentication enabled="true" />
            <anonymousAuthentication enabled="false" />
        </authentication>
    </security>
</system.webServer>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Vladimir
  • 1
  • 2

1 Answers1

0

this is wrong way of using session in web service

Session["ConnectCount"];

The right way is

HttpContext.Current.Session["ConnectCount"];

So your function should be

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public string Get_UsersList2()
    {
        int count;
        if ( HttpContext.Current.Session["ConnectCount"] == null)
            count = 1;
        else
            count = (int) HttpContext.Current.Session["ConnectCount"] + 1;
         HttpContext.Current.Session["ConnectCount"] = count;
        return count.ToString();
    }

Reference
How can I access session in a webmethod?

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • Thanks a lot for the answer. I've changed the code as you suggested. but unfortunately it didn't solve my problem. When I run ajax query - system gets Session with the same ID as in APS button but it doesn't have any elements - the session is empty. But in debug mode all works fine - I can add parameter in Session from asp button and read it from Web Service. – Vladimir Mar 26 '13 at 23:17