Is your feature request related to a problem? Please describe the problem.
I'm not sure if this is the proper repo, but I did not get any response in dotnet/runtime repo. I'm hoping someone from the team can take a look? Thank you!
Describe the solution you'd like
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 and Service B may need to add items to a collection maintained by Service 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 of Service 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 this IServiceProvider instance to create a new Workflow object. Is my assumption true that all services from this point on will be resolved using this scoped IServiceProvider rather that the root IServiceProvider? For instance, in the constructor of the DocumentBuilder object, I need all services that were registered with scoped lifetime to be resolved using the scoped IServieProvider passed to WorkfloFactory.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.
public class Process<TFiling, TPosting> : IHostedService
where TFiling : Filing where TPosting : PostingModel, new()
{
...
public async Task StartAsync(CancellationToken cancellationToken)
{
var notifications = await m_notificationWebApiService.GetUnprocessedFilingNotificationsAsync();
foreach (var notification in notifications)
{
using (var scope = m_serviceProvider.CreateScope())
{
// all scoped services resolved from this point on need to be instantiated when accessed for the first time
await m_workflowFactory.Create(scope.ServiceProvider, notification.FilingTypeGuid).Execute(notification.FilingNotificationGuid);
}
}
}
}
...
public class WorkflowFactory : IWorkflowFactory<Filing, Posting>
{
public Workflow<Filing, Posting> Create(IServiceProvider serviceProvider, Guid filingTypeGuid)
{
Workflow<Filing, Posting> workflow = null;
if (filingTypeGuid == Constant.FilingTypeGuid.Will)
{
workflow = ActivatorUtilities.CreateInstance<Will>(serviceProvider);
}
else if (filingTypeGuid == Constant.FilingTypeGuid.MentalHealth)
{
workflow = ActivatorUtilities.CreateInstance<MentalHealth>(serviceProvider);
}
...
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
I'm not sure if this is the proper repo, but I did not get any response in dotnet/runtime repo. I'm hoping someone from the team can take a look? Thank you!
Describe the solution you'd like
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
Additional context
No response