Reputation: 230
This is probably a more of a finesse question but I have the following method inside a ViewComponent class
public async Task<IViewComponentResult> InvokeAsync()
{
return View();
}
but the name InvokeAsync is underlined and gives the following warning
This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread
but if I try removing the async from the method then return View() is underlined with red and outputs the following error
'Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult' to 'System.Threading.Tasks.Task' MVCStateManagement
So my question is what approach should I take? Let the async there indiferently of the warning, or is there a workaround / fix for this warning? Does it have that much of an impact on my project?
Thanks!
Upvotes: 3
Views: 1792
Reputation: 169360
It's unclear why the method was defined as an async
method that returns a Task<IViewComponentResult>
in the first place.
Since the method seems to be truly synchronous and simply returns a view, you should probably define it like this:
public IViewComponentResult Invoke()
{
return View();
}
A synchronous method doesn't magically become asynchronous just because you add the async
keyword to it.
If you are implementing an interface and cannot change the signature of the method, you could use the Task.FromResult
method to return an already completed task (you should still remove the async
keyword):
public Task<IViewComponentResult> InvokeAsync()
{
return Task.FromResult<IViewComponentResult>(View());
}
Upvotes: 4
Reputation: 4883
You have to remove the async flag and the Task<>
. Just return a IViewComponentResult
.
You will tipically return a object wrapped into Task<>
when you do async work. If you don´t it does not make any sense.
From MSDN:
The Task class represents a single operation that returns a value and that usually executes asynchronously. Task objects are one of the central components of the task-based asynchronous pattern first introduced in the .NET Framework 4. Because the work performed by a Task object typically executes asynchronously on a thread pool thread rather than synchronously on the main application thread, you can use the Status property, as well as the IsCanceled, IsCompleted, and IsFaulted properties, to determine the state of a task. Most commonly, a lambda expression is used to specify the work that the task is to perform.
https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1?view=netframework-4.7.2
EDIT:
You can also try returning:
return Task.FromResult<IViewComponentResult>(View());
Upvotes: 1
Reputation: 2174
The method runs on a Task of ViewcomponentResult. You can call it without async by using this:
public Task<IViewComponentResult> InvokeAsync()
{
return Task.FromResult<IViewComponentResult>(View());
}
Your warning will no longer show and your code will run. You could also leave it. Either will have no impact on the performance of your project.
Upvotes: 3