Enable validate scopes in dependency injection (#783)
This commit is contained in:
parent
93012e66a0
commit
7787e963dd
|
|
@ -19,16 +19,17 @@ namespace MusicStore.Models
|
||||||
|
|
||||||
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider, bool createUsers = true)
|
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider, bool createUsers = true)
|
||||||
{
|
{
|
||||||
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
|
using (var serviceScope = serviceProvider.CreateScope())
|
||||||
{
|
{
|
||||||
var db = serviceScope.ServiceProvider.GetService<MusicStoreContext>();
|
var scopeServiceProvider = serviceScope.ServiceProvider;
|
||||||
|
var db = scopeServiceProvider.GetService<MusicStoreContext>();
|
||||||
|
|
||||||
if (await db.Database.EnsureCreatedAsync())
|
if (await db.Database.EnsureCreatedAsync())
|
||||||
{
|
{
|
||||||
await InsertTestData(serviceProvider);
|
await InsertTestData(scopeServiceProvider);
|
||||||
if (createUsers)
|
if (createUsers)
|
||||||
{
|
{
|
||||||
await CreateAdminUser(serviceProvider);
|
await CreateAdminUser(scopeServiceProvider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,10 @@ namespace MusicStore
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
.UseConfiguration(config)
|
.UseConfiguration(config)
|
||||||
.UseIISIntegration()
|
.UseIISIntegration()
|
||||||
.UseStartup("MusicStore");
|
.UseStartup("MusicStore")
|
||||||
|
.UseDefaultServiceProvider((context, options) => {
|
||||||
|
options.ValidateScopes = true;
|
||||||
|
});
|
||||||
|
|
||||||
if (string.Equals(builder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", System.StringComparison.Ordinal))
|
if (string.Equals(builder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", System.StringComparison.Ordinal))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue