简体   繁体   中英

Tasks and Parallel Programming ASP.Net

Is it possible to wrap a call using the TPL Library in .Net?

I want to do something like this

Task t1 = Task.Factory.StartNew(() => { dynamic result = FacebookApp.Get("me/videos/uploaded", parameters); });

But I get an HttpContext error, I am assuming this is because the FacebooApp.Get method is using this internally.

How can I wrap calls to the FB API into tasks to run in parellel on the server?

I forgot to mention I am using the Facebook C# SDK hence FacebookApp.Get


EDITED

I have been watching some pluralsight videos on the TPL library and went with the pattern in the picture below. As you said there is the main thread coming in from the Create Controller itself, but I still needed to spawn 4 separate network tasks to do the network calls in parallel. I fix the HttpContext error by not having the function that was being called in 4th task to not rely on HttpContext at all.

The net result for me now is a more highly performance method call as promised with parallel programming.

If you call to a web service, and don't want to block the request thread, it's better to go with asynchronous pages (Web Forms) or asynchronous controllers (MVC). If you spin up a Task and need the value returned from the task, you will still be waiting for that operation to complete, which still blocks the request thread.

Note that since ASP.NET is multi-threaded by default, you shouldn't spin up multiple threads within a single web request.

UPDATE:

See this SO answer about multi-threading in web applications for more info.

The implementation you have in the screenshot above is going to result in wasting CPU resources, specifically thread pool threads which ASP.NET also uses, while waiting for network calls to complete. If you want to maximize your throughput you must use asynchronous I/O calls that don't result in blocking a CPU thread. In order to fully benefit from that then you would also need to use async controllers as @Steven mentioned.

However, the advice that you shouldn't utilize multiple threads in an ASP.NET request implementation is not really good advice because it's not as black and white as that. It's true that the ASP.NET requests themselves are obviously executing on multiple threads and so there's already a good degree of parallelism from the top, but if you're making calls out to other layers that involve I/O you want to relinquish the ASP.NET thread while those I/O operations are occurring so that ASP.NET can process more requests while those I/O operations are pending.

As always, it depends on the specifics of an implementation, but in your case since you're calling out to something like Facebook I can tell you you will absolutely see benefits by using async on those calls.

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