damienbod / AspNetCoreLocalization

Localization.SqlLocalizer & ASP.NET Core MVC Localization Examples
http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/
MIT License
251 stars 102 forks source link

Update ASP.NET Core 2.0 #43

Closed damienbod closed 7 years ago

McAroon commented 7 years ago

Hi! In .NET Core 2.0 the Main method has changed to

       public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();

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]'.

damienbod commented 7 years ago

strange, I don't have this problem. I'll try to reproduce with a clean repo

McAroon commented 7 years ago

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.

pjvanhall commented 7 years ago

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)."

damienbod commented 7 years ago

thanks, I'll fix this tomorrow

damienbod commented 7 years ago

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
);
pjvanhall commented 7 years ago

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?

damienbod commented 7 years ago

@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.

McAroon commented 7 years ago

@damienbod Thanks, it works now. Btw, transient also works in case there will be any issues with singletons.

damienbod commented 7 years ago

@McAroon thanks for the feedback