简体   繁体   中英

Result of a async task is blocking

I have an issue with a task blocking when I try to retrieve it's result.

I have the following piece of code I want executed synchronously (which is why I'm looking for the result)

I would ignore the reason each call has to be made (legacy software that requires multiple calls through different layers)

the call seems to break down after it starts the task for the final call to be made in the PostCreateProfile, I can see this request never makes it any further than this.

if (CreateProfile(demographics).Result) // Task blocks here
{
    //dothing
}

private async Task<bool> CreateProfile(Demographics demographics)
{
    ProfileService profileService = new ProfileService();

    CreateProfileBindingModel createProfileBindingModel = this.CreateProfileModel(demographics);

    return await profileService.Create(createProfileBindingModel);
}

public async Task<bool> Create(CreateProfileBindingModel model)
{
    HttpResponseMessage response = await profileServiceRequest.PostCreateProfile(rootURL, model);

    return response.IsSuccessStatusCode;
}

public Task<HttpResponseMessage> PostCreateProfile(string url, CreateProfileBindingModel model)
{
    HttpContent contents = SerialiseModelData(model);
    var resultTask = client.PostAsync(url, contents);

    return resultTask;
}

The request will reach its destination if I was to change CreateProfile to an async void like so:

private async void CreateProfile(AppointmentController controller)
{
    ProfileService profileService = new ProfileService();

    CreateProfileBindingModel createProfileBindingModel = this.CreateProfileModel(controller);

    await profileService.Create(createProfileBindingModel);
}

But I can't return the bool I want to use from this. Can anyone point out what I am doing wrong?

You should never call .Result on a async/await chain .

Whatever code that calls CreateProfile(demographics) needs to be async too so it can do

if (await CreateProfile(demographics))
{
    //dothing
}

Also, if you can you really should put .ConfigureAwait(false) wherever it is logically possible.

if (await CreateProfile(demographics).ConfigureAwait(false)) // depending on what dothing is you may not want it here.
{
    //dothing
}

private async Task<bool> CreateProfile(Demographics demographics)
{
    ProfileService profileService = new ProfileService();

    CreateProfileBindingModel createProfileBindingModel = this.CreateProfileModel(demographics);

    return await profileService.Create(createProfileBindingModel).ConfigureAwait(false);
}

public async Task<bool> Create(CreateProfileBindingModel model)
{
    HttpResponseMessage response = await profileServiceRequest.PostCreateProfile(rootURL, model).ConfigureAwait(false);

    return response.IsSuccessStatusCode;
}

public Task<HttpResponseMessage> PostCreateProfile(string url, CreateProfileBindingModel model)
{
    HttpContent contents = SerialiseModelData(model);
    var resultTask = client.PostAsync(url, contents);

    return resultTask;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM