0

I am having a Repository method GetSubContracts() that is typically invoked as await and it works fine. I am trying to use the same in a non task/async operation such as a Button click Handler event. I would like to do on of the two options.

  1. Call the async method without async as explained in the below article https://visualstudiomagazine.com/Blogs/Tool-Tracker/2019/10/calling-methods-async.aspx

  2. Cast the Task<IEnumerable> to List

This is because I don’t get .ToList() option when using GetSubContacts in an non Task/await context.

The result. Is also not giving me a Cast or TypeOf options.

I tried various options including what seems to be similar topic here

Casting IEnumerable<T> to List<T>

On a general note, should I be adding to the above post or ask a new question here as I am doing now?

Code below.

 public List<SubContract> SubContracts; 
public List<SubContract> SubContractsOfSelectedSites = new List<SubContract>();


protected override async Task OnInitializedAsync()
{
    Sites = (await SitesService.GetSites()).ToList();
    SubContracts = (await SubContractsService.GetSubContracts()).ToList();
}



private void  GenerateContractListHandler(MouseEventArgs args)
{
    SubContractsTabDisabled = false;
    //Get the result as Task<IEnumerable<SubContract>>
    var result = (SubContractsService.GetSubContracts());
    SubContractsOfSelectedSites =result.
Abraham
  • 11
  • 3
  • 1
    Does something prevent you making GenerateContractListHandler async? – Ralf Jun 21 '22 at 13:10
  • 2
    @Abraham An `Event Handler` is an exception to the rule to not use `async void`. You can safely make the method `async` and `await` your call to the database. [should-i-avoid-async-void-event-handlers](https://stackoverflow.com/questions/19415646/should-i-avoid-async-void-event-handlers) – Ryan Wilson Jun 21 '22 at 13:11
  • 2
    `.Result` is a really bad idea, as it will often cause a deadlock – Charlieface Jun 21 '22 at 13:13
  • Thanks all. The proposed solution worked. – Abraham Jun 21 '22 at 14:55
  • For my understanding, is it possible to cast IEnumerable to List as the second option that I was trying? – Abraham Jun 21 '22 at 14:57

1 Answers1

-1

You can call a async method in a non async function by simply assigning it to a variable then adding .wait();

var test = GetSubContracts()
test.Wait();

Hope this helps :)

You also might use after with its various options.

test.Result
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 14:55
  • Is it really required to wait specially if it is a long running query? for now, the below worked for me. private async void GenerateContractListHandler(MouseEventArgs args) { SubContractsTabDisabled = false; SubContractsOfSelectedSites = (await SubContractsService.GetSubContracts()).ToList(); – Abraham Jun 21 '22 at 15:01
  • I personally have had issues trying to call a async function in a non async function without using an wait() but I could be wrong. – MystifiedSky Jun 21 '22 at 16:03
  • It was downvoted because the advice to use `.Wait()` and `.Result` in lieu of `await` is a bad idea 99.9% of the time, and furthermore, unnecessary to solve the OP's problem. – Kirk Woll Jun 22 '22 at 22:23