REST WCF服务和会话‏在ASP.NET

时间:2021-01-01 16:49:27

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.

我一直试图从WCF REST服务中访问asp.net应用程序的当前会话对象。

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

根本没有成功。从服务访问的会话对象与aspx页面中的会话对象不同。

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

因此,我的问题是:是否可能通过HttpContext.Current访问REST WCF服务中的当前会话。会话吗?

The code has the following points:

守则有以下几点:

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

in web.config:

在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,

谢谢你!

2 个解决方案

#1


4  

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.

为了让会话恢复,您需要提供一个密钥。在普通的asp.net应用程序中,密钥由用户通过cookie或url参数提供。

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?

您打算如何从REST客户端获得该密钥?这些客户端如何在身份验证之后获得该密钥?他们把钥匙放在哪里?

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

这就是为什么大多数基于REST的服务都使用api访问键和另一个键来签名每个请求。

IMHO sessions are irrelevant in REST based designs.

IMHO会话与REST的设计无关。

#2


1  

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:

我知道这个问题很久以前就有人问过,但这可以通过在asp.net应用程序中托管wcf restful服务来实现,然后在服务类的顶部添加以下属性:

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

HttpContext:运行在ASP中的WCF服务。NET兼容模式可以访问当前状态及其关联状态。

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

更多信息请参见这里:AspNetCompatibilityRequirements的真正含义是什么?

#1


4  

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.

为了让会话恢复,您需要提供一个密钥。在普通的asp.net应用程序中,密钥由用户通过cookie或url参数提供。

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?

您打算如何从REST客户端获得该密钥?这些客户端如何在身份验证之后获得该密钥?他们把钥匙放在哪里?

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

这就是为什么大多数基于REST的服务都使用api访问键和另一个键来签名每个请求。

IMHO sessions are irrelevant in REST based designs.

IMHO会话与REST的设计无关。

#2


1  

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:

我知道这个问题很久以前就有人问过,但这可以通过在asp.net应用程序中托管wcf restful服务来实现,然后在服务类的顶部添加以下属性:

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

HttpContext:运行在ASP中的WCF服务。NET兼容模式可以访问当前状态及其关联状态。

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

更多信息请参见这里:AspNetCompatibilityRequirements的真正含义是什么?