JasperFx / wolverine

Supercharged .NET server side development!
https://wolverinefx.net
MIT License
1.27k stars 139 forks source link

Service collection modified after build #1140

Closed OwnageIsMagic closed 4 days ago

OwnageIsMagic commented 5 days ago

AddDbContextWithWolverineIntegration is calling services.AddSingleton<IWolverineExtension, EntityFrameworkCoreBackedPersistence>();

HostBuilderExtensions.AddWolverine is calling

var extensions = s.GetServices<IWolverineExtension>();
options.ApplyExtensions(extensions.ToArray());

and EntityFrameworkCoreBackedPersistence contains

options.Services.AddScoped(typeof(IDbContextOutbox<>), typeof(DbContextOutbox<>));
        options.Services.AddScoped<IDbContextOutbox, DbContextOutbox>();

thats reults in 'System.InvalidOperationException' in Microsoft.Extensions.DependencyInjection.Abstractions.dll: 'The service collection cannot be modified because it is read-only.'

IWolverineExtension resloved from DI, can't modify DI container.

It's can be resolved by explicitly calling UseEntityFrameworkCoreTransactions which applies this extension inline.

jeremydmiller commented 5 days ago

Yeah, sorry, you can no longer do that after we switched to using ServiceProvider as a default. Pretty sure this is documented. Guess we can make the docs call it out a little clearer, or catch & rethrow a more explanatory exception.

You can modify the Wolverine configuration, but not make any new service registrations

OwnageIsMagic commented 5 days ago

https://wolverine.netlify.app/guide/durability/efcore.html examples shows that method, but don't mention that it's required when using AddDbContextWithWolverineIntegration and removing it results in not friendly exception. Any reason why AddDbContextWithWolverineIntegration can't apply EntityFrameworkCoreBackedPersistence inline?

jeremydmiller commented 5 days ago

It's apparently a bug. I think the test harnesses might still be using Lamar so this didn't get caught.

At some point the root cause is just me not being omniscient, and it's something that got missed. Admittedly, Wolverine is much more commonly used with Marten and the EF Core integration doesn't always keep up. Something I'd like to see improved greatly next year.