Using Session from a Windows App throgh a WebService

P

Per Loevgren

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
 
P

Per Loevgren

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
 
P

Per Loevgren

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top