Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST WCF service and Session‏ in ASP.NET

Please help if you can.

I have been trying to access the current session object of an asp.net application from within a WCF REST service.

There has been no success at all. the session object accessed from the service is not the same one in the aspx pages.

So, here is my question: Is it possible to access the current session in a REST WCF service through HttpContext.Current.Session ?

The code has the following points:

 [AspNetCompatibilityRequirements
(RequirementsMode = 
AspNetCompatibilityRequirementsMode.Allowed)] // I have also tried Required
public class DataService : IDataService

in web.config:

<system.serviceModel>
  <behaviors>
   <endpointBehaviors>
    <behavior name="ClosedRoom.DataServiceBehavior">
     <enableWebScript />
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" >
  <baseAddressPrefixFilters>
        <add prefix="http://localhost:63399"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>

  <services>
    <service name="ClosedRoom.DataService">
      <endpoint address="" behaviorConfiguration="ClosedRoom.DataServiceBehavior"
        binding="webHttpBinding" contract="ClosedRoom.IDataService" />
    </service>
  </services>
</system.serviceModel>

Thank you,

like image 813
Mohammed Swillam Avatar asked Dec 28 '10 12:12

Mohammed Swillam


People also ask

What is REST WCF service?

Introduction to WCF RESTWCF stands for Windows Communication Foundation, it works based on Simple Object Access Protocol. REST stands for Representational State Transfer. REST supports cross-platform and exchange data in JSON and XML format and use the HTTP protocols (GET, POST, PUT, PATCH and DELETE)

What is difference between SOAP and REST WCF service?

Actually only the difference is how clients access our service. Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.).

What is session in WCF?

WCF sessions have the following main conceptual features: They are explicitly initiated and terminated by the calling application (the WCF client). Messages delivered during a session are processed in the order in which they are received. Sessions correlate a group of messages into a conversation.

Can we use session in WCF service C#?

WCF manage session by creating the instance of the service class. These created instance(s) handle the incoming service request. In WCF, session is the way of managing the services instance(s) so that server can used these instances in an optimized way.


2 Answers

In order for a session to be rehidrated, you need to supply a key. In a normal asp.net application that key is supplied by user either via cookie or url parameter.

How are you planning to acquire that key from the REST client? How those clients get that key initially after the authentication? Where they store the key?

This is why most of the REST based services take a api access key and also another key to sign every request.

IMHO sessions are irrelevant in REST based designs.

like image 95
chandmk Avatar answered Oct 03 '22 02:10

chandmk


I know this question was asked a long time ago but this can be achieved by hosting the wcf restful service within an asp.net application and then on the top of your service class add the following attribute:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

This enables many things including:

HttpContext: WCF services running in ASP.NET Compatibility Mode can access Current and its associated state.

See here for more info: What does AspNetCompatibilityRequirements really mean?

like image 25
Rafi Avatar answered Oct 03 '22 02:10

Rafi