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

Changed the registration mechanics of EF Core DbContext to avoid IoC … #1146

Merged
merged 1 commit into from
Nov 21, 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
6 changes: 6 additions & 0 deletions docs/guide/extensions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Configuration Extensions

::: warning
As of Wolverine 3.0 and our move to directly support non-Lamar IoC containers, it is no longer
possible to alter service registrations through Wolverine extensions that are themselves registered
in the IoC container at bootstrapping time.
:::

Wolverine supports the concept of extensions for modularizing Wolverine configuration with implementations of the `IWolverineExtension` interface:

<!-- snippet: sample_IWolverineExtension -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override async System.Threading.Tasks.Task HandleAsync(Wolverine.Runtime.
using var serviceScope = _serviceScopeFactory.CreateScope();

/*
* Dependency: Descriptor: ServiceType: Microsoft.EntityFrameworkCore.DbContextOptions"1[EfCoreTests.SampleDbContext] Lifetime: Scoped ImplementationFactory: System.Object <AddCoreServices>b__0(System.IServiceProvider)
* Dependency: Descriptor: ServiceType: Microsoft.EntityFrameworkCore.DbContextOptions"1[EfCoreTests.SampleDbContext] Lifetime: Scoped ImplementationFactory: Microsoft.EntityFrameworkCore.DbContextOptions"1[EfCoreTests.SampleDbContext] CreateDbContextOptions[SampleDbContext](System.IServiceProvider)
* The service registration for Microsoft.EntityFrameworkCore.DbContextOptions<EfCoreTests.SampleDbContext> is an 'opaque' lambda factory with the Scoped lifetime and requires service location
*/
var sampleDbContext = Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<EfCoreTests.SampleDbContext>(serviceScope.ServiceProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ internal class EntityFrameworkCoreBackedPersistence : IWolverineExtension
public void Configure(WolverineOptions options)
{
options.CodeGeneration.InsertFirstPersistenceStrategy<EFCorePersistenceFrameProvider>();

options.Services.AddScoped(typeof(IDbContextOutbox<>), typeof(DbContextOutbox<>));
options.Services.AddScoped<IDbContextOutbox, DbContextOutbox>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private static IServiceCollection addDbContextWithWolverineIntegration<T>(IServi
}, ServiceLifetime.Scoped, ServiceLifetime.Singleton);

services.AddSingleton<IWolverineExtension, EntityFrameworkCoreBackedPersistence>();

services.AddScoped(typeof(IDbContextOutbox<>), typeof(DbContextOutbox<>));
services.AddScoped<IDbContextOutbox, DbContextOutbox>();

services.AddSingleton<WolverineDbContextCustomizationOptions>(_ =>
string.IsNullOrEmpty(wolverineDatabaseSchema)
Expand All @@ -61,6 +64,19 @@ private static IServiceCollection addDbContextWithWolverineIntegration<T>(IServi
/// <param name="options"></param>
public static void UseEntityFrameworkCoreTransactions(this WolverineOptions options)
{
try
{
options.Services.AddScoped(typeof(IDbContextOutbox<>), typeof(DbContextOutbox<>));
options.Services.AddScoped<IDbContextOutbox, DbContextOutbox>();
}
catch (InvalidOperationException e)
{
if (!e.Message.Contains("The service collection cannot be modified because it is read-only."))
{
throw;
}
}

options.Include<EntityFrameworkCoreBackedPersistence>();
}

Expand Down
16 changes: 15 additions & 1 deletion src/Wolverine/WolverineOptions.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ internal void ApplyExtensions(IWolverineExtension[] extensions)

foreach (var extension in extensions)
{
extension.Configure(this);
try
{
extension.Configure(this);
}
catch (InvalidOperationException e)
{
if (e.Message.Contains("The service collection cannot be modified because it is read-only."))
{
throw new InvalidOperationException(
"As of Wolverine 3.0, it's no longer supported to alter IoC service registrations through Wolverine extensions that are themselves registered in the IoC container",
e);
}

throw;
}
AppliedExtensions.Add(extension);
}

Expand Down
Loading