2

I need to implement multithreading background job for import file. I have implemented it with background job(Hangfire). But if i use one thread it goes very slow. The function look like this.

I using non-transaction unit to save changes to db immediately.

var contactFound = await _contactRepository.FirstOrDefaultAsync(x => x.Email.ToLower() == contact.Email.ToLower());

            if (contactFound != null)
            {
                await _bjInfoManager.AddLog(args.JobId, "Found duplicated email: " + contact.Email);

            }
            else
            {
                contact.ContactListId = args.ContactListId;
                contact.Email = contact.Email.ToLower();

                await _contactRepository.InsertAsync(contact);

                //Save changes in db
                await CurrentUnitOfWork.SaveChangesAsync();
            }

The problem occur when I tries to use this with Producer-Consumer Dataflow Pattern. I throws the exception "A second operation started on this context before a previous asynchronous operation completed."

The question is how to create isolated DbContext inside this method. Please help me.

grinay
  • 709
  • 1
  • 6
  • 21

1 Answers1

1

Transactions should not be multi-threaded. If you create a new task/thread in a UOW, you can create a seperated UOW using IUnitOfWork.Begin(TransactionScopeOption.RequiresNew) in a using block.

See the links

If you are using Microsoft SQL Server, then I recommend you to use bulk insert. It's super fast than entity framework.

https://learn.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql

Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
  • Yes. I figure out before get answer. Thank. But there were many problems. I forgot add MaxLength for Email field in this way is not possible create index to this field. But fluent api didn't say nothing about that. These things come to very slow search even if table not so big. After create data annotation for field to restrict length of field index was created and all thing starts work fast. And one more thing I'm using method any for check exist method in db, it works more faster then firstOfDefault method. – grinay Dec 10 '17 at 10:12
  • what you mention is already filed as issue... You can track https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2762 – Alper Ebicoglu Dec 11 '17 at 05:37