Closed tgardner closed 2 months ago
@tgardner Shoot, I took this in too quickly. It's going to take some changes for disposal at a minimum.
This:
internal class DependencyInjectionCommandCreator : ICommandCreator
{
private readonly IServiceProvider _serviceProvider;
public DependencyInjectionCommandCreator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public IOaktonCommand CreateCommand(Type commandType)
{
// Right here, you need to do something to ensure that the scope is disposed to clean up stuff
var scope = _serviceProvider.CreateScope();
return (IOaktonCommand)scope.ServiceProvider.GetRequiredService(commandType);
}
public object CreateModel(Type modelType)
{
return Activator.CreateInstance(modelType)!;
}
}
I'll build off what you did here as a starting point though.
@jeremydmiller - I was under the belief that the scope would need to live as long as the command that it created did. Disposing the scope, will in turn dispose any objects created, resulting in an exception when trying to use inside the command.
I made some assumptions here, possibly wrong. That the scope would be disposed of once the command executes and application finishes.
I'm seeing a couple of options here:
I'm happy to help if you can provide any form of guideance.
Fixes #92