当我尝试检索结果时,我遇到了任务阻塞的问题.
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)
在启动要在PostCreateProfile中进行最终调用的任务之后,该调用似乎中断了,我可以看到此请求再也没有做到这一点了.
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; }如果我将CreateProfile更改为异步void,则请求将到达其目的地,如下所示:
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?
推荐答案永远不要在异步/等待链上调用.Result .
任何调用CreateProfile(demographics)的代码都必须也是异步的,这样它才能做到
Whatever code that calls CreateProfile(demographics) needs to be async too so it can do
if (await CreateProfile(demographics)) { //dothing }此外,如果可以的话,您真的应该将.ConfigureAwait(false)放在逻辑上可能的地方.
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; }更多推荐
异步任务的结果被阻止
发布评论