1

I am writing a ASP.NET MVC app and have a method calling out to a web service.

protected Task ExecuteRequest(HttpClient client, HttpRequestMessage restRequest)
{
    SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
    scheduler = TaskScheduler.FromCurrentSynchronizationContext();

    Task requestTask = client.SendAsync(restRequest).ContinueWith(antecendent =>
            {
                # Process response. Break point here. Doesn't seem to be running unless I spend time stepping through code in debug mode.
            }, scheduler);
    return requestTask;
}


/* ======================================================================
 * This stuff probably doesn't matter but just in case, I'm including it.
 * ====================================================================== */
protected HttpClient GetClient() {
    var clientHandler = new HttpClientHandler();
    if (clientHandler.SupportsAutomaticDecompression)
    {
        clientHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    }
    HttpClient client = new HttpClient(clientHandler);
    return client;
}

Then when I call ExecuteRequest: Task request = ExecuteRequest(GetClient(), myRequestMessage); The breakpoint in the ContinueWith doesn't fire. Instead, the MVC request that triggered this call finishes and the page finishes loading and no code is ever called in the ContinueWith antecedent. I've put loggs to see if the code is even hit but nothing I put in there seems to be run as none of the logs write to their file and none of the results of the logic in that code seems to have taken place.

My assumption is that the task, being called by an MVC request, dies with the main thread when the MVC response finishes. If I step through the code in debug mode after the task is started, sometimes the code will run and the break point will be hit so it seems like a race condition.

I've looked at This post but the solution they had was to wait for the task to finish. I don't want to wait for the service call to finish before returning the response to the user. If the task hasn't finished, I'm gonna have the user refresh later to see the results which should be still running in the background on a separate thread... but the problem is that the results never happen.

Why does it seem like the "# Process response" bit isn't being run?

Ian Kirkpatrick
  • 1,861
  • 14
  • 33
  • You should probably run the service call in a different thread. When `ExecuteRequest()` returns it ends execution of `client.SendAsync()` as well because you are not awaiting it. See https://stackoverflow.com/a/1018630/3608792 – Dan Wilson Jul 24 '18 at 15:34
  • I get a 'Start may not be called on a promise-style task.' error when I call Run. – Ian Kirkpatrick Jul 24 '18 at 15:48
  • The scope of the request is completing before the task can complete. As is the nature of async and asp.net mvc – Nkosi Jul 24 '18 at 16:07
  • I'm fairly new to Async programming. What do you mean by scope? To me, scope means a list of variables and functions that are accessible to a stack frame. Is that what you're talking about? or is it something different? – Ian Kirkpatrick Jul 24 '18 at 17:22

0 Answers0