Base solution for your next web application
Open Closed

A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe. #9290


User avatar
0
razkhan78 created

Hi

We are using latest version of asp.net MVC Core and jQuery.

In one of our app service we have multiple item deletion which works ike this:

public async Task FirstLevel(List<int> Ids) { foreach(var i in Ids) { SecondLevel(id); } }

private async Task SecondLevel(int id) { //Fetch data from repository //Delete obj MethodWithThread(obj.extId) }

private async Task MethodWithThread(string extId) { Task.Factory.StartNew(() => { call external apis }; }

Here in MethodWithThread we were getting A second operation started on this context before a previous operation completed so tried to used using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)).

But after using that in our second level methid still randomlt getting A second operation started on this context before a previous operation completed and also 2nd record in loops randomly not gets deleted.

Is there any other way to get data in method with thread?


1 Answer(s)
  • User Avatar
    0
    musa.demir created

    (I hardly understand your problem. (Sory if I get something unrelated))

    Could it be about forgetten await?

    await MethodWithThread(obj.extId);