1

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
  • 1
    What 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. Commented 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) Commented Aug 5, 2009 at 10:15

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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

0

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

1 Comment

That works with Web Methods, but if the WCF service is stand alone, it wont work. blogs.msdn.com/wenlong/archive/2010/02/21/… covers how to do this in detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.