0

I am having an issue with EF not updating my object within another thread. I am able to retrieve objects, but when I call UnitOfWork.Commit();, SQL Server profiler doesnt show the item being updated. If I run this outside of the thread sequentially then it works fine. Any ideas?

 new Thread(() =>
                {
                    var divisionBracketsService = DependencyResolver.Current.GetService<IDivisionBracketsService>();

                    if (divisionBracketsService != null)
                        divisionBracketsService.ProcessGame(gameId);

                }).Start();

UPDATE

Well it worked when I updated the scope to thread. Are there any patterns to use both scopes?

kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>().InThreadScope();

UPDATE

I noticed this question Ninject InRequestScope fallback to InThreadScope, but I am using version 3.0 of Ninject and it doesn't contain StandardScopeCallbacks.Request.

Community
  • 1
  • 1
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341

1 Answers1

2

From your question I see that you are not aware of the problems of starting threads in ASP.NET to make it short, IIS can decide to kill that thread at any time leaving your application in an inconsisent state. If the excecution of this thread is essential to your app then do it either syncronous, delegate it to an other process e.g. A windows service or get the knowhow how to do it correctly by registering with ASP.NET

Using a hybrid scope is not deterministic, because at the time the thread is executed the http context may exist or not but it can be dispsed at anytime. The better way is to inject some processor class and define it as the scope for its dedencies using the named scope instead of newing a thread.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98