2

I have a "blog" website developed using ASP.NET MVC 1. Recent version of MVC includes AsyncController feature. This actually requires some additional task in development. But how can I reuse my existing code without modifying my business layer.

Some part of the code looks like:

BlogPost post = new BlogPost();
post.GetPost(58345);

BlogComment comments = new BlogComment();
comments.GetComments(58345);

As per the current environment, I need to wait till the request completes two operations. Using AsyncController, I can do two operations simultaneously. But the classes BlogPost and BlogComment requires to be changed to support for asynchronous operations like adding EventHandlers to know whether the operation is completed and etc.

How can I do asynchronous operation without modifying existing business layer.

1
  • Max, Were you able to find a solution for this? I am in a similar boat and want to call my existing Business Logic Layer (BLL) methods Asynchronoulsy using AsyncController WITHOUT modifying or adding EventHandlers to my BLL. I would greatly appreciate your response/suggestion. You can see my question here: stackoverflow.com/questions/5760285/… Commented Apr 25, 2011 at 7:59

2 Answers 2

3

You could do this:

public class BlogController : AsyncController
{
    private readonly IBlogRepository _repository;
    public BlogController(IBlogRepository repository)
    {
        _repository = repository;
    }

    public void ShowAsync(int id)
    {
        AsyncManager.OutstandingOperations.Increment(2);
        new Thread(() =>
        {
            AsyncManager.Parameters["post"] = _repository.GetPost(id);
            AsyncManager.OutstandingOperations.Decrement();
        }).Start();
        new Thread(() =>
        {
            AsyncManager.Parameters["comments"] = _repository.GetComments(id);
            AsyncManager.OutstandingOperations.Decrement();
        }).Start();
    }

    public ActionResult ShowCompleted(Post post, IEnumerable<Comment> comments)
    {
        return View(new BlogViewModel
        {
            Post = post,
            Comments = comments,
        });
    }  
}

You should measure the performance of your application and decide whether introducing an async controller brings any value to the performance.

Sign up to request clarification or add additional context in comments.

5 Comments

Doing your processing on the Thread Pool really defeats the purpose of using async controllers. Requests are processed by the thread pool workers. By queing the work on the thread pool, you're returning a thread to the thread pool, only to use it again by the worker process. What's worse, you're now using two threads.
@Mystere Man, didn't you read the end of my post? I will put a comment in the code as well to make this more obvious as it is important. Thanks.
Yes, I did read the end of your post. I don't see how it has anything to do with my point, though. Yes, it's always a good idea to put long running threads on their own thread rather than use the thread pool. My point is that using Async, only to use the thread pool has no point, and shouldn't be done in the first place because it achieves no benefit.
@Mystere Man, agreed. I updated my post to no longer use threads from the thread pool as it could be misleading.
Thank you very much Darin Dimitrov
2

First, why do you think you need to do Async Controllers? Are you experiencing some performance problem that you thinkn Async will help you with? Why complicate your application with Async handling if you do not really need it?

Async is really designed to handle much more massive scaling, or when you need to do non-cpu bound operations in your controllers that might take a long time to execute.

Second, I think you are a bit confused about how Async controllers operate. You don't need to modify your business layer in most cases, you simply need to create an async "shim" to wrap your business layer. Async does not mean "multi-threaded". it will still do one thread per request, and you will still call your business logic single threaded (unless you write code to do things multi-threaded).

All Async controllers do is allow for better utilization of the Thread Pool. When you have threads that are not CPU bound, they can be returned to the thread pool while waiting for your request to be re-activated, thus allowing the thread pool to be better utlized, rather than using a thread doing nothing but waiting.

If you need to call multiple operations, you use the AsyncManager.OutstandingOperations property to control how many operaitons must complete to complete the request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.