views:

702

answers:

3

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?

+2  A: 

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

veggerby
+2  A: 

Try having a look as ASPCompatibityMode with WCF and you then turn it on and share the session in the service method

chugh97
True, but the OP should first make certain that it is the **ASP.NET** session state that he needs. It's possible he just needs a mechanism _like_ ASP.NET Session State.
John Saunders
A: 

Hello, 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
BonsaiGuy
That works with Web Methods, but if the WCF service is stand alone, it wont work. http://blogs.msdn.com/wenlong/archive/2010/02/21/using-asp-net-sessions-from-wcf.aspx covers how to do this in detail.
Serapth