public static Microsoft.AspNetCore.Hosting.IWebHost SeedUsers(this Microsoft.AspNetCore.Hosting.IWebHost host) { using (var scope = host.Services.GetRequiredService <IServiceScopeFactory>().CreateScope()) { //seed users var customDbContext = scope.ServiceProvider.GetService <DevArenaDbContext>(); if (customDbContext.Users.FirstOrDefault(_ => _.Username == "admin") == null) { customDbContext.Users.Add(new DevArenaUser() { Username = "******", Password = "******", SubjectId = Guid.NewGuid().ToString(), Role = 1 }); } if (customDbContext.Users.FirstOrDefault(_ => _.Username == "guest") == null) { customDbContext.Users.Add(new DevArenaUser() { Username = "******", Password = "******", SubjectId = Guid.NewGuid().ToString(), Role = 2 }); } customDbContext.SaveChanges(); } return(host); }
public static Microsoft.AspNetCore.Hosting.IWebHost ConfigureIdentityServer(this Microsoft.AspNetCore.Hosting.IWebHost host) { using (var scope = host.Services.GetRequiredService <IServiceScopeFactory>().CreateScope()) { //create database scheme var persistedGrantContext = scope.ServiceProvider.GetService <PersistedGrantDbContext>(); persistedGrantContext.Database.Migrate(); var configurationContext = scope.ServiceProvider.GetService <ConfigurationDbContext>(); configurationContext.Database.Migrate(); //get data from appsettings var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile($"appsettings.{environmentName}.json", optional: false, reloadOnChange: true); IConfigurationRoot configuration = builder.Build(); var data = new IdentityServerConfigurationHelper(configuration); //seed IS4 data (from appsettings to db) data.Seed(configurationContext); } return(host); }
//this is a method called CreateDbContext, returns an ApplicationDbContext object //where is this called? public ApplicationDbContext CreateDbContext(string[] args) { // Note that the BuilderWebHost method in the Program.cs is called on the next line Microsoft.AspNetCore.Hosting.IWebHost webHost = Program.BuildWebHost(args); System.IServiceProvider serviceProvider = webHost.Services; ApplicationDbContext applicationDbContext = serviceProvider.GetRequiredService <ApplicationDbContext>(); return(applicationDbContext); }
public static System.Threading.Tasks.Task WaitForShutdownAsync(this Microsoft.AspNetCore.Hosting.IWebHost host, System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) { throw null; }
public static void WaitForShutdown(this Microsoft.AspNetCore.Hosting.IWebHost host) { }
public static System.Threading.Tasks.Task StopAsync(this Microsoft.AspNetCore.Hosting.IWebHost host, System.TimeSpan timeout) { throw null; }
public static void Run(this Microsoft.AspNetCore.Hosting.IWebHost host) { }
public WebHostService(Microsoft.AspNetCore.Hosting.IWebHost host) { }
public static string GetAddress(this Microsoft.AspNetCore.Hosting.IWebHost host) { throw null; }
public static void Run(this Microsoft.AspNetCore.Hosting.IWebHost host, System.Threading.CancellationToken token) { }
public static Microsoft.AspNetCore.TestHost.TestServer GetTestServer(this Microsoft.AspNetCore.Hosting.IWebHost host) { throw null; }
public static System.Net.Http.HttpClient GetTestClient(this Microsoft.AspNetCore.Hosting.IWebHost host) { throw null; }
public static Microsoft.AspNetCore.Hosting.IWebHost MigrateDbContext <TContext>(this Microsoft.AspNetCore.Hosting.IWebHost webHost, Action <TContext, IServiceProvider> seeder) where TContext : DbContext { using (var scope = webHost.Services.CreateScope()) { var services = scope.ServiceProvider; var logger = services.GetRequiredService <ILogger <TContext> >(); var context = services.GetService <TContext>(); try { logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}"); var retry = Policy.Handle <SqlException>() .WaitAndRetry(new TimeSpan[] { TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(8), }); retry.Execute(() => { //if the sql server container is not created on run docker compose this //migration can't fail for network related exception. The retry options for DbContext only //apply to transient exceptions. context.Database .Migrate(); seeder(context, services); }); logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}"); } catch (Exception ex) { logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}"); } } return(webHost); }