title: How does IHostingEnvironment.ContentRootPath get set?
body: <p>In my Azure Service Fabric Web API project, I am able to add my appsettings.json files to my config using this code in my <code>Api.cs</code> class:</p>
  
  <pre><code>    protected override IEnumerable&lt;ServiceInstanceListener&gt; CreateServiceInstanceListeners()
      {
          return new ServiceInstanceListener[]
          {
              new ServiceInstanceListener(serviceContext =&gt;
                  new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =&gt;
                  {
                      ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
  
                      return new WebHostBuilder()
                                  .UseKestrel()
                                  .ConfigureAppConfiguration((builderContext, config) =&gt;
                                  {
                                          var env = builderContext.HostingEnvironment;
                                          config.SetBasePath(env.ContentRootPath)
                                              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                                              .AddEnvironmentVariables();
                                  })
                                  .ConfigureServices((hostingContext, services) =&gt; services
                                  .AddSingleton&lt;StatelessServiceContext&gt;(serviceContext)
                                  .AddApplicationInsightsTelemetry(hostingContext.Configuration))
                                  .UseContentRoot(Directory.GetCurrentDirectory())
                                  .UseStartup&lt;Startup&gt;()
                                  .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                  .UseUrls(url)
                                  .Build();
                  }))
          };
      }
  </code></pre>
  
  <p>The <code>env.ContentRootPath</code> contains this value:</p>
  
  <pre><code>C:\SfDevCluster\Data\_App\_Node_0\Abc.IntegrationType_App197\Abc.Integration.ApiPkg.Code.1.0.0
  </code></pre>
  
  <p>In this folder I can see the appsettings.json files.</p>
  
  <p>However, in my Azure Service Fabric Stateless Service project, I have this code in my <code>Service.cs</code> class:</p>
  
  <pre><code>    protected override async Task RunAsync(CancellationToken cancellationToken)
      {
          Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
              .ConfigureAppConfiguration((builderContext, config) =&gt;
              {
                  var env = builderContext.HostingEnvironment;
  
                  var appName = AppDomain.CurrentDomain.FriendlyName;
                  var parentFolderPath = Directory.GetParent(env.ContentRootPath).FullName;
                  var packageCodeFolderPath = Path.Combine(parentFolderPath, appName + "Pkg.Code.1.0.0");
  
                  config.SetBasePath(packageCodeFolderPath)
                      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                      .AddEnvironmentVariables();
              })
              .UseStartup&lt;Startup&gt;()
              .Build()
              .Run();
      }
  </code></pre>
  
  <p>Here <code>env.ContentRootPath</code> contains this value:</p>
  
  <pre><code>C:\SfDevCluster\Data\_App\_Node_0\Abc.Integration.OpticalType_App198\work
  </code></pre>
  
  <p>However because the <code>appsettings.json</code> files are located in:
  <code>C:\SfDevCluster\Data\_App\_Node_0\Abc.Integration.OpticalType_App198\Abc.Integration.Optical.ServicePkg.Code.1.0.0</code>,</p>
  
  <p>I'm forced to construct the <code>packageCodeFolderPath</code> variable you can see above.</p>
  
  <p>This doesn't seem like a very elegant solution, and I'm worried the version number in <code>Pkg.Code.1.0.0</code> might change (if that is even a version number?).</p>
  
  <p>How can I set <code>env.ContentRootPath</code> to be the path to the folder containing the <code>appsettings.json</code> files? Or is there a better way to go about accessing these files?</p>