I plan to use both application and session state items in an app I'm working on. Before I design too much I was playing with some simple code to get a feel for how this works.
The code I've posted below contains both a simple web service (Engine) and a C# console app client that uses the web service (Class1).
When I access the web service in a browser by typing the following url:
http://localhost/StateTesterWebService/Engine.asmx
all three web methods work fine.
However, when I run the console app containing the Class1 code below the program bombs on the following statement:
sessionVal = eng.Service2( );
I would think if there is a problem with the web service accessing it from both the browser and client app would bomb. Can any one explain
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace StateTesterWebService
{
public class Engine : System.Web.Services.WebService
{
private int baseValue = 5;
public Engine()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
SimulatedDBConnection dbConn = new SimulatedDBConnection( );
Application["DatabaseConnection"] = dbConn;
}
#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 void Service1( int incrementBaseBy )
{
// Service 1 will be used to demonstrate setting session state functionality
Session["SessionValue"] = baseValue + incrementBaseBy;
}
[WebMethod(EnableSession=true)]
public int Service2( )
{
// Service 2 will be used to demonstrate getting session state functionality
return (int)Session["SessionValue"];
}
[WebMethod]
public string Service3( )
{
// Service 3 will be used to demonstrate getting application state functionality
SimulatedDBConnection dbConn = (SimulatedDBConnection)Application["DatabaseConnection"];
return dbConn.GetConnectionMessage( );
}
}
}
using System;
using StateTesterClient1.StateTesterWebService;
namespace StateTesterClient1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Engine eng = new Engine( );
int sessionVal;
Console.WriteLine( eng.Service3( ) );
eng.Service1( 5 );
sessionVal = eng.Service2( );
Console.WriteLine( "Client 1 Session state variable is: " + sessionVal );
}
}
}

Session State Question
Barry333
I posted this question but I think I know what the problem is.
Since I'm accessing the web service from a client application, not a browser, I need to turn off using cookies.
Can someone confirm
Thanks.