Jon*_*nas 11 asp.net web-services webforms session-state
我们有一个.net asmx Web服务,可以从javascript(使用ASP.Net AJAX)调用,并且需要访问Session.
[WebMethod(true)]
public string DoSomethingOnTheServer() { }
Run Code Online (Sandbox Code Playgroud)
我们遇到了会话被锁定在读/写请求上的问题.有没有办法将Web服务方法标记为要求只读访问Session?
谢谢!
小智 7
这是一个非常古老的主题,但我在寻找同一问题的答案时偶然发现了它.
我在其他地方找到了答案,并将其留在我的地方为其他互联网:
在Global.asax中,您可以为每个请求指定请求对会话对象应具有的访问权限,以及它是否应该阻止.
private void Application_BeginRequest(object sender, EventArgs e)
{
// This will set the session to read only for asmx services
// This will make the asmx services non blocking for other requests as it doesnt lock the session object
if (Context.Request.Path.Contains(".asmx/"))
{
Context.SetSessionStateBehavior(SessionStateBehavior.ReadOnly);
}
}
Run Code Online (Sandbox Code Playgroud)
这样,asmx服务始终只具有对会话的只读访问权限,并且不会阻止其他请求
这个http://msdn.microsoft.com/en-us/library/aa480509.aspx页面似乎表明答案是否定的 - 您不能将 WebSerivce 标记为具有EnableSessionState=ReadOnly.
如果您从同一进程同时进行 Web 服务调用,则请求将在服务器上进行序列化,以便在任一时间只有一个执行。与支持对 HttpSessionState 对象进行只读访问(允许同时处理多个请求)的 .ASPX 页面不同,ASP.NET Web 服务没有这种功能。启用会话的所有 Web 方法调用都具有读/写访问权限,并将在每个会话中进行序列化。
警告:那篇文章是旧的(2002)。
根据WebMethod 属性的MSDN 文档,有几个可能的属性,因此我不确定 WebMethod 属性中的“true”值会做什么。
你有没有尝试过:
[WebMethod(EnableSession=true)]
Run Code Online (Sandbox Code Playgroud)
根据此文档,您应该可以完全访问该会话。
| 归档时间: |
|
| 查看次数: |
3237 次 |
| 最近记录: |