简体   繁体   中英

Fire and Forget (Asynch) ASP.NET Method Call

We have a service to update customer information to server. One service call takes around few seconds, which is normal.

Now we have a new page where at one instance around 35-50 Costumers information can be updated. Changing service interface to accept all customers together is out of question at this point.

I need to call a method (say "ProcessCustomerInfo"), which will loop through customers information and call web service 35-50 times. Calling service asynchronously is not of much use.

I need to call the method "ProcessCustomerInfo" asynchronously. I am trying to use RegisterAsyncTask for this. There are various examples available on web, but the problem is after initiating this call if I move away from this page, the processing stops.

Is it possible to implement Fire and Forget method call so that user can move away (Redirect to another page) from the page without stopping method processing?

Details on: http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

Basically you can create a delegate which points to the method you want to run asynchronously and then kick it off with BeginInvoke.

// Declare the delegate - name it whatever you would like
public delegate void ProcessCustomerInfoDelegate();

// Instantiate the delegate and kick it off with BeginInvoke
ProcessCustomerInfoDelegate d = new ProcessCustomerInfoDelegate(ProcessCustomerInfo); 
simpleDelegate.BeginInvoke(null, null);

// The method which will run Asynchronously
void ProcessCustomerInfo()
{
   // this is where you can call your webservice 50 times
}

This was something I whipped just to do that...


    public class DoAsAsync
    {
        private Action action;
        private bool ended;

        public DoAsAsync(Action action)
        {
            this.action = action;
        }

        public void Execute()
        {
            action.BeginInvoke(new AsyncCallback(End), null);
        }

        private void End(IAsyncResult result)
        {
            if (ended)
                return;

            try
            {
                ((Action)((AsyncResult)result).AsyncDelegate).EndInvoke(result);
            }
            catch
            {
                /* do something */
            }
            finally
            {
                ended = true;
            }
        }
    }

And then

    new DoAsAsync(ProcessCustomerInfo).Execute();

Also need to set the Async property in the Page directive <%@ Page Async="true" %>

I'm not sure exactly how reliable this is, however it did work for what I needed it for. Wrote this maybe a year ago.

I believe the issue is the fact is your web service is expecting a client to return the response to, that the service call itself is not a one way communication.

If you're using WCF for your webservices look at http://moustafa-arafa.blogspot.com/2007/08/oneway-operation-in-wcf.html for making a one way service call.

My two cents: IMO whoever put the construct on you that you're not able to alter the service interface to add a new service method is the one making unreasonable demands. Even if your service is a publicly consumed API adding a new service method shouldn't impact any existing consumers.

当然可以

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM