Alex Rivera | Logout

Long running operations (threads) in a web (asp.net) environment

Asked 2010-05-11T07:07:52.930
10

I have an asp.net (mvc) web site. As the part of the functions I will have to support some long running operations, for example:

Initiated from user: User can upload (xml) file to the server. On the server I need to extract file, do some manipulation (insert into the db) etc... This can take from one minute to ten minutes (or even more - depends on file size). Of course I don't want to block the request when the import is running , but I want to redirect user to some progress page where he will have a chance to watch the status, errors or even cancel the import.

This operation will not be frequently used, but it may happen that two users at the same time will try to import the data. It would be nice to run the imports in parallel. At the beginning I was thinking to create a new thread in the iis (controller action) and run the import in a new thread. But I am not sure if this is a good idea (to create working threads on a web server). Should I use windows services or any other approach?

Initiated from system: - I will have to periodically update lucene index with the new data. - I will have to send mass emails (in the future).

Should I implement this as a job in the site and run the job via Quartz.net or should I also create a windows service or something?

What are the best practices when it comes to running site "jobs"?

Thanks!

Edit
Report

1 Answer

1

I've successfully implemented a scenario similar to this using jQuery. basically, i use the beforeSend: function to display the 'please wait' type page. here's the basic code in play (altso i don't, you could also use the AsyncController base class to make the action async):

<script type="text/javascript">
    $(document).ready(function() {
        $('#create').bind('click', function() {
            saveFundProperty();
        });
    });

    // main functions
    function saveFundProperty() {
        var url = '<%= Url.Action("Create", "FundProperty") %>';
        var params = { fundId: $("#FundID").val(), propertyId: $("#PropertyID").val() };
        SendAjax(url, params, beforeQuery, saveFundPropertyResponse);
    }

    function beforeQuery() {
        var url = '<%= Url.Action("Wait", "FundProperty") %>';
        $("#statusMsg").load(url);
    }

    function saveFundPropertyResponse(data) {
        if (data.length != 0) {
            if (data.indexOf("ERROR:") >= 0) {
                $("#statusMsg").html(data).css('backgroundColor','#eeaa00');
            }
            else {
                $("#statusMsg").html(data);
            }
        }
    }
</script>

hope this helps.

the SendAjax method is purely a wrapper function to make things a little more consistant. here it is in full:

<script type="text/javascript">
function SendAjax(urlMethod, jsonData, beforeSendFunction, returnFunction, dataType, contentType) {
    $.ajaxSetup({ cache: false });
    dataType = dataType || "text"; // default return type
    contentType = contentType || "application/x-www-form-urlencoded"; // default input type
    $.ajax({
        type: "POST",
        url: urlMethod,
        data: jsonData,
        dataType: dataType,
        contentType: contentType,
        beforeSend: function() {
            if(beforeSendFunction!==null)
                bef
answered 2010-05-11T08:26:56.950

Your Answer