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;
}
Related
I have two methods which return independent data. I assume it's a good idea to run them in parallel.
Without any modifications code look's like:
private async Task<Entity> MethodAsync()
{
...
var model1 = await client.Method1(Id1, cancellationToken);
var model2 = await client.Method2(Id2, cancellationToken);
...
}
Those methods return data as I expecting. Now when I change code all methods return "null". When I inspect Task object in visual studio there are properties Id = 2356, Status = RanToCompletion, Method = "{null}", Result = "".
private async Task<Entity> MethodAsync()
{
var model1Task = client.Method1(Id1, cancellationToken);
var model2Task = client.Method2(Id2, cancellationToken);
var task = Task.WhenAll(new Task[] { model1Task ,model2Task });
await task; //doesn't work
//task.Wait(); //doesn't work
//await Task.WhenAll(new Task[] { model1Task , model2Task }); //doesn't work
//Task.WhenAll(new Task[] { model1Task, model2Task}); //doesn't work
}
Code of client methods almost the same:
public async Task<Model> Method1(Guid Id, CancellationToken cancellationToken)
{
HttpResponseMessage responseMessage = await client.GetAsync($"customEndPoint");
ResponseMessageSingle<Model> response = JsonSerializer.Deserialize<ResponseMessageSingle<Model>>(
await responseMessage.Content.ReadAsStringAsync(cancellationToken),
new JsonSerializerOptions(JsonSerializerDefaults.Web));
return response.result;
}
private class ResponseMessageSingle<T>
{
public bool success { get; set; }
public string message { get; set; }
public T result { get; set; }
}
Also there is AuthorizeInterceptor(DelegatingHandler):
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
some logic...
request.SetBearerToken(TokenResponse.AccessToken);
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
return await Task.FromResult(response);
}
Task.WhenAll does not return a result in your case, because you create an array of Task, which has no result. And since your tasks return different results, you cannot get that in an array nicely.
You should change your code like this to get the results of your methods:
private async Task<Entity> MethodAsync()
{
var model1Task = client.Method1(Id1, cancellationToken);
var model2Task = client.Method2(Id2, cancellationToken);
var task = Task.WhenAll(new Task[] { model1Task ,model2Task });
await task;
var result1 = await model1Task;
var result2 = await model2Task;
...
}
Since both methods will be completed, awaiting them will just immediately get you the results.
I am setting up a new server, and I want to expose Async API, this API will call a function and this function will call another function and so on, the last function in the calling tree will call an external service and use Async/Await code pattern. what is the right way to implement such API?
Shall I add Async/Await in all functions or just adding them in the last function?
E.g.
[ResponseType(typeof(AnyTypeResponse))]
[HttpPost]
public async Task<IHttpActionResult> MyAPI()
{
var res = await MyFuncTree1();
return Ok(res);
}
public async Task<AnyTypeResponse> MyFuncTree1()
{
var res = await MyFuncTree2();
return res;
}
public async Task<AnyTypeResponse> MyFuncTree2()
{
var res = await MyFuncTree3();
return res;
}
public async Task<AnyTypeResponse> MyFuncTree3()
{
var res = await CallExternalService();
return res;
}
Only the top function needs to be marked async in your example and await the result of MyFuncTree1. The others can just return the result task of the function they are calling (as the result is not used inside the function).
[ResponseType(typeof(AnyTypeResponse))]
[HttpPost]
public async Task<IHttpActionResult> MyAPI()
{
var res = await MyFuncTree1();
return Ok(res);
}
public Task<AnyTypeResponse> MyFuncTree1()
{
return MyFuncTree2();
}
public Task<AnyTypeResponse> MyFuncTree2()
{
return MyFuncTree3();
}
public Task<AnyTypeResponse> MyFuncTree3()
{
return CallExternalService();
}
Please help me to understand why this code cause a deadlock?
I have an asp.net web api application and I tried to make some controller method asynchronous.
[HttpPost]
[Authentication]
public async Task<SomeDTO> PostSomething([FromBody] SomeDTO someDTO)
{
return await _service.DoSomething(someDTO);
}
this is how looks the called service method:
public async Task<SomeDTO> DoSomething(SomeDTO someDTO)
{
...
var someTask = Task.Run(() =>
{
var entity = new SomeEntity(someDTO);
return _repository.Create(entity);
});
...
var result = await someTask;
...
}
And there is some globalhandler, that prints a response to a console.
public class AppGlobalHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var resp = base.SendAsync(request, cancellationToken);
Debug.WriteLine($"Response:{request.RequestUri}{Environment.NewLine}{resp?.ConfigureAwait(false).GetAwaiter().GetResult()?.Content?.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult()}");
return resp;
}
}
Looks like ConfigureAwait(false).GetAwaiter().GetResult()
blocks the caller thread, but I supposed that ConfigureAwait(false) should avoid this, isn't it?
ConfigureAwait(false) would not help you here because it must be all the way down in the call stack (see more here) not at place where you wait synchronously, i.e. it depends rather on the implementation of base.SendAsync. If it acquired a lock on current thread it's too late to do something about it. It is also not recommended in ASP.net pipeline to continue responding on other thread after all (see discussion here and post here).
Finally it is always a highly risky idea to wait synchronously in async context.
If you need to read content, why not doing it like that:
public class AppGlobalHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var resp = await base.SendAsync(request, cancellationToken);
var content = resp?.Content != null
? (await resp.Content.ReadAsStringAsync())
: string.Empty;
Debug.WriteLine($"Response:{request.RequestUri}{Environment.NewLine}{content}");
return resp;
}
}
I think you overlook async keyword in Task.Run() method.
public async Task<SomeDTO> DoSomething(SomeDTO someDTO)
{
var someTask = Task.Run( async () => //simply add this for async run
{
var entity = new SomeEntity(someDTO);
return _repository.Create(entity);
});
var result = await someTask;
}
I am learning about async and await. I have an async method for dialog message and I am doing await to another method.
My code=>
private async Task CalculateProcess()
{
dialogCoordinator = DialogCoordinator.Instance;
// Show...
ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEADER", "MESSAGE");
controller.SetIndeterminate();
// Do your work...
await testing();//I want to await testing method but i cant await testing because testing method no return task
// Close...
await controller.CloseAsync();
}
private void testing()
{
for(int i=0;i<=1000000;i++)
{
}//Looping is example
}
I want to await to testing method but if I want to use await I need to return task from testing.How can I await testing method if I don't have any other returning task?
If you really want to await that method this is the way to go:
private async Task CalculateProcess()
{
dialogCoordinator = DialogCoordinator.Instance;
// Show...
ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEADER", "MESSAGE");
controller.SetIndeterminate();
// Do your work...
await testing();
// Close...
await controller.CloseAsync();
}
private Task testing()
{
for(int i=0;i<=1000000;i++)
{
}//Looping is example
return Task.FromResult(0);
}
I have an async Task unit test (MVC, c#, .NET 4.5.2). It does an await on a aysnc Task<ActionResult> method, which in turn has an await call on a async method.
The test, and others like it, will pass if I select them and choose Debug Selected Tests from the right-click menu in Visual Studio 2017.
The problem is when I select Run Selected Tests or Run All. It is then that many of the tests will fail if they follow the condition mentioned at the beginning. Any test that only returns a RedirectToRouteResult without having gone the aforementioned drill-down will pass.
[TestMethod]
public async Task TestPartsController_GetPartInfo_ReturnsInfo()
{
//arrange
PartController pc = new PartController();
//act
var result = await pc.GetPartInfo("PC123456");
//assert
Assert.IsIntanceOfType(result, typeof(ViewResult));
Assert.AreEqual("Form", ((ViewResult)result).ViewName);
Assert.AreEqual("PC123456", result.Model.PartNum.ToUpper());
}
public async Task<ActionResult> GetPartInfo(string partNum)
{
if (string.IsNullOrEmpty(partNum)
{
return RedirectToAction("Index")
}
var response = await ServiceClient.GetJsonAsync("/part/partinfo", "?partNum=" + partNum;
response.EnsureSuccessStatusCode();
results = await response.Content.ReadAsAsync<Dto.PartNumInfo>();
...
return View("Form", model);
}
public async Task<HttpResponseMessage> GetAsync(Controllers controller, string criteria)
{
HttpClient client;
string service = GetService(controller, out client);
var response = await client.GetAsync(service + criteria);
return response;
}
Solution
Use async/await all the way through as well as using statements and IDisposable.
public async Task<HttpResponseMessage> GetJsonAsync<T>(Controllers controller, T data)
{
HttpResponseMessage response;
using (var service = new MyService())
{
HttpClient http;
string serviceLoc = service.GetServiceClient(controller, out http);
response = await http.GetAsync(serviceLoc, data);
}
return response;
}
Solution Use async/await all the way through as well as using statements and IDisposable.
public async Task<HttpResponseMessage> GetJsonAsync<T>(Controllers controller, T data)
{
HttpResponseMessage response;
using (var service = new MyService())
{
HttpClient http;
string serviceLoc = service.GetServiceClient(controller, out http);
response = await http.GetAsync(serviceLoc, data);
}
return response;
}