Closed damienbod closed 7 years ago
strange, I don't have this problem. I'll try to reproduce with a clean repo
Weird, I've reproduced it on your AspNetCoreLocalization example app, just changed the Main method in Startup class. Also need to change Startup constructor to
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
since stuff that has been there before is handled by "WebHost.CreateDefaultBuilder(args)" in Main.
I have the same issue. Just updated today to .net core 2.0. The issue seems to be related to the pattern described here:
https://blog.markvincze.com/two-gotchas-with-scoped-and-singleton-dependencies-in-asp-net-core/
The first gotcha (Depending on a scoped dependency from a singleton).
Quote:
"Luckily in the 2.0 version of ASP.NET Core this (Depending on a scoped dependency from a singleton) is going to be validated by the DI framework, and we'll get an exception if we mess it up (this change cannot be done in the 1.* versions, since it'd be a breaking change)."
thanks, I'll fix this tomorrow
fixed in this version
https://www.nuget.org/packages/Localization.SqlLocalizer/2.0.1
You need to update the context creation as well.
services.AddDbContext<LocalizationModelContext>(options =>
options.UseSqlite(
sqlConnectionString,
b => b.MigrationsAssembly("Angular2LocalizationAspNetCore")
),
ServiceLifetime.Singleton,
ServiceLifetime.Singleton
);
Thank you! I will update my code later today, and check your fix. Does your fix imply that you made the lifetimes of the dbcontext and the IStringLocalizerFactory singleton?
@pjvanhall thanks, yes
the context must now be a singleton because the factories are also a singleton now. This is ok, because the GETS are do not modify the data and can be used with mutliple requests.
@damienbod Thanks, it works now. Btw, transient also works in case there will be any issues with singletons.
@McAroon thanks for the feedback
Hi! In .NET Core 2.0 the Main method has changed to
Now the app crashes on launch with
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Cannot consume scoped service 'Microsoft.Extensions.Localization.IStringLocalizerFactory' from singleton 'Microsoft.Extensions.Options.IOptions'1[Microsoft.AspNetCore.Mvc.MvcOptions]'.