Open Closed

Parallel asynchronous calls in AppService #902

doubledp created


Could you please help me with the following:

I am trying to execute async calls is parallel and only use execute subsequent code after all the async tasks have been completed.

Here is the code:

public async Task<LabourSalesHighlightsDto> GetLabourSalesHighlights(LabourSalesAggregateInput input)
            LabourSalesHighlightsDto labourSalesHighlights = new LabourSalesHighlightsDto();

            var labourSalesOperatorAggregateTask = GetLabourSalesAggregation_ByCompanyNumber_ByOperatorName(input);
            var labourSalesInvoicingAggregateTask = GetLabourSalesAggregation_ByCompanyNumber_ByInvoicing(input);
            var labourSalesCreditingAggregateTask = GetLabourSalesAggregation_ByCompanyNumber_ByCredits(input);

            await Task.WhenAll(labourSalesOperatorAggregateTask, labourSalesInvoicingAggregateTask, labourSalesCreditingAggregateTask);

            var labourSalesOperatorAggregate = labourSalesOperatorAggregateTask.Result;
            var labourSalesInvoicingAggregate = labourSalesInvoicingAggregateTask.Result;
            var labourSalesCreditingAggregate = labourSalesCreditingAggregateTask.Result;

            labourSalesHighlights.SalesTopTotal = labourSalesOperatorAggregate.Items.Select(lsa => lsa.Value).FirstOrDefault();
            labourSalesHighlights.SalesTopName = labourSalesOperatorAggregate.Items.Select(lsa => lsa.Label).FirstOrDefault();
            labourSalesHighlights.SalesTopDescription = labourSalesOperatorAggregate.Items.Select(lsa => lsa.CompanyName).FirstOrDefault();

            labourSalesHighlights.InvoicingTotal = labourSalesInvoicingAggregate.Items.Select(lsa => lsa.Value).Sum();
            labourSalesHighlights.InvoicingBestName = labourSalesInvoicingAggregate.Items.Select(lsa => lsa.Label).FirstOrDefault();
            labourSalesHighlights.InvoicingWorstName = labourSalesInvoicingAggregate.Items.Select(lsa => lsa.Label).LastOrDefault();

            labourSalesHighlights.CreditingTotal = labourSalesCreditingAggregate.Items.Select(lsa => lsa.Value).Sum();
            labourSalesHighlights.CreditingBestName = labourSalesCreditingAggregate.Items.Select(lsa => lsa.Label).FirstOrDefault();
            labourSalesHighlights.CreditingWorstName = labourSalesCreditingAggregate.Items.Select(lsa => lsa.Label).LastOrDefault();

            return labourSalesHighlights;

When I don't inherit from AppServiceBase for this particular service, the code is working with the only exception that the audit log entry does not get created then.

When I inherit from AppServiceBase for this particular service, the execution of the above method results in an exception as per below:

System.NotSupportedException: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

I want to inherit from AppServiceBase so that my method call gets logged to the Audit Log table.

Please help!

3 Answer(s)
  • 0
    ismcagdas created
    Support Team


    Same situation is answered here. #668@37243a73-e69b-4f6c-b607-73f85972ce49

    I hope this helps.

  • 0
    doubledp created


    Thank you for this. I have seen that post.

    Not quite sure what Halil means by if you are not in a UOW there would be no problem. Another thing that I don't get is when I remove the inheritance of AppServiceBase my code works correctly.

    I am hoping Halil can point me in the right direction.

  • 0
    hikalkan created
    Support Team


    You can inherit from your AppServiceBase and disable UOW for your method:

    [UnitOfWork(IsDisabled = true)]
    public async Task<LabourSalesHighlightsDto> GetLabourSalesHighlights(LabourSalesAggregateInput input)

    To understand it better, you can see documents: <a class="postlink" href=""> ... sablingUow</a>

    Have a nice day.