I'm using an IServiceCollection
to create a list of required services for my objects. Now I want to instantiate an object and have the DI container resolve the dependencies for that object
Example
// In my services config.
services
.AddTransient<IMyService, MyServiceImpl>();
// the object I want to create.
class SomeObject
{
public SomeObject(IMyService service)
{
...
}
}
How to I get the DI container to create an object of type SomeObject
, with the dependecies injected? (presumably this is what it does for controllers?)
Note: I do not want to store SomeObject
in the services collection, I just want to be able to do something like this...
SomeObject obj = startup.ServiceProvider.Resolve<SomeObject>();
... Rationale: I don't have to add all of my controllers to the service container, so I don't see why I would have to add SomeObject
to it either!?