1

I have a JQuery AJAX call for ASP.NET WebMethod.

I understand the async: true that allows the WebPage not to be locked until the function returns and therefore i set it to 'true'.

The problem that i am facing is that the inner method GetVersionFromDataBase() inside the GetVersion() takes a long time until it finishes, and meanwhile the whole backend is locked (single threaded).

The user experience is that for the other user who are browsing the website, even on different pages, their screen is locked until the 'heavy' method returns.

How can i make the function GetVersionFromDataBase() be in a different thread / tasks?

$.ajax({
    type: "POST",
    url: "Default.aspx/GetVersion",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: true,
    success: function (result) {
        alert('the version is:' + result.d);
    }
});



public partial class Default : System.Web.UI.Page
{
    [WebMethod]
    public string GetVersion()
    {
        string version = GetVersionFromDatabase(); // this is a heavy method freezes the program till it finishes
        return version;
    }
}
user829174
  • 6,132
  • 24
  • 75
  • 125
  • When you say the "the whole backend is locked (single threaded)", do you mean this happens in a production environment in IIS? Or is this just happening in your development web server? Each request should be handled by IIS asynchronously. – Rob Jun 11 '13 at 17:31
  • Rob, it is happening on my development web server. i opened a new tab while the method was invoked and the whole website just hang. do you think it will act differently on production IIS? – user829174 Jun 11 '13 at 17:46
  • In my experience, it only happens in the development server. See this post: http://stackoverflow.com/questions/9036150/asp-net-development-server-concurrent-processing-doesnt-work – Rob Jun 11 '13 at 17:52
  • I've added an answer below, if you feel it's the correct answer, please mark it. – Rob Jun 11 '13 at 18:00

2 Answers2

0

EDIT: @Rob is right, a long running process kicked off by one user (even the way you're doing it) shouldn't really affect other users unless it is really eating up all your CPU cycles and memory.. In that case my approach below wouldn't help with that problem.

There are quite a few ways to approach this problem (an ASP.NET Long Running Process). I have always used a server-side background thread with client-side polling approach. What this means is that your application would function like this:

1) Client indicates they wish to start the long running process.

2) Server kicks off the process in a background thread, and returns a unique identifier to the client.

3) Client waits some period of time, and asks the server "Are we there yet?" (think of kids in the back seat of a long car ride) Passing with this question the identifier from step 2, so the server knows what job to check for completion.

4) Server replies "Not yet.." (go back to step 3) OR "Yes we are there.." (go to step 5).

5) Long running process is completed, most likely the client then requests some results from the server and displays them, or just indicates to the user that the job is finished.

Now -- how to code this? This is a resource I've used for quite a few years and it has served me well, I've made a few minor changes to the classes but overall the approach is solid:

http://www.devx.com/asp/Article/29617

Here is another:

http://www.allannienhuis.com/archives/2010/11/27/long-running-asp-net-processes-a-simple-example/

One thing to consider when you code this is if your application will ever be served in a LOAD BALANCED environment. If so you may need to configure "sticky sessions" or ensure that the unique job identifier key and its associated job are accessible from each web server. Enjoy!

mikey
  • 5,090
  • 3
  • 24
  • 27
0

This will only happen in the development web server, not in IIS in a production environment.

ASP.NET Development Server concurrent processing doesn't work

Community
  • 1
  • 1
Rob
  • 5,578
  • 3
  • 23
  • 28