Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DisposeAsync with scopes and hosts #99

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/Oakton/DependencyInjectionCommandCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,24 @@ public object CreateModel(Type modelType)

internal class WrappedOaktonCommand : IOaktonCommand
{
private readonly IServiceScope _scope;
private readonly AsyncServiceScope _scope;
private readonly IOaktonCommand _inner;

public WrappedOaktonCommand(IServiceProvider provider, Type commandType)
{
_scope = provider.CreateScope();
_scope = provider.CreateAsyncScope();
_inner = (IOaktonCommand)_scope.ServiceProvider.GetRequiredService(commandType);
}

public Type InputType => _inner.InputType;
public UsageGraph Usages => _inner.Usages;
public async Task<bool> Execute(object input)
{
try
await using (_scope)
{
// Execute your actual command
return await _inner.Execute(input);
}
finally
{
// Make sure the entire scope is disposed
_scope.SafeDispose();
}
}
}

Expand Down
30 changes: 22 additions & 8 deletions src/Oakton/HostWrapperCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,29 @@ public HostWrapperCommand(IOaktonCommand inner, Func<IHost> hostSource, Property
public UsageGraph Usages => _inner.Usages;
public async Task<bool> Execute(object input)
{
using var host = _hostSource();
using var scope = host.Services.CreateScope();
foreach (var prop in _props)
var host = _hostSource();
try
{
var serviceType = prop.PropertyType;
var service = scope.ServiceProvider.GetRequiredService(serviceType);
prop.SetValue(_inner, service);
}
await using var scope = host.Services.CreateAsyncScope();
foreach (var prop in _props)
{
var serviceType = prop.PropertyType;
var service = scope.ServiceProvider.GetRequiredService(serviceType);
prop.SetValue(_inner, service);
}

return await _inner.Execute(input);
return await _inner.Execute(input);
}
finally
{
if (host is IAsyncDisposable ad)
{
await ad.DisposeAsync();
}
else
{
host.Dispose();
}
}
}
}
11 changes: 9 additions & 2 deletions src/Oakton/HostedCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static async Task<int> RunHostedOaktonCommandsAsync(this IHost host, stri
{
try
{
using var scope = host.Services.CreateScope();
await using var scope = host.Services.CreateAsyncScope();
var options = scope.ServiceProvider.GetRequiredService<IOptions<OaktonOptions>>().Value;
args = ApplyArgumentDefaults(args, options);

Expand All @@ -88,7 +88,14 @@ public static async Task<int> RunHostedOaktonCommandsAsync(this IHost host, stri
}
finally
{
host.SafeDispose();
if (host is IAsyncDisposable ad)
{
await ad.DisposeAsync();
}
else
{
host.Dispose();
}
}
}

Expand Down
Loading