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 the CreateAsyncScope() helper to avoid having to manually cast IServiceProvider to IAsyncDisposable #2275

Merged
merged 1 commit into from
Mar 19, 2025
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
28 changes: 6 additions & 22 deletions sandbox/OpenIddict.Sandbox.AspNetCore.Client/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,17 @@ namespace OpenIddict.Sandbox.AspNetCore.Client;

public class Worker : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private readonly IServiceProvider _provider;

public Worker(IServiceProvider serviceProvider)
=> _serviceProvider = serviceProvider;
public Worker(IServiceProvider provider)
=> _provider = provider;

public async Task StartAsync(CancellationToken cancellationToken)
{
var scope = _serviceProvider.CreateScope();
await using var scope = _provider.CreateAsyncScope();

try
{
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await context.Database.EnsureCreatedAsync(cancellationToken);
}

finally
{
if (scope is IAsyncDisposable disposable)
{
await disposable.DisposeAsync();
}

else
{
scope.Dispose();
}
}
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await context.Database.EnsureCreatedAsync(cancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
Expand Down
32 changes: 8 additions & 24 deletions sandbox/OpenIddict.Sandbox.AspNetCore.Server/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,20 @@ namespace OpenIddict.Sandbox.AspNetCore.Server;

public class Worker : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private readonly IServiceProvider _provider;

public Worker(IServiceProvider serviceProvider)
=> _serviceProvider = serviceProvider;
public Worker(IServiceProvider provider)
=> _provider = provider;

public async Task StartAsync(CancellationToken cancellationToken)
{
var scope = _serviceProvider.CreateScope();
await using var scope = _provider.CreateAsyncScope();

try
{
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await context.Database.EnsureCreatedAsync(cancellationToken);

await RegisterApplicationsAsync(scope.ServiceProvider);
await RegisterScopesAsync(scope.ServiceProvider);
}

finally
{
if (scope is IAsyncDisposable disposable)
{
await disposable.DisposeAsync();
}
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await context.Database.EnsureCreatedAsync(cancellationToken);

else
{
scope.Dispose();
}
}
await RegisterApplicationsAsync(scope.ServiceProvider);
await RegisterScopesAsync(scope.ServiceProvider);

static async Task RegisterApplicationsAsync(IServiceProvider provider)
{
Expand Down
2 changes: 1 addition & 1 deletion sandbox/OpenIddict.Sandbox.Console.Client/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Worker(IServiceProvider provider)

public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _provider.CreateScope();
await using var scope = _provider.CreateAsyncScope();

var context = scope.ServiceProvider.GetRequiredService<DbContext>();
await context.Database.EnsureCreatedAsync(cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion sandbox/OpenIddict.Sandbox.WinForms.Client/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Worker(IServiceProvider provider)

public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _provider.CreateScope();
await using var scope = _provider.CreateAsyncScope();

var context = scope.ServiceProvider.GetRequiredService<DbContext>();
await context.Database.EnsureCreatedAsync(cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion sandbox/OpenIddict.Sandbox.Wpf.Client/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Worker(IServiceProvider provider)

public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _provider.CreateScope();
await using var scope = _provider.CreateAsyncScope();

var context = scope.ServiceProvider.GetRequiredService<DbContext>();
await context.Database.EnsureCreatedAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,49 +146,33 @@ private async Task HandleRequestAsync<TProperty>(TProperty property, Cancellatio

cancellationToken.ThrowIfCancellationRequested();

var scope = _provider.CreateScope();
await using var scope = _provider.CreateAsyncScope();

try
{
var dispatcher = scope.ServiceProvider.GetRequiredService<IOpenIddictClientDispatcher>();
var factory = scope.ServiceProvider.GetRequiredService<IOpenIddictClientFactory>();

// Create a client transaction and store the specified instance so
// it can be retrieved by the event handlers that need to access it.
var transaction = await factory.CreateTransactionAsync();
transaction.SetProperty(typeof(TProperty).FullName!, property);
var dispatcher = scope.ServiceProvider.GetRequiredService<IOpenIddictClientDispatcher>();
var factory = scope.ServiceProvider.GetRequiredService<IOpenIddictClientFactory>();

var context = new ProcessRequestContext(transaction)
{
CancellationToken = cancellationToken
};
// Create a client transaction and store the specified instance so
// it can be retrieved by the event handlers that need to access it.
var transaction = await factory.CreateTransactionAsync();
transaction.SetProperty(typeof(TProperty).FullName!, property);

await dispatcher.DispatchAsync(context);
var context = new ProcessRequestContext(transaction)
{
CancellationToken = cancellationToken
};

if (context.IsRejected)
{
await dispatcher.DispatchAsync(new ProcessErrorContext(transaction)
{
CancellationToken = cancellationToken,
Error = context.Error ?? Errors.InvalidRequest,
ErrorDescription = context.ErrorDescription,
ErrorUri = context.ErrorUri,
Response = new OpenIddictResponse()
});
}
}
await dispatcher.DispatchAsync(context);

finally
if (context.IsRejected)
{
if (scope is IAsyncDisposable disposable)
{
await disposable.DisposeAsync();
}

else
await dispatcher.DispatchAsync(new ProcessErrorContext(transaction)
{
scope.Dispose();
}
CancellationToken = cancellationToken,
Error = context.Error ?? Errors.InvalidRequest,
ErrorDescription = context.ErrorDescription,
ErrorUri = context.ErrorUri,
Response = new OpenIddictResponse()
});
}
}

Expand Down
Loading