Search code examples
c#asp.netasynchronousasp.net-boilerplate

Async Method of DeleteAsync not working


I'm new to asynchronous programming in c#.

So here is my code:

    private async Task testDeleteBank(int id)
    {
        await _msBankRepo.DeleteAsync(id);
        var checkBank = (from A in _msBankRepo.GetAll()
                         where A.Id == id
                         select A).Count();
        if(checkBank > 0)
        {
            Console.Write(checkBank);
        }
    }

    public void testAsync(GetAllBankListDto input)
    {
        testDeleteBank(input.bankID);
        UpdateMsBank(input);
    }

When I run testAsync method, it does update a record in my table. But why it's not deleting my record after DeleteAsync method?


Solution

  • You should await testDeleteBank(input.bankID) in:

    public async Task testAsync(GetAllBankListDto input)
    {
        await testDeleteBank(input.bankID);
        // UpdateMsBank(input);
    }
    

    If you need test to be synchronous, use AsyncHelper in:

    public void test(GetAllBankListDto input)
    {
        AsyncHelper.RunSync(() => testDeleteBank(input.bankID));
        // UpdateMsBank(input);
    }