Search code examples
c#wcfwcf-rest

Can RESTful WCF service hold session data?


We are developing some native mobile applications. These application consume REST Service. It is WCF Service. We are using HttpContext.Current.Session["Key"]=Value in WCF for holding some details.

Can WCF service hold session information for windows client ?

Reference : WCF sessions with a wsHttpBinding and without windows security


Solution

  • Can WCF service hold session information for windows client ?

    Firstly, - the linked article is not relevant because while wsHttpBinding does support sessions, it is not a RESTful binding.

    If you're hosting a REST service in WCF you will not have access to a native session because webHttpBinding, which is the binding used for REST services in WCF, does not support sessions.

    So, although you'll have access to HttpContext.Current, the Session property will be null.

    Secondly, it's arguable that the use of sessions violates RESTful principals (see post here). This is because HTTP was designed to be a stateless in nature.

    If you really do need sessions, you are either going to have to use a SOAP-based binding (like wsHttpBinding), or implement some way of storing state for a given user on the service itself.