title: Asp core: IHostedService and IHttpClientFactory - best way?
body: <p>I have asp core app. I use IHostedService for periodic task. I need send periodic http request. This is my code:</p>
  <pre><code>  class MyService: IHostedService 
    {
      
      private readonly IServiceScopeFactory _scopeFactory;
  
      public MyService(IServiceScopeFactory scopeFactory)
      {
          _scopeFactory = scopeFactory;
      }        
  
      private async void DoWork(object state)
      {
          using (var scope = _scopeFactory.CreateScope())
          {
              var clientFactory = scope.ServiceProvider.GetRequiredService&lt;IHttpClientFactory&gt;();
              var request = new HttpRequestMessage(HttpMethod.Get,
                  &quot;https://api.github.com/repos/aspnet/AspNetCore.Docs/branches&quot;);
              request.Headers.Add(&quot;Accept&quot;, &quot;application/vnd.github.v3+json&quot;);
              request.Headers.Add(&quot;User-Agent&quot;, &quot;HttpClientFactory-Sample&quot;);
  
              var client = clientFactory.CreateClient();
              var response = await client.SendAsync(request);
              ...
              
          }
      }
    }
   
  </code></pre>
  <p>Also I added it in my Startup.cs</p>
  <pre><code>services.AddHttpClient(); 
  </code></pre>
  <p>This is working. But I'm not sure if this is the best performance solution. Any tips?</p>