title: Custom IConfiguration Service in Client SIde Blazor
body: <p>I am trying to have some basic configuration from json file to a singleton service inside my client side blazor application at the start up.</p>
  
  <p>Below is my code setup</p>
  
  <p>AppConfig and IAppConfig files</p>
  
  <pre><code>interface IAppConfig
  {
      string BaseUrl { get; set; }
  
      string Source { get; set; }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class AppConfig : IAppConfig
  {
    public string BaseUrl { get; set; }
    public string Source { get; set; }
  }
  </code></pre>
  
  <p>Than a json file by the name of environment.json inside wwwroot as wwwroot/ConfigFiles/environment.json</p>
  
  <p>Than a service to read this file </p>
  
  <pre><code>interface ISharedServices
  {
    Task&lt;AppConfig&gt; GetConfigurationAsync();
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class SharedServices : ISharedServices
  {
      private HttpClient Http { get; set; }
  
      public SharedServices(HttpClient httpClient)
      {
          Http = httpClient;
      }
  
      public async Task&lt;AppConfig&gt; GetConfigurationAsync()
      {
          return await Http.GetJsonAsync&lt;AppConfig&gt;("ConfigFiles/environment.json");
      }
  }
  </code></pre>
  
  <p>Now i am calling it into my component which load first </p>
  
  <pre><code>public class IndexComponent : ComponentBase
  {
      [Inject]
      internal IAppConfig AppConfig { get; set; }
  
      [Inject]
      internal ISharedServices sharedServices { get; set; }
  
      protected override async Task OnInitializedAsync()
      {
          var appconfig = await sharedServices.GetConfigurationAsync();
          AppConfig = appconfig;
      }
  }
  </code></pre>
  
  <p>All this works fine , but i want to have this configuration ready at the time of application load in browser , so as suggested by "auga from mars" in my other <a href="https://stackoverflow.com/questions/58128731/share-data-through-service-in-asp-net-blazor-client-side/">Question</a> i tried below code inside startup.cs at the moment i add IAppConfig as singleton service </p>
  
  <pre><code>services.AddSingleton&lt;IAppConfig, AppConfig&gt;(provider =&gt;
   {
     var http = provider.GetRequiredService&lt;HttpClient&gt;();
     return http.GetJsonAsync&lt;AppConfig&gt;("ConfigFiles/environment.json").GetAwaiter().GetResult();
    });
  </code></pre>
  
  <p>But , buy using this code the blazor app never start up , all it show a blank white page with text Loading.... , not even any error but in every 5 min pop up show - page taking too much time to load with two option of wait and close .</p>
  
  <p>If i change this code a bit from</p>
  
  <pre><code>return http.GetJsonAsync&lt;AppConfig&gt;("ConfigFiles/environment.json").GetAwaiter().GetResult();
  </code></pre>
  
  <p>to </p>
  
  <pre><code>return http.GetJsonAsync&lt;AppConfig&gt;("ConfigFiles/environment.json").Result;
  </code></pre>
  
  <p>Than it say - "Maximum call stack size exceed"</p>
  
  <p>How to have configuration ready at startup ??</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>A little Update </p>
  
  <p>in Basecomponent file , code is </p>
  
  <pre><code> protected override async Task OnInitializedAsync()
      {
          var appconfig = await sharedServices.GetConfigurationAsync();
          AppConfig.BaseUrl = appconfig.BaseUrl;
          AppConfig.Source = appconfig.Source;
      }
  </code></pre>
  
  <p>I have to set every property one by one manually , need to get rid of this too</p>