public class PageController : RenderMvcController
{
private ContentService _contentService;
public PageController()
{
_contentService = new ContentService();
}
public ActionResult Index()
{
var page = (Page)Umbraco.AssignedContentItem;
var Model = new PageModel(Page)
{
Configuration = _contentService.Client.GetContentAsync().Result
};
return View("~/Views/Page.cshtml", Model);
}
}
I know it's not real async code, but this is solution for us.
If you want to call an async method from a non-async location, you can not directly access the Result. That will occur in deadlocks and will not work properly.
You should use something like:
var result = Task.Run(async() => await _contentService.Client.GetTvContentAsync().ConfigureAwait(false)).Result;
Hmmm its simply not working, must be because of inheriting Umbraco.Web.Mvc.SurfaceController i think.
I will come back if i find out more on this problem, im not sure what to do next thoug...
Hi everyone. I know this post is a couple years old, but why can't the ActionResult simply be made a Task
public class EventsPageController : RenderMvcController
{
public ITessituraApiFactory TessituraApiFactory { get; set; }
private readonly IUmbracoContextFactory _umbracoContext;
public EventsPageController(IUmbracoContextFactory umbracoContext)
{
TessituraApiFactory = new TessituraApiFactory();
_umbracoContext = umbracoContext;
}
public async Task<ActionResult> Index(ContentModel contentModel)
{
var service = new ProductionDetailService(TessituraApiFactory);
var vm = new EventDetailViewModel(contentModel.Content);
try
{
var eventsPage = contentModel.Content as EventsPage;
vm.Event = eventsPage;
vm.Performances = await service.GetProductionPerformancesViewModel(eventsPage.ProductionSeasonId);
}
catch (Exception ex)
{
vm.Error = "Error retrieving event information. Please try again later.";
}
return View(vm);
}
}
In the above, I'm calling a pure asynchronous method from my service class (makes a web request to a REST API).
I realize this is a RenderMvcController and not a Surface controller, but I've done similar things in a Surface controller, where the action result is an awaitable Task
Calling async method from controller
Hi, Im struggling trying to call an async method from my Umbraco controller. Can anyone give an example what to do to get it working?
Hi Amigo,
What type of controller are you using?
We have this example it our project:
I know it's not real async code, but this is solution for us.
Hope it will help.
Thanks,
Alex
hmm i geuess its because im using Umbraco.Web.Mvc.SurfaceController. its not working then...
Hi Amigo,
Can you share your code?
Hi, Alex, sure Its something like this:`
{
}
Hi Amigo,
If you want to call an async method from a non-async location, you can not directly access the Result. That will occur in deadlocks and will not work properly.
You should use something like:
var result = Task.Run(async() => await _contentService.Client.GetTvContentAsync().ConfigureAwait(false)).Result;
That should do the trick.
Thanks, Richard, this is the same what I wrote. Main idea is to use .Result, what isn't not async exactly.
Thanks,
Alex
Here is how I handle async methods from a controller.
Hi Guys,
Hmmm its simply not working, must be because of inheriting Umbraco.Web.Mvc.SurfaceController i think. I will come back if i find out more on this problem, im not sure what to do next thoug...
Hi Amigo,
Waiting for your response!
Hi everyone. I know this post is a couple years old, but why can't the ActionResult simply be made a Task
In the above, I'm calling a pure asynchronous method from my service class (makes a web request to a REST API).
I realize this is a RenderMvcController and not a Surface controller, but I've done similar things in a Surface controller, where the action result is an awaitable Task
https://docs.microsoft.com/en-us/aspnet/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4
Stephen Cleary has great articles about this as well
https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
Anyway, curious to know what you all think about the solution!
is working on a reply...