HttpContext items and one-way methods

A

Ales Pour

Hi,

I try to put some custom objects into HttpContext.Current.Items collection
in one-way webmethod, but they seem not to be there in another invocation.
Is it supposed to work?

Thank you!

Regards,
Ales Pour
 
V

Vitaly Filimonov [MSFT]

Ales,

HttpContext.Current gives you, as name implies, http call context for the
current call. If you need to preserve some information between two calls to
your web service you can use either cookies or session state. Session state
is easier to implement, but can give you some performance drop. If your web
service doesn't have high number of the requests, you can use sessions.

Technically, you'll need to enable session state in web.config like so:
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="20"
/>
Most likely this xml node is already present in your web.config. Once it is
there, you can run "session-enabled" web methods, for example (C# example
from Visual Studio.NET help on WebMethod.EnableSession property):

[WebMethod(Description="Per session Hit Counter",EnableSession=true)]
public int SessionHitCounter()
}
if (Session["HitCounter"] == null)
{
Session["HitCounter"] = 1;
}
else
{
Session["HitCounter"] = ((int) Session["HitCounter"]) + 1;
}
return ((int) Session["HitCounter"]);
}
 
A

Ales Pour

You are absolutely right, I've been using wrong context. Thanks!

AlesP

Vitaly Filimonov said:
Ales,

HttpContext.Current gives you, as name implies, http call context for the
current call. If you need to preserve some information between two calls to
your web service you can use either cookies or session state. Session state
is easier to implement, but can give you some performance drop. If your web
service doesn't have high number of the requests, you can use sessions.

Technically, you'll need to enable session state in web.config like so:
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="20"
/>
Most likely this xml node is already present in your web.config. Once it is
there, you can run "session-enabled" web methods, for example (C# example
from Visual Studio.NET help on WebMethod.EnableSession property):

[WebMethod(Description="Per session Hit Counter",EnableSession=true)]
public int SessionHitCounter()
}
if (Session["HitCounter"] == null)
{
Session["HitCounter"] = 1;
}
else
{
Session["HitCounter"] = ((int) Session["HitCounter"]) + 1;
}
return ((int) Session["HitCounter"]);
}

--
Vitaly.
-------
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

Ales Pour said:
Hi,

I try to put some custom objects into HttpContext.Current.Items collection
in one-way webmethod, but they seem not to be there in another invocation.
Is it supposed to work?

Thank you!

Regards,
Ales Pour
 

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,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top