I have been getting my feet wet using async/await and most of what I have been reading states "Don't Block on Async Code" with the exception of the main function in a console application. I find myself unable to follow this simple rule :(
My application depends heavily on a WebAPI layer. The client I am using in the MVC apllication is using the async methods on System.Net.Http.HttpClient
to call API methods. Currently I am handling it two ways. In some places we are getting the result immediately after calling the client:
//WebAPI Client
public object GetSomething(int id)
{
var task = _client_GetSomethingFromApiAsync(id);
var result = task.Result;
return result;
}
In others using async all the way up to the controller. I am using SiteFinity and cannot use async controller actions so I end up with something like this:
//WebAPI Client
public Task<object> GetSomethingAsync(int id)
{
return _client_GetSomethingFromApiAsync(id);
}
//Domain Service
public Task<object> GetSomethingDomainServiceAsync(int id)
{
return _service.GetSomethingAsync(id);
}
//Controller
public JsonResult GetSomethingControllerAction(int id)
{
var result = _domainService.GetSomethingDomainServiceAsync(viewModel.id).Result;
return Json(new { Success = true, Payload = result });
}
Based on my research both of these solutions are not ideal although I have not had any issues with deadlocks and everything seems to be working. Can someone please help me understand the best pattern I should follow and explain why?
Thank You!