xoail
xoail

Reputation: 3064

Session.SessionId persistence in requests to ashx

when i make a webrequest call to a service, why does it always generates a new sessionid?

This is how i call from sitea.com

WebClient fs = new WebClient();
            var data = fs.DownloadData("http://siteb.com/serice.ashx");
            var tostring = System.Text.Encoding.ASCII.GetString(data);
            return tostring;

This is the service code at siteb.com

[WebMethod(EnableSession = true)]
    private string Read(HttpContext context)
    {
        var value = context.Session.SessionId;
        if (value !=null) return value.ToString();
            return "false";
    }

value is always different for every request. How can i persist this?

Upvotes: 1

Views: 2773

Answers (2)

Tomek
Tomek

Reputation: 805

You have to receive session id and pass it to subsequent requests. By default, it will be sent in a cookie, but WebClient doesn't handle cookies. You can use CookieAwareWebClient to solve this:

public class CookieAwareWebClient : WebClient
{
    private CookieContainer m_container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = m_container;
        }
        return request;
    }
}

As long as you're reusing the same instance of web client, you should get the same session id (if the session won't time out of course).

Upvotes: 3

Waqar Janjua
Waqar Janjua

Reputation: 6123

From MSDN: An XML Web service client is uniquely identified by an HTTP cookie returned by an XML Web service. In order for an XML Web service to maintain session state for a client, the client must persist the cookie. Clients can receive the HTTP cookie by creating a new instance of CookieContainer and assigning that to the CookieContainer property of the proxy class before calling the XML Web service method. If you need to maintain session state beyond when the proxy class instance goes out of scope, the client must persist the HTTP cookie between calls to the XML Web service. For instance, a Web Forms client can persist the HTTP cookie by saving the CookieContainer in its own session state. Because not all XML Web services use session state and thus clients are not always required to use the CookieContainer property of a client proxy, the documentation for the XML Web service should state whether session state is used.

The following code example is a Web Forms client of an XML Web service that uses session state. The client persists the HTTP cookie that uniquely identifies the session by storing it in the client's session state.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<html>

<script runat="server">

    void EnterBtn_Click(Object Src, EventArgs E) 
{
  // Create a new instance of a proxy class for your XML Web service.
  ServerUsage su = new ServerUsage();
      CookieContainer cookieJar;

  // Check to see if the cookies have already been saved for this session.
  if (Session["CookieJar"] == null) 
    cookieJar= new CookieContainer();
      else
   cookieJar = (CookieContainer) Session["CookieJar"];

    // Assign the CookieContainer to the proxy class.
    su.CookieContainer = cookieJar;

  // Invoke an XML Web service method that uses session state and thus cookies.
  int count = su.PerSessionServiceUsage();         

  // Store the cookies received in the session state for future retrieval by this session.
  Session["CookieJar"] = cookieJar;

      // Populate the text box with the results from the call to the XML Web service method.
      SessionCount.Text = count.ToString();  
    }

</script>
<body>
   <form runat=server ID="Form1">

         Click to bump up the Session Counter.
         <p>
         <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
         <p>
         <asp:label id="SessionCount"  runat=server/>

   </form>
  </body>
  </html>

Read complete detail on MSDN http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx

Upvotes: 2

Related Questions