how can I use ASP.NET session in WCF? or is there any alternative way to use "ASP.NET Session" like structure in WCF such as data storage?
-
1What do you want to accomplish by using the ASP.NET session in your WCF service? WCF services are typically "per-call" - there's no state stored between two calls and across multiple concurrent callers.marc_s– marc_s2009-08-05 09:15:06 +00:00Commented Aug 5, 2009 at 9:15
-
marc_s: true that is the typical way, but you can definitely have stateful services (at least depending on your binding) (see msdn.microsoft.com/en-us/library/ms730879.aspx)veggerby– veggerby2009-08-05 10:15:40 +00:00Commented Aug 5, 2009 at 10:15
3 Answers
Try having a look as ASPCompatibityMode with WCF and you then turn it on and share the session in the service method
1 Comment
You cannot use the ASP.NET session, since you can easily run a WCF service without having the ASP.NET engine fired up, eg. using a netTcpBinding.
There is however session handling native in WCF, where you can specify this on the service contract using the SessionMode parameter on the ServiceContract attribute.
See http://msdn.microsoft.com/en-us/library/ms733040.aspx for more details
Comments
In case anyone is still facing this issue (trying to use a SESSION variable in a .NET Web App consuming a WCF Service). Don't worry about the [AspNetCompatibilityRequirements .......] or adding aspNetCompatibilityEnabled="true" in the web.config.
After playing around with all that for a while I found out all I had to do was change each [WebMethod] within the _______.ASMX.CS to [WebMethod (EnableSession=true)].
So change [WebMethod] to [WebMethod (EnableSession=true)]. That's it.
I found out that from http://weblogs.asp.net/stevewellens/archive/2009/04/05/using-session-state-in-a-web-service.aspx
Thanks!
- Bonsai