Result of a async task is blocking

亡梦爱人 提交于 2019-12-02 09:38:44

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