Task.ContinueWith execution order

asp.net-web-api, c#, task

Solution

If you're using an asynchronous operation, the best approach would be to make your operation asynchronous as well, otherwise you'll lose on the advantages of the async call you're making. Try rewriting your method as follows:

public Task<string> UploadFile()
{
    if (Request.Content.IsMimeMultipartContent())
    {
        //Save file
        MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/Files"));
        Task<IEnumerable<HttpContent>> task = Request.Content.ReadAsMultipartAsync(provider);

        return task.ContinueWith<string>(contents =>
        {
            return provider.BodyPartFileNames.First().Value;
        }, TaskScheduler.FromCurrentSynchronizationContext()); 
    }
    else
    {
        // For returning non-async stuff, use a TaskCompletionSource to avoid thread switches
        TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
        tcs.SetResult("Invalid.");
        return tcs.Task;
    }
}

Problem

Apparently, I'm not understanding how to use the ContinueWith method. My goal is to execute a task, and when complete, return a message. Here's my code: ``` public string UploadFile() { if (Request.Content.IsMimeMultipartContent()) { //Save file MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/Files")); Task<IEnumerable<HttpContent>> task = Request.Content.ReadAsMultipartAsync(provider); string filename = "Not set"; task.ContinueWith(o => { //File name filename = provider.BodyPartFileNames.First().Value; }, TaskScheduler.FromCurrentSynchronizationContext()); return filename; } else { return "Invalid."; } } ``` The variable "filename" always returns "Not set". It seems the code within the ContinueWith method is never called. (It does get called if I debug through it line by line in VS.) This method is being called in my ASP.NET Web API controller / Ajax POST. What am I doing wrong here?

Original source

Related problems