I have a windows service that contains a file watcher that raises events when a file arrives. When an event occurs, I will use Ninject to create business-level objects, inside which there is a link to the Entity Framework context, which is also entered through Ninject. In my web applications, I always used InRequestScope for context, so in a single request all business-level objects work with the same Entity Framework context. In my current Windows service script, would it suffice to switch the Entity Framework context binding to the InThreadScope binding?
In theory, when an event handler in a service launches it in some thread, then if another file arrives at the same time, it will execute under another thread. Therefore, both events will not use the Entity Framework context, in fact, as two different HTTP requests on the Internet.
One thing that bothers me is the destruction of these scope objects when you look at the Ninject wiki:
.InThreadScope()- One instance of the type will be created for the stream.
.InRequestScope()- One type instance will be created for each web request and will be destroyed when the request is completed.
Based on this, I understand that InRequestScope objects will be destroyed (garbage collected?) When (or at some point after) the request ends. This says nothing about how InThreadScope objects are destroyed. To return to my example, when the file tracking event handler method is complete, the thread leaves (back to the thread pool?), What happens to the InThreadScope-d objects that were introduced?
EDIT:
, InThreadScope() , . , , , Entity Framework, , . , 5 , , .