Using Session from a Windows App throgh a WebService

Discussion in 'ASP .Net Web Services' started by Per Loevgren, Jun 28, 2004.

  1. Per Loevgren

    Per Loevgren Guest

    I want to set and get information in a Session variable on an IIS
    application (ASP.NET website) from a Windows App.

    I use webservices to communicate with the IIS app. and keep all calls
    from the webservices in the same session.

    I have deployed my webservices to the folder where the IIS app resides
    and when I call to get the value of an Application variabel there are
    no problems, but as soon as I call to get the value of a Session
    variable I get an exception.

    If i call the webservice from my ASP.NET app. there are no problems
    with the session variable.

    The Webservice looks as follows:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;

    namespace StatusWS
    {

    public class GetStatus : System.Web.Services.WebService
    {
    public GetStatus()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services
    Designer
    InitializeComponent();
    }

    #region Component Designer generated code

    //Required by the Web Services Designer
    private IContainer components = null;

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if(disposing && components != null)
    {
    components.Dispose();
    }
    base.Dispose(disposing);
    }

    #endregion

    [WebMethod (EnableSession=true)]
    public string GetStatusApplication( string strSerialNo, string OTP )
    {
    string strStatus = Application["Status"] != null ?
    Application["Status"].ToString() : "<No status found>";
    return Server.HtmlEncode(strStatus);
    }

    [WebMethod (EnableSession=true)]
    public string GetStatusSession( string strSerialNo, string OTP )
    {
    string strStatus = Session["Status"] != null ?
    Session["Status"].ToString() : "<No status found>";
    return Server.HtmlEncode(strStatus);
    }
    }
    }

    I have deployed it by copying the asmx file to the ASP.NET application
    folder, and added a webreference to it in my windows app.


    The windows app. calls like this:

    private void button1_Click(object sender, System.EventArgs e)
    {
    textBox1.Text = new StatusWS.GetStatus().GetStatusSession( "", "" );
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    textBox2.Text = new StatusWS.GetStatus().GetStatusApplication( "", ""
    );
    }

    I have tried to set CookieContainer on my webservice object but that
    didn't do any good.

    Any ideas or thoughts are most welcome

    Rgds.,

    Per Loevgren
    Per Loevgren, Jun 28, 2004
    #1
    1. Advertising

  2. Show code for this.

    > I have tried to set CookieContainer on my webservice object but that
    > didn't do any good.
    >
    > Any ideas or thoughts are most welcome
    >
    > Rgds.,
    >
    > Per Loevgren
    Kondratyev Denis, Jun 28, 2004
    #2
    1. Advertising

  3. Per Loevgren

    Per Loevgren Guest

    Some info i forgot in my previous post ...

    The code that sets the CookieContainer is like this:

    private void button1_Click(object sender, System.EventArgs e)
    {
    StatusWS.GetStatus objTest = new StatusWS.GetStatus();
    objTest.CookieContainer = new CookieContainer();
    textBox1.Text = objTest.GetStatusSession( "", "" );
    }

    The exception thrown is:

    An unhandled exception of type
    'System.Web.Services.Protocols.SoapException' occurred in
    system.web.services.dll

    Additional information: System.Web.Services.Protocols.SoapException:
    Server was unable to process request. --->
    System.NullReferenceException: Object reference not set to an instance
    of an object.
    at StatusWS.GetStatus.GetStatusSession(String strSerialNo, String
    OTP) in c:\inetpub\wwwroot\statusws\statusws.asmx.cs:line 64
    --- End of inner exception stack trace ---


    Rgds.,

    Per Loevgren
    Per Loevgren, Jun 29, 2004
    #3
  4. Per Loevgren

    Per Loevgren Guest

    Re: Using Session from a Windows App through a WebService

    I found the solution.

    What I did wrong was to create a new CookieContainer for each call to
    the webservice.

    Instead I have create a private variable to hold the CookieContainer
    and then only set this variable if its value is null.

    i.e.:

    private CookieContainer thisCookie;

    private void button1_Click(object sender, System.EventArgs e)
    {
    StatusWS.GetStatus objWS = new StatusWS.GetStatus();
    if ( thisCookie == null )
    {
    thisCookie = new CookieContainer();
    }
    objWS.CookieContainer = thisCookie;
    textBox1.Text = objWS.GetStatusSession( "", "" );
    }

    Rgds.,

    Per Loevgren
    Per Loevgren, Jun 29, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tom
    Replies:
    4
    Views:
    14,077
    jpowers5882
    Oct 24, 2008
  2. Replies:
    1
    Views:
    697
    =?Utf-8?B?QWx0YWYgQWwtQW1pbiBOYWp3YW5p?=
    May 19, 2006
  3. Odin Jensen

    Problems launching legacy windows app from webservice

    Odin Jensen, Sep 18, 2003, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    92
    richlm
    Sep 19, 2003
  4. Joshua Moore

    Windows app using webservice sessions and cookies

    Joshua Moore, Jan 18, 2006, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    154
    Joshua Moore
    Jan 19, 2006
  5. Roberto Giorgetti

    Error connection using webservice locally (3 webservice in balanci

    Roberto Giorgetti, Jun 13, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    189
    Roberto Giorgetti
    Jun 13, 2006
Loading...

Share This Page