cslaforum icon indicating copy to clipboard operation
cslaforum copied to clipboard

Marking [Fetch] Method Async

Open animjn opened this issue 5 years ago • 5 comments

Question Hi. I wanted to check with you, if you see any possible issues in marking the [Fetch] method Async similar to the below line?

[Fetch] private async void Fetch(int id)

Version and Platform CSLA version: 5.1.0 OS: Windows Platform: WinForms, WPF, ASP.NET Core

animjn avatar Mar 16 '20 10:03 animjn

As I understand it, no reason to make the Fetch method async. The DataPortal.FetchAsync will handle this correctly.

JacoJordaan avatar Mar 16 '20 10:03 JacoJordaan

It needs to be

[Fetch]
private async Task Fetch() 

rockfordlhotka avatar Mar 16 '20 14:03 rockfordlhotka

Never have async methods return void unless it's an event handler.

JasonBock avatar Mar 16 '20 15:03 JasonBock

True - and in this particular case, the data portal specifically looks for async Task and won't invoke the method if it is async void.

rockfordlhotka avatar Mar 16 '20 15:03 rockfordlhotka

Also, fwiw @JacoJordaan, you are right - the client-side data portal should be used async (FetchAsync, etc.) as a general rule.

And on the server-side you can also have async methods, which is often necessary because you'll be using Dapper, EF, or other APIs that require the use of the await keyword - so you need the server-side method to be async Task.

rockfordlhotka avatar Mar 16 '20 15:03 rockfordlhotka