Brian
Brian

Reputation: 25834

Should unnecessary Async Page directives be avoided?

I have noticed a few places on my website where I talk to third-party resources. I have seen several recommendations (e.g., MSDN Magazine: Asynchronous Pages in ASP.NET 2.0) that such code should run asynchronously, if only to avoid saturating the thread pool. Suppose I introduce several functions like this:

public static void LoadThirdPartyInfo(/*Arguments*/)
{
    Page page = (Page) HttpContext.Current.CurrentHandler;
    if(page.IsAsync)
    {
        page.AddOnPreRenderCompleteAsync( //...
    }
    else
    {
        //Oops...
    }
}

At this point, I could:
1. Remove (or keep) the else clause and try to be careful to only call it on Async pages, possibly adding some logging to detect when this is done incorrectly.
2. Same as #1, but use Async="True" on new pages.
3. Same as #1, but use Async="True" on all pages.

I am unsure if there are risks to introducing unnecessary Page Async directives. They don't seem to break anything.

Question: Is it OK to introduce spurious Async directives?

Note: I am using .Net 4.0, but am open to responses targeting .Net 4.5 .

Upvotes: 1

Views: 70

Answers (1)

Levi
Levi

Reputation: 32828

Simply adding Async=true to a Page without activating any of the async-specific code paths is fine. Doing this will incur a slight performance penalty. The penalty is normally insignificant, but it might be a good idea to perf test your site again after any change you make just to make sure nothing has regressed.

Upvotes: 2

Related Questions