How do I get session state

D

Donald Adams

How can I get session state for my user in a web service?

I understand that with WinForm clients that this was not necessary, but now
with Asp.net Ajax, I can access all the server objects but the session one.
I can use the User object but it's a lot of extra work.

Is Microsoft employees listening here?

Thanks in advance,
Donald Adams
 
L

Laurent Bugnion [MVP]

Hi,

Donald said:
How can I get session state for my user in a web service?

I understand that with WinForm clients that this was not necessary, but
now with Asp.net Ajax, I can access all the server objects but the
session one. I can use the User object but it's a lot of extra work.

Is Microsoft employees listening here?

Thanks in advance,
Donald Adams

You need to enable the Session state for your web service. By default,
they are stateless.

Use

[WebMethod( EnableSession=true )]

Also, see this:
http://geekswithblogs.net/lbugnion/archive/2007/02/25/107243.aspx
This is also true for web services.

HTH,
Laurent
 
D

Donald Adams

Thanks that worked, but I was hoping I could use the session object in the
webservice constructor too.

,,,Donald

Laurent Bugnion said:
Hi,

Donald said:
How can I get session state for my user in a web service?

I understand that with WinForm clients that this was not necessary, but
now with Asp.net Ajax, I can access all the server objects but the
session one. I can use the User object but it's a lot of extra work.

Is Microsoft employees listening here?

Thanks in advance,
Donald Adams

You need to enable the Session state for your web service. By default,
they are stateless.

Use

[WebMethod( EnableSession=true )]

Also, see this:
http://geekswithblogs.net/lbugnion/archive/2007/02/25/107243.aspx
This is also true for web services.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
J

John Saunders

Donald Adams said:
Thanks that worked, but I was hoping I could use the session object in the
webservice constructor too.

What prevents you from using it in the constructor?

John
 
D

Donald Adams

you mean
[WebMethod( EnableSession=true )]
can be used on the constructor too? I didn't think of the constructor as a
method that the client could call.
I thought there would be something that would apply to the whole class like:
[WebService( EnableSession=true )]
but that gives me an error.

,,,Donald Adams
 
J

John Saunders

Donald Adams said:
you mean
[WebMethod( EnableSession=true )]
can be used on the constructor too? I didn't think of the constructor as
a method that the client could call.
I thought there would be something that would apply to the whole class
like:
[WebService( EnableSession=true )]
but that gives me an error.

Ok, good point.

But what do you want to use Session for in the constructor? Were you aware
that the constructor will be called on every single web service call?

John
 
D

Donald Adams

John,

yes, I'm aware it will be called everytime, I want to initialize some values
that will be used in every web method.

Thanks for your help.
,,,Donald

John Saunders said:
Donald Adams said:
you mean
[WebMethod( EnableSession=true )]
can be used on the constructor too? I didn't think of the constructor as
a method that the client could call.
I thought there would be something that would apply to the whole class
like:
[WebService( EnableSession=true )]
but that gives me an error.

Ok, good point.

But what do you want to use Session for in the constructor? Were you aware
that the constructor will be called on every single web service call?

John
 
J

John Saunders

Donald Adams said:
John,

yes, I'm aware it will be called everytime, I want to initialize some
values that will be used in every web method.

Donald,

What I mean is that I didn't see what use you would make of Session state
inside of the constructor. Do you mean that you want to use it to _read_
values from session state during the constructor, that you'd then use to
initialize values that will be used in every web method? I couldn't
understand a use case for _writing_ into session state during the
constructor call.

At any rate, you can simply place all of your session-specific
initialization code into a method which is called from each web method,
since session state will be available then.

John
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top