Test if session is set on web service

J

Jeff User

Hello
I am hoping someone knows the answers I seek..

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, pointing to the word Session, the following
information pops up:
Session = <undefined value>

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

John
Thanks for the info. I have read this article, but it still has not
solved the problem, entirely. I added (EnableSession=true) to my
WebMethod attribute as shown:

[WebMethod(EnableSession=true)]

And, as stated in the article, hitting this service url from the
browser, getting the test page and testing it, works. Every time I
retest, the counter (good example code from article) increases.

However, the article does not address my case in which my client IS a
browser. My client app is a web page created in VS using the "Add Web
Refernce" tool. When my client hits the webService, it is treated as a
new session every time.

So, I added the cookieContainer code anyway, just to try it.
This yields a compile error :
c:\inetpub\wwwroot\TestClient\WebForm1.aspx.cs(118):
'TestClient.localhost.Service1' does not contain a definition for
'cookieContainer'

Service1 being the web service .

I am coding in C#, I think the example was VB, however I don't know
that that matters here.
Anyway, any other ideas?

Really stuck
Thanks again
Jeff
 
J

Jeff User

Greetings all
In addition to the 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
 
J

John Timney \( MVP \)

not that I did much, but your welcome all the same :)

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Jeff User said:
Greetings all
In addition to the 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


Hello
I am hoping someone knows the answers I seek..

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, pointing to the word Session, the following
information pops up:
Session = <undefined value>

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top