Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readonly access to session in a web service call?

We have a .net asmx web service that gets called from javascript (using ASP.Net AJAX), and requires access to Session.

[WebMethod(true)]
public string DoSomethingOnTheServer() { }

We're running into the problem of session being locked on a read/write request. Is there any way to mark a web service method as requiring read-only access to Session?

Thanks!

like image 383
Jonas Avatar asked Jun 01 '09 16:06

Jonas


2 Answers

This is a really old thread, but i stumbled on it in my search for an answer to the same question.

I found the answer else where, and will leave it here for other internets in my place:

In Global.asax you can specify for each request, what access the request should have to the session object, and thereby if it should block or not.

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);
    }
}

This way asmx services always only have read only access to the session and will not block other requests

like image 54
Mikael Hardø Avatar answered Sep 29 '22 23:09

Mikael Hardø


This http://msdn.microsoft.com/en-us/library/aa480509.aspx page seems to suggest that the answer is "no" - you cannot mark a WebSerivce as having EnableSessionState=ReadOnly.

If you are making simultaneous Web service calls from the same process, the requests will be serialized at the server so that only one will execute at any one time. Unlike .ASPX pages that have support for read-only access to the HttpSessionState object, which allows for simultaneous processing of multiple requests, there is no such capability with ASP.NET Web services. All Web method calls with sessions enabled have read/write access and will be serialized within each session.

Warning: That article is old (2002).

like image 39
Chris Fewtrell Avatar answered Sep 30 '22 01:09

Chris Fewtrell