.Net Core 2.2.4 - Getting IHostingEnvironment in Program.cs Main
-
I know how to get it in Startup, but I wrote a class that seeds the database with values, and expanded it to write the product images to wwwroot from the database called SeedDatabase. I just need the wwwroot so I can write the images to the right place. Guess I figured it out... Amazing, had no idea this would work, well in development at least. So I can request services from startup? That's my new question.
private static void SeedDatabase(IWebHost host, Settings appSettings, bool isDocker, string env)
{
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var logger = services.GetRequiredService>();
var environment = services.GetService();
logger.LogInformation("DbAuthConnection=" + appSettings.MongoDB.Connection);
logger.LogInformation("DbAuthContainer=" + appSettings.MongoDB.Container);
logger.LogInformation("Environment=" + env);try { DbInitializer.SeedData(appSettings, environment, isDocker); logger.LogInformation("Seed DB Succeeded:" + DateTime.Now); } catch (Exception ex) { logger.LogError(ex, "Seed DB Failed on " + DateTime.Now + ":" + ex.Message); } }
}
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
I know how to get it in Startup, but I wrote a class that seeds the database with values, and expanded it to write the product images to wwwroot from the database called SeedDatabase. I just need the wwwroot so I can write the images to the right place. Guess I figured it out... Amazing, had no idea this would work, well in development at least. So I can request services from startup? That's my new question.
private static void SeedDatabase(IWebHost host, Settings appSettings, bool isDocker, string env)
{
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var logger = services.GetRequiredService>();
var environment = services.GetService();
logger.LogInformation("DbAuthConnection=" + appSettings.MongoDB.Connection);
logger.LogInformation("DbAuthContainer=" + appSettings.MongoDB.Container);
logger.LogInformation("Environment=" + env);try { DbInitializer.SeedData(appSettings, environment, isDocker); logger.LogInformation("Seed DB Succeeded:" + DateTime.Now); } catch (Exception ex) { logger.LogError(ex, "Seed DB Failed on " + DateTime.Now + ":" + ex.Message); } }
}
If it ain't broke don't fix it Discover my world at jkirkerx.com
[^][^]