Created
December 6, 2020 08:50
-
-
Save rahulsahay19/271058502ed5557229edfbf7f69e74ed to your computer and use it in GitHub Desktop.
program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
using Movies.Infrastructure.Data; | |
namespace Movies.API | |
{ | |
public class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
var host = CreateHostBuilder(args).Build(); | |
await CreateAndSeedDb(host); | |
host.Run(); | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
}); | |
private static async Task CreateAndSeedDb(IHost host) | |
{ | |
using (var scope = host.Services.CreateScope()) | |
{ | |
var services = scope.ServiceProvider; | |
var loggerFactory = services.GetRequiredService<ILoggerFactory>(); | |
try | |
{ | |
var moviesContext = services.GetRequiredService<MovieContext>(); | |
await MovieContextSeed.SeedAsync(moviesContext, loggerFactory); | |
} | |
catch (Exception ex) | |
{ | |
var logger = loggerFactory.CreateLogger<Program>(); | |
logger.LogError($"Exception Occured in API: {ex.Message}"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment