简体   繁体   中英

Async web request in asp.net httphandler

I have the following scenario and would like to get some clarification if possible.

I have a javascript function that makes an ajax call to a handler(.ashx) file. The javascript function does not need a return value. The handler will post some data and be done. The handler file makes an ASYNC web request to Bitly, Facebook, and Twitter (not in that order). My idea solution would be for the handler to make an ASYNC call to Bitly (bit.ly), get the result then post to Twitter and Facebook at the same time on different threads as they are independent of each other.

What I am questioning about is the WaitHandle. Should I be using this since the handler is running outside of the user actions or will the callback methods be sufficient? Will the handler file still be listening for the callback if there is no call to hold the current thread until it returns? If I do need to make the thread wait for the callback am I still getting the benefit of making ASYNC web request seeing that the original thread is still waiting or hung up?

I kind of understand how this is suppose to work but can't quite put it all together.

NOTE: The handle is not used to intercept a web request. I am using the handler to process an ajax post. I am calling the handler directly.

I wouldn't use an Async Handler for this. Since you're not returning a result, using an Async Handler for this purpose is wasteful. You use an Aysnc handler in situations where the task you're doing is relatively long running and the result of this task has to be sent back as a response to the waiting thread and eventually the browser.

In your case you should use a secondary process or MSMQ to get the job done. A simple solution would be to to post the data into a table and have another process process these records. MSMQ works really well for these situations since queuing a message takes a fraction of a second and the process that processes these messages can take it's own sweet time without any ill effects on your ASP.NET web application/site. You'll get a lot of scalability of your website like this.

To answer your question about WaitHandle (if you're hell bent on going this route), no you shouldn't use the waithandle, you should use it as a true asynchronous non-blocking handler. WaitHandle will block.

There is no guarantee that the handler will be alive during the entire process. Your worker process can be reset during that time. All the more reason to not do this as an Async Handler but rather as an "out of band" async job. By out of band I mean not in the same process as your ASP.NET application.

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