2016-04-27 5 views
0

Ich habe ein Problem mit einer Aufgabe blockiert, wenn ich versuche, das Ergebnis zu erhalten.Ergebnis einer asynchronen Aufgabe ist Blockieren

Ich habe das folgende Stück Code, den ich synchron ausgeführt werden soll (weshalb ich für das Ergebnis der Suche)

ich den Grund jeder Anruf gemacht werden muss ignorieren würde (Legacy-Software, die durch mehrere Anrufe erfordert verschiedene Schichten)

der Anruf scheint nach dem Start der Aufgabe für den letzten Aufruf im PostCreateProfile zu brechen, kann ich sehen, dass diese Anfrage nie weiter geht.

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

Der Antrag wird sein Ziel erreichen, wenn ich Create auf ein asynchrones Leere wie so war zu ändern:

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

    CreateProfileBindingModel createProfileBindingModel = this.CreateProfileModel(controller); 

    await profileService.Create(createProfileBindingModel); 
} 

Aber ich zurückkehren kann nicht die Bool ich von dieser verwendet werden soll. Kann jemand darauf hinweisen, was ich falsch mache?

+0

'private async void CreateProfile' ist schlecht, versuche' private async Task', du kannst es dann immer noch 'erwarten'. Mit 'async void' feuern Sie die Aufgabe einfach ab und kehren vor ihrer Fertigstellung zurück. –

Antwort

1

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

Unabhängig von Code, der CreateProfile(demographics) Bedürfnisse nennt async sein, damit es

tun können
if (await CreateProfile(demographics)) 
{ 
    //dothing 
} 

Auch wenn Sie können Sie wirklich .ConfigureAwait(false) setzen sollten, wo immer es logisch möglich ist.

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