Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:29:45+00:00 2026-06-16T05:29:45+00:00

I’ve encountered a pretty strange case when task execution is not continued after await

  • 0

I’ve encountered a pretty strange case when task execution is not continued after await in IIS (not sure if it’s related to IIS). I reproduced this issue using Azure Storage and following controller (full solution on github):

public class HomeController : Controller
{
    private static int _count;

    public ActionResult Index()
    {
        RunRequest(); //I don't want to wait on this task
        return View(_count);
    }

    public async Task RunRequest()
    {
        CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;
        var cloudTable = account.CreateCloudTableClient().GetTableReference("test");

        Interlocked.Increment(ref _count);
        await Task.Factory.FromAsync<bool>(cloudTable.BeginCreateIfNotExists, cloudTable.EndCreateIfNotExists, null);

        Trace.WriteLine("This part of task after await is never executed");
        Interlocked.Decrement(ref _count);
    }
}

I would expect the value of _count to be always 1 (when rendered in view), but if you hit F5 several time you’ll see that _count is incrementing after each refresh. That means that continuation is not called for some reason.

In fact I’ve lied a bit, I’ve noticed that continuation is called once, when Index is called for the first time. All further F5’s don’t decrement the counter.

If I change the method to be async:

    public async Task<ActionResult> Index()
    {
        await RunRequest(); //I don't want to wait on this task
        return View(_count);
    }

everything starts working as expected except that I don’t want to keep client waiting for my asynchronous operation to finish.

So my question is: I would like to understand why this happens, and what is the consistent way to run “fire and forget” work, preferably without spanning new threads.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:29:47+00:00Added an answer on June 16, 2026 at 5:29 am

    what is the consistent way to run “fire and forget” work

    ASP.NET was not designed for fire-and-forget work; it was designed to serve HTTP requests. When an HTTP response is generated (when your action returns), that request/response cycle is complete.

    Note that ASP.NET will feel free to take down your AppDomain any time that there are no active requests. This is normally done on shared hosts after an inactivity timeout, or when your AppDomain has had a certain number of garbage collections, or every 29 hours just for no reason at all.

    So you don’t really want “fire and forget” – you want to produce the response but not have ASP.NET forget about it. The simple solution of ConfigureAwait(false) will cause everyone to forget about it, which means that once in a blue moon your continuation could just get “lost”.

    I have a blog post that goes into more detail on this subject. In short, you want to record the work to be done in a persistent layer (like an Azure table) before your response is generated. That’s the ideal solution.

    If you aren’t going to do the ideal solution, then you’re going to live dangerously. There is code in my blog post that will register Tasks with the ASP.NET runtime, so that you can return a response early but notify ASP.NET that you’re not really done yet. This will prevent ASP.NET from taking down your site while you have outstanding work, but it will not protect you against more fundamental failures like a hard drive crash or someone tripping over the power cord of your server.

    The code in my blog post is duplicated below; it depends on the AsyncCountdownEvent in my AsyncEx library:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web.Hosting;
    using Nito.AsyncEx;
    
    /// <summary>
    /// A type that tracks background operations and notifies ASP.NET that they are still in progress.
    /// </summary>
    public sealed class BackgroundTaskManager : IRegisteredObject
    {
        /// <summary>
        /// A cancellation token that is set when ASP.NET is shutting down the app domain.
        /// </summary>
        private readonly CancellationTokenSource shutdown;
    
        /// <summary>
        /// A countdown event that is incremented each time a task is registered and decremented each time it completes. When it reaches zero, we are ready to shut down the app domain. 
        /// </summary>
        private readonly AsyncCountdownEvent count;
    
        /// <summary>
        /// A task that completes after <see cref="count"/> reaches zero and the object has been unregistered.
        /// </summary>
        private readonly Task done;
    
        private BackgroundTaskManager()
        {
            // Start the count at 1 and decrement it when ASP.NET notifies us we're shutting down.
            shutdown = new CancellationTokenSource();
            count = new AsyncCountdownEvent(1);
            shutdown.Token.Register(() => count.Signal(), useSynchronizationContext: false);
    
            // Register the object and unregister it when the count reaches zero.
            HostingEnvironment.RegisterObject(this);
            done = count.WaitAsync().ContinueWith(_ => HostingEnvironment.UnregisterObject(this), TaskContinuationOptions.ExecuteSynchronously);
        }
    
        void IRegisteredObject.Stop(bool immediate)
        {
            shutdown.Cancel();
            if (immediate)
                done.Wait();
        }
    
        /// <summary>
        /// Registers a task with the ASP.NET runtime.
        /// </summary>
        /// <param name="task">The task to register.</param>
        private void Register(Task task)
        {
            count.AddCount();
            task.ContinueWith(_ => count.Signal(), TaskContinuationOptions.ExecuteSynchronously);
        }
    
        /// <summary>
        /// The background task manager for this app domain.
        /// </summary>
        private static readonly BackgroundTaskManager instance = new BackgroundTaskManager();
    
        /// <summary>
        /// Gets a cancellation token that is set when ASP.NET is shutting down the app domain.
        /// </summary>
        public static CancellationToken Shutdown { get { return instance.shutdown.Token; } }
    
        /// <summary>
        /// Executes an <c>async</c> background operation, registering it with ASP.NET.
        /// </summary>
        /// <param name="operation">The background operation.</param>
        public static void Run(Func<Task> operation)
        {
            instance.Register(Task.Run(operation));
        }
    
        /// <summary>
        /// Executes a background operation, registering it with ASP.NET.
        /// </summary>
        /// <param name="operation">The background operation.</param>
        public static void Run(Action operation)
        {
            instance.Register(Task.Run(operation));
        }
    }
    

    It can be used like this for async or synchronous code:

    BackgroundTaskManager.Run(() =>
    {
        // Synchronous example
        Thread.Sleep(20000);
    });
    BackgroundTaskManager.Run(async () =>
    {
        // Asynchronous example
        await Task.Delay(20000);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.