I want to know if it is possible using build in dotnet core DI framework to register multiple instances of the same class
For example I have a class ToDo that sends off messages to clients. I want 2 instances of the same ToDo object with different injected configuration objects. So at the end I will have 2 separate instances of ToDo object.
Is this possible?
Edit:
Suppose I have a pattern pub/sub
I have a generic class called MessageSubService.cs and the implementation looks something like this
public class ASBMessageSubService : ASBSubService, IASBSubService
{
public ASBMessageSubService(..., IOptions<ASBSubOptions> options): base(options)
}
So based on this I have multiple ASBMessageSubService that I will need to create. The only thing will differ is the IOptions that passed in. IOptions is internal access. I can not access that property if I use provider.GetRequireServices<T>.
I do understand I can do this
service.AddSingleton<ASBMessageSubService, IASBSubService>
service.AddSingleton<ASBMessageSubService, IASBSubService>
service.AddSingleton<ASBMessageSubService, IASBSubService>
This will register me 3 different instances. The issue is The implementation is that same and I will not be able to resolve it by the type where `nameof(ASBMessageSubService);
I can also register a deligate where I can resolve it based on name or type but this runs into same issue I described above, the type of implementation will be the same.
(I am aware that I can use libraries like structuremap or autofac to get this done with registering them as named instance. However I would like to avoid 3rd party tools like this in this project. )
Any suggestions on this?
Thank you!
services.AddSingleton<IMyService>(...)multiple times and your consumer classes can take anIEnumerable<IMyService>for example.IEnumnerable<>How will I know what instance to get, since the underneath implementations is that same? For example I will register sameToDoServicemultiple times perIToDointerface. ?