Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. .Net Core 2.2.4 - Getting IHostingEnvironment in Program.cs Main

.Net Core 2.2.4 - Getting IHostingEnvironment in Program.cs Main

Scheduled Pinned Locked Moved ASP.NET
databasequestioncsharpasp-netmongodb
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • J jkirkerx

      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

      N Offline
      N Offline
      Nerea Clark
      wrote on last edited by
      #2

      [^][^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups