Test id Session variable is set

J

Jeff User

Greetings
I am writing code in a C# web service solution.
I want to create an instance of an object (that I created) in a
Session variable. I need to check to see if the Session variable has
been set yet. If the variable already contains an instance of this
object then I will retrieve and use the existing object, rather than
create a new instance. ie. I will only create a new object the first
time the procedure is called, and then save it for the next time.

So, in an article I found in my MSDN help entitled
"Code: Reading Values From Session State (Visual C#)"
the procedure recommended to check to see if the Session var was set
yet, is this:
if (Session["SomeVarName"] == null)

When I do this, I get the
"Object reference not set to an instance of an object" error.
Checking in debug, I see that "Session" is not defined.

I thought Session was supposed to be created first thing when a page
is accessed. So, how come mine is not defined yet?

Could it be because it is an .asmx file rather than an aspx file?

If asmx pages don't create a Session object, is there a way to
maintain session information in a web service?

Thanks much
Jeff
 
J

Jeff User

To be more specific in reference to:
Checking in debug, I see that "Session" is not defined.

Breaking on this line :
if (Session["SomeVarName"] == null)
and pointing to the word Session, The following information pops up:
Session = <undefined value>

Thanks again
Jeff




Greetings
I am writing code in a C# web service solution.
I want to create an instance of an object (that I created) in a
Session variable. I need to check to see if the Session variable has
been set yet. If the variable already contains an instance of this
object then I will retrieve and use the existing object, rather than
create a new instance. ie. I will only create a new object the first
time the procedure is called, and then save it for the next time.

So, in an article I found in my MSDN help entitled
"Code: Reading Values From Session State (Visual C#)"
the procedure recommended to check to see if the Session var was set
yet, is this:
if (Session["SomeVarName"] == null)

When I do this, I get the
"Object reference not set to an instance of an object" error.
Checking in debug, I see that "Session" is not defined.

I thought Session was supposed to be created first thing when a page
is accessed. So, how come mine is not defined yet?

Could it be because it is an .asmx file rather than an aspx file?

If asmx pages don't create a Session object, is there a way to
maintain session information in a web service?

Thanks much
Jeff
 
J

Jeff User

Greetings all
In addition to an article that John Timney suggested:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnservice/html/service08062002.asp

I would also recommend this article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;816637

The first article got me 3/4 of the way there.
The 2nd article listed here includes saving the cookie on the client
side for use on the next call to the server.

Thanks again John for giving me the good start that I needed.

Jeff


To be more specific in reference to:
Checking in debug, I see that "Session" is not defined.

Breaking on this line :
if (Session["SomeVarName"] == null)
and pointing to the word Session, The following information pops up:
Session = <undefined value>

Thanks again
Jeff




Greetings
I am writing code in a C# web service solution.
I want to create an instance of an object (that I created) in a
Session variable. I need to check to see if the Session variable has
been set yet. If the variable already contains an instance of this
object then I will retrieve and use the existing object, rather than
create a new instance. ie. I will only create a new object the first
time the procedure is called, and then save it for the next time.

So, in an article I found in my MSDN help entitled
"Code: Reading Values From Session State (Visual C#)"
the procedure recommended to check to see if the Session var was set
yet, is this:
if (Session["SomeVarName"] == null)

When I do this, I get the
"Object reference not set to an instance of an object" error.
Checking in debug, I see that "Session" is not defined.

I thought Session was supposed to be created first thing when a page
is accessed. So, how come mine is not defined yet?

Could it be because it is an .asmx file rather than an aspx file?

If asmx pages don't create a Session object, is there a way to
maintain session information in a web service?

Thanks much
Jeff
 

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

Top