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

Support IServiceProvider access when registering DbContext #7017

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,46 @@ public static class AspireAzureEFCoreCosmosExtensions
private const string DefaultConfigSectionName = "Aspire:Microsoft:EntityFrameworkCore:Cosmos";
private const DynamicallyAccessedMemberTypes RequiredByEF = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties;

/// <inheritdoc cref="AddCosmosDbContext{TContext}(IHostApplicationBuilder, string, string, Action{EntityFrameworkCoreCosmosSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddCosmosDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
string databaseName) where TContext : DbContext
=> AddCosmosDbContextInternal<TContext>(builder, connectionName, databaseName, configureSettings: null, configureDbContextOptions: null);

/// <inheritdoc cref="AddCosmosDbContext{TContext}(IHostApplicationBuilder, string, string, Action{EntityFrameworkCoreCosmosSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddCosmosDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
string databaseName,
Action<EntityFrameworkCoreCosmosSettings>? configureSettings) where TContext : DbContext
=> AddCosmosDbContextInternal<TContext>(builder, connectionName, databaseName, configureSettings, configureDbContextOptions: null);

/// <inheritdoc cref="AddCosmosDbContext{TContext}(IHostApplicationBuilder, string, string, Action{EntityFrameworkCoreCosmosSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddCosmosDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
string databaseName,
Action<DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddCosmosDbContextInternal<TContext>(builder, connectionName, databaseName, configureSettings: null, (_, options) => configureDbContextOptions?.Invoke(options));

/// <inheritdoc cref="AddCosmosDbContext{TContext}(IHostApplicationBuilder, string, string, Action{EntityFrameworkCoreCosmosSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddCosmosDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
string databaseName,
Action<EntityFrameworkCoreCosmosSettings>? configureSettings,
Action<DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddCosmosDbContextInternal<TContext>(builder, connectionName, databaseName, configureSettings, (_, options) => configureDbContextOptions?.Invoke(options));

/// <inheritdoc cref="AddCosmosDbContext{TContext}(IHostApplicationBuilder, string, string, Action{EntityFrameworkCoreCosmosSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddCosmosDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
string databaseName,
Action<IServiceProvider, DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddCosmosDbContextInternal<TContext>(builder, connectionName, databaseName, configureSettings: null, configureDbContextOptions);

/// <summary>
/// Registers the given <see cref="DbContext" /> as a service in the services provided by the <paramref name="builder"/>.
/// Enables db context pooling, logging and telemetry.
Expand All @@ -39,8 +79,16 @@ public static class AspireAzureEFCoreCosmosExtensions
this IHostApplicationBuilder builder,
string connectionName,
string databaseName,
Action<EntityFrameworkCoreCosmosSettings>? configureSettings = null,
Action<DbContextOptionsBuilder>? configureDbContextOptions = null) where TContext : DbContext
Action<EntityFrameworkCoreCosmosSettings>? configureSettings,
Action<IServiceProvider, DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddCosmosDbContextInternal<TContext>(builder, connectionName, databaseName, configureSettings, configureDbContextOptions);

private static void AddCosmosDbContextInternal<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
IHostApplicationBuilder builder,
string connectionName,
string databaseName,
Action<EntityFrameworkCoreCosmosSettings>? configureSettings,
Action<IServiceProvider, DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
{
ArgumentNullException.ThrowIfNull(builder);

Expand Down Expand Up @@ -69,7 +117,7 @@ public static class AspireAzureEFCoreCosmosExtensions

ConfigureInstrumentation<TContext>(builder, settings);

void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
void ConfigureDbContext(IServiceProvider services, DbContextOptionsBuilder dbContextOptionsBuilder)
{
if (!string.IsNullOrEmpty(settings.ConnectionString))
{
Expand All @@ -88,7 +136,7 @@ void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
$"in the '{DefaultConfigSectionName}' or '{DefaultConfigSectionName}:{typeof(TContext).Name}' configuration section.");
}

configureDbContextOptions?.Invoke(dbContextOptionsBuilder);
configureDbContextOptions?.Invoke(services, dbContextOptionsBuilder);
}

void UseCosmosBody(CosmosDbContextOptionsBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings.Re
Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings.RequestTimeout.get -> System.TimeSpan?
Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings.RequestTimeout.set -> void
Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions
static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName, System.Action<Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings!>? configureSettings = null, System.Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions = null) -> void
static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.EnrichCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, System.Action<Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings!>? configureSettings = null) -> void
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
#nullable enable

static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName) -> void
static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName, System.Action<Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings!>? configureSettings) -> void
static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName, System.Action<Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings!>? configureSettings, System.Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName, System.Action<Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings!>? configureSettings, System.Action<System.IServiceProvider!, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName, System.Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName, System.Action<System.IServiceProvider!, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
*REMOVED*static Microsoft.Extensions.Hosting.AspireAzureEFCoreCosmosExtensions.AddCosmosDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, string! databaseName, System.Action<Aspire.Microsoft.EntityFrameworkCore.Cosmos.EntityFrameworkCoreCosmosSettings!>? configureSettings = null, System.Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions = null) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,41 @@ public static class AspireSqlServerEFCoreSqlClientExtensions
private const string DefaultConfigSectionName = "Aspire:Microsoft:EntityFrameworkCore:SqlServer";
private const DynamicallyAccessedMemberTypes RequiredByEF = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties;

/// <inheritdoc cref="AddSqlServerDbContext{TContext}(IHostApplicationBuilder, string, Action{MicrosoftEntityFrameworkCoreSqlServerSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddSqlServerDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName) where TContext : DbContext
=> AddSqlServerDbContextInternal<TContext>(builder, connectionName, configureSettings: null, configureDbContextOptions: null);

/// <inheritdoc cref="AddSqlServerDbContext{TContext}(IHostApplicationBuilder, string, Action{MicrosoftEntityFrameworkCoreSqlServerSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddSqlServerDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
Action<MicrosoftEntityFrameworkCoreSqlServerSettings>? configureSettings) where TContext : DbContext
=> AddSqlServerDbContextInternal<TContext>(builder, connectionName, configureSettings, configureDbContextOptions: null);

/// <inheritdoc cref="AddSqlServerDbContext{TContext}(IHostApplicationBuilder, string, Action{MicrosoftEntityFrameworkCoreSqlServerSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddSqlServerDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
Action<DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddSqlServerDbContextInternal<TContext>(builder, connectionName, configureSettings: null, (_, options) => configureDbContextOptions?.Invoke(options));

/// <inheritdoc cref="AddSqlServerDbContext{TContext}(IHostApplicationBuilder, string, Action{MicrosoftEntityFrameworkCoreSqlServerSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddSqlServerDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
Action<MicrosoftEntityFrameworkCoreSqlServerSettings>? configureSettings,
Action<DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddSqlServerDbContextInternal<TContext>(builder, connectionName, configureSettings, (_, options) => configureDbContextOptions?.Invoke(options));

/// <inheritdoc cref="AddSqlServerDbContext{TContext}(IHostApplicationBuilder, string, Action{MicrosoftEntityFrameworkCoreSqlServerSettings}?, Action{IServiceProvider, DbContextOptionsBuilder}?)"/>
public static void AddSqlServerDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
Action<IServiceProvider, DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddSqlServerDbContextInternal<TContext>(builder, connectionName, configureSettings: null, configureDbContextOptions);

/// <summary>
/// Registers the given <see cref="DbContext" /> as a service in the services provided by the <paramref name="builder"/>.
/// Enables db context pooling, retries, health check, logging and telemetry for the <see cref="DbContext" />.
Expand All @@ -37,8 +72,15 @@ public static class AspireSqlServerEFCoreSqlClientExtensions
public static void AddSqlServerDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
this IHostApplicationBuilder builder,
string connectionName,
Action<MicrosoftEntityFrameworkCoreSqlServerSettings>? configureSettings = null,
Action<DbContextOptionsBuilder>? configureDbContextOptions = null) where TContext : DbContext
Action<MicrosoftEntityFrameworkCoreSqlServerSettings>? configureSettings,
Action<IServiceProvider, DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
=> AddSqlServerDbContextInternal<TContext>(builder, connectionName, configureSettings, configureDbContextOptions);

private static void AddSqlServerDbContextInternal<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
IHostApplicationBuilder builder,
string connectionName,
Action<MicrosoftEntityFrameworkCoreSqlServerSettings>? configureSettings,
Action<IServiceProvider, DbContextOptionsBuilder>? configureDbContextOptions) where TContext : DbContext
{
ArgumentNullException.ThrowIfNull(builder);

Expand All @@ -60,7 +102,7 @@ public static class AspireSqlServerEFCoreSqlClientExtensions

ConfigureInstrumentation<TContext>(builder, settings);

void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
void ConfigureDbContext(IServiceProvider serviceProvider, DbContextOptionsBuilder dbContextOptionsBuilder)
{
// We don't register logger factory, because there is no need to:
// https://learn.microsoft.com/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.useloggerfactory?view=efcore-7.0#remarks
Expand All @@ -82,7 +124,7 @@ void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
}
});

configureDbContextOptions?.Invoke(dbContextOptionsBuilder);
configureDbContextOptions?.Invoke(serviceProvider, dbContextOptionsBuilder);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
#nullable enable

static Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName) -> void
static Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<Aspire.Microsoft.EntityFrameworkCore.SqlServer.MicrosoftEntityFrameworkCoreSqlServerSettings!>? configureSettings) -> void
static Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<Aspire.Microsoft.EntityFrameworkCore.SqlServer.MicrosoftEntityFrameworkCoreSqlServerSettings!>? configureSettings, System.Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
static Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<Aspire.Microsoft.EntityFrameworkCore.SqlServer.MicrosoftEntityFrameworkCoreSqlServerSettings!>? configureSettings, System.Action<System.IServiceProvider!, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
static Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
static Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<System.IServiceProvider!, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions) -> void
*REMOVED*static Microsoft.Extensions.Hosting.AspireSqlServerEFCoreSqlClientExtensions.AddSqlServerDbContext<TContext>(this Microsoft.Extensions.Hosting.IHostApplicationBuilder! builder, string! connectionName, System.Action<Aspire.Microsoft.EntityFrameworkCore.SqlServer.MicrosoftEntityFrameworkCoreSqlServerSettings!>? configureSettings = null, System.Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder!>? configureDbContextOptions = null) -> void
Loading
Loading