In my web service I need to keep track of a value called store number. I have two methods I use to attempt to do this. Both have EnableSession=true set.
Method #1 - SetStoreNbr(string StoreNum) - I call this method and pass it the value. The value is then stored in the Session variable, like this:
Session["StoreNum"] = StoreNum;
In method #2 - GetStoreNbr(), I attempt to retrieve the value from the Session variable, like so:
string str = Session["StoreNum"].ToString();
However, when GetStoreNbr() is called, the Session variable is null, resulting in the following error:
System.Web.Services.Protocols.SoapException: Server was unable to process request.
---> System.NullReferenceException: Object reference not set to an instance of an object.
at THDEMPWebS.GetStoreNbr()
If I execute the Web Service in the browser via the asmx file, I can execute both of these of methods just fine. GetStoreNbr() will return me the store number. Attempting to execute these methods through an ASP.NET application raises the error mentioned
above.
I'm new to ASP.NET, so if there's a better way to do this, please feel free to mention it.
rpprince
0 Points
3 Posts
Session variable set to null between web service method calls
Apr 14, 2009 04:24 PM|LINK
In my web service I need to keep track of a value called store number. I have two methods I use to attempt to do this. Both have EnableSession=true set.
Method #1 - SetStoreNbr(string StoreNum) - I call this method and pass it the value. The value is then stored in the Session variable, like this:
Session["StoreNum"] = StoreNum;
In method #2 - GetStoreNbr(), I attempt to retrieve the value from the Session variable, like so:
string str = Session["StoreNum"].ToString();However, when GetStoreNbr() is called, the Session variable is null, resulting in the following error:
System.Web.Services.Protocols.SoapException: Server was unable to process request.
---> System.NullReferenceException: Object reference not set to an instance of an object.
at THDEMPWebS.GetStoreNbr()
If I execute the Web Service in the browser via the asmx file, I can execute both of these of methods just fine. GetStoreNbr() will return me the store number. Attempting to execute these methods through an ASP.NET application raises the error mentioned above.
I'm new to ASP.NET, so if there's a better way to do this, please feel free to mention it.
Thanks.
Vinija
Contributor
2460 Points
402 Posts
Re: Session variable set to null between web service method calls
Apr 14, 2009 05:31 PM|LINK
On your [WebMethod] attribute you should add EnableSession=true
Like that:
[
Blog
rpprince
0 Points
3 Posts
Re: Session variable set to null between web service method calls
Apr 14, 2009 08:23 PM|LINK
Thanks, that's how I've had it coded, and it didn't work.
I managed to fix the problem by switching to "Application" instead of Session, but if anyone knows why Session doesn't work that would be nice.
Vince Xu - M...
All-Star
80367 Points
6801 Posts
Re: Session variable set to null between web service method calls
Apr 17, 2009 06:29 AM|LINK
Hi,
You can try session mode "StateServer" which needs start the "StateServer" in server.
1. Deploy the sessionState in web.config as:
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="600"/>
2. Before starting web application, you need start StateServer Service.(Administration Tools-> Services->ASP.NET State Service->Start)
This link can lead you to know about Session State with StateServe and SqlServer:
http://wixuser.blogspot.com/2007/11/session-state-with-sqlserver-or_09.html
asifchouhan
Participant
1637 Points
298 Posts
Re: Session variable set to null between web service method calls
Apr 17, 2009 07:01 AM|LINK
In your Method#1 just check whether you are able to set the value of StoreNum to session variable.
in your web config
<add namespace="System.Web.SessionState"/>
<sessionState mode="InProc"></sessionState>
Regards
Asif
This can be beneficial to other community members reading the thread.
sudiptabaner...
Member
6 Points
4 Posts
Re: Session variable set to null between web service method calls
Jul 12, 2012 01:55 PM|LINK
http://www.prakriti.in/