title: How to access appsettings.json from Azure Service Fabric stateless service
body: <p>I have a solution with a stateless service project (.sfproj) and a corresponding console app project.</p>
  
  <p>I have added appsettings.json to the console app project and am trying to set up access to it using this:</p>
  
  <pre><code>protected override IEnumerable&lt;ServiceInstanceListener&gt; CreateServiceInstanceListeners()
  {
      //return this.CreateServiceRemotingInstanceListeners(); //This line works
      return new ServiceInstanceListener[] //This line causes an error in Service Fabric Explorer
      {
          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;
                      {
                          config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                      })
                      .ConfigureServices(
                          services =&gt; services
                              .AddSingleton&lt;StatelessServiceContext&gt;(serviceContext))
                      .UseContentRoot(Directory.GetCurrentDirectory())
                      //.UseStartup&lt;Startup&gt;() //Startup.cs does not exist
                      .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                      .UseUrls(url)
                      .Build();
              }))
      };
  }
  </code></pre>
  
  <p>This doesn't compile because I don't have a <code>Startup</code> class as it's not a web api project, it's using a stateless service. The only classes I have in the console app are <code>Program.cs</code>, <code>Service.cs</code> and <code>ServiceEventSource.cs</code></p>
  
  <p>I tried removing the <code>.UseStartup&lt;Startup&gt;()</code> to get it to compile, however when I run the application, I get the following error in Service Fabric Explorer:</p>
  
  <pre><code>'System.FM' reported Error for property 'State'.
  Partition is below target replica or instance count.
  fabric:/Integration/Integration.Service 1 1 9810816d-5be9-4b53-9eed-c5be403219b0
    InBuild _Node_1 132003711574580709
    (Showing 1 out of 1 instances. Total available instances: 0)
  </code></pre>
  
  <p>I can fix this by using <code>return this.CreateServiceRemotingInstanceListeners();</code> instead, however then I don't know how to set up access to appsettings.json.</p>
  
  <p>How can I access <code>appsettings.json</code> from within my stateless service?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I managed to get it working using this in <code>Service.cs</code>:</p>
  
  <pre><code>        protected override async Task RunAsync(CancellationToken cancellationToken)
          {
              Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
                  .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();
                  })
                  .UseStartup&lt;Startup&gt;()
                  .Build()
                  .Run();
          }
  </code></pre>
  
  <p>I added this to <code>ServiceManifest.xml</code> to make sure <code>env.ContentRootPath</code> points to the code package folder which contains the <code>appsettings.json</code> files (the default seems to be the Work folder which was empty for me):</p>
  
  <pre><code>&lt;EntryPoint&gt;
    &lt;ExeHost&gt;
      &lt;Program&gt;TheAppNameGoesHere.exe&lt;/Program&gt;
      &lt;WorkingFolder&gt;CodePackage&lt;/WorkingFolder&gt;
    &lt;/ExeHost&gt;
  &lt;/EntryPoint&gt;
  </code></pre>
  
  <p>I added this <code>Startup</code> class:</p>
  
  <pre><code>public class Startup
  {
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      { }
  }
  </code></pre>
  
  <p>I feel like there must be a better way to access <code>appsettings.json</code>, especially considering the <code>Startup.Configure()</code> method is doing nothing.</p>
  
  <p>Does anyone know a better way to access <code>appsettings.json</code>?</p>