Closed daniel-p-tech closed 2 years ago
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection See info in area-owners.md if you want to be subscribed.
Author: | daniel-p-tech |
---|---|
Assignees: | - |
Labels: | `area-Extensions-DependencyInjection` |
Milestone: | - |
Answered elsewhere.
Hi,
I am architecting a complex background process that will be processing items (called "workflows" in my application) from a queue (an
IEnumerable
returned from a Web API call). The process consists of numerous services, some of which expose data structures that need to be accessible from other services:Service A
andService B
may need to add items to a collection maintained byService C
. Once a "workflow" item is processed, next "workflow" item to be processed from the queue needs to start with a new state (all services need to be disposed of and a new instance ofService C
is provided when requested again).My understanding is that this should be possible to implement using a scoped service. However, I have spent several hours doing research/reading documentation, but I was unable to find a good example on how to architect this. Please take a look at the code snippets below. Based on my preliminary tests, this approach seems to work. Basically, I pass in the
scope.ServiceProvider
reference to the workflow factory which then uses thisIServiceProvider
instance to create a newWorkflow
object. Is my assumption true that all services from this point on will be resolved using this scopedIServiceProvider
rather that the rootIServiceProvider
? For instance, in the constructor of theDocumentBuilder
object, I need all services that were registered with scoped lifetime to be resolved using the scopedIServieProvider
passed toWorkfloFactory.Create()
method.In other words, I am expecting the same behavior as ASP.NET Core web app except that my request is created inside a custom loop rather that an HTTP request.
Thank you for your help, Daniel