Result of a async task is blocking

早过忘川 提交于 2019-12-04 06:04:00

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/36895530/result-of-a-async-task-is-blocking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!