Ninject definition for DBContext used in Quartz.Net job

What is the best application for a DbContext implementation that is created using the Ninject dependency implementer during the execution of the Quartz.Net implementation? If I used a thread scope, will the same DbContext instance be serviced if the same thread in the Quartz thread pool is used to complete the job multiple times?

I need a scope that means that I get one (and only one) new instance of DbContext every time I start a job.

+5
source share
1 answer

Yes, I would recommend using InThreadScope . When a pool thread stream is used, a leak will occur.

In addition, there is nothing built into ninject like "QuartzScope", so you will need to create your own solution. start by creating a job. This fooobar.com/questions/966938 / ... and, in more detail, the source code for this nuget package .

Now, one of the possible solutions is the JobFactory extension for controlling the creation of the DbContext and its entry into the task and all its dependencies (as an inherited parameter of ConstructorArgument ). However, this has two drawbacks: it always creates a DbContext (whether this task is needed or not), and you need to track DbContext instances DbContext that you can .Dispose() of them in the IJobFactory.ReturnJob(IJob) (p.ex. a Dictionary<IJob, DBContext or the like).

It is much easier to use .InCallScope (included in Ninject.Extensions.NamedScope ) to bind DbContext . This will create one instance of DbContext per kernel.Get<FooJob>() .

If you need different uses for DbContext - depending on where you use them, p.Ex. inside the job and inside your ASP.NET MVC stuff, can you watch the Ninject Conditional Self bind to change the scope (for Task-scheduler) not working properly?

+6
source

Source: https://habr.com/ru/post/1200470/


All Articles