title: HttpClient wrapper with Autofac for Web API
body: <p>I have inherited a stub project which is a HttpClient wrapper specific to an API enpoint we maintain.
  The intention is to distribute this solution as nuget to other .NET teams that would need to consume the API endpoint.</p>
  
  <p>Looking at the Autofac wire-up as a Module below - my question is would the consumer do this:</p>
  
  <pre><code>var client = PlayersAPIHttpClientModule("http:/api.players.com");
  </code></pre>
  
  <p>How does  this setup facilitate the consumer to pass the base URI and then access the GetPlayerInformation method?</p>
  
  <pre><code>using Autofac;
  using AutoMapper;
  using Alpha.Domain.Players;
  using System.Net.Http;
  
  namespace Alpha.Clients.Players
  {
      public class PlayersAPIHttpClientModule : Module
      {
          private readonly string _serviceBaseUrl;
  
          public PlayersAPIHttpClientModule(string serviceBaseUrl)
          {
              this._serviceBaseUrl = serviceBaseUrl;
          }
  
          protected override void Load(ContainerBuilder builder)
          {
              base.Load(builder);
  
              builder.Register(ctx =&gt;
              {
                  var serviceClient = new HttpClient 
                  { 
                      BaseAddress = 
                      new System.Uri(this._serviceBaseUrl) 
                  };
  
                  return new 
                   PlayerDomainManager(serviceClient, 
                                       ctx.Resolve&lt;IMappingEngine&gt;());
              })
                  .SingleInstance()
                  .As&lt;IPlayerDomainManager&gt;();
          }
      }
  }   
  </code></pre>
  
  <p>This is the interface shared with the core domain.  </p>
  
  <pre><code>public interface IPlayerDomainManager
  {
      IPlayer GetPlayerInformation (string playerId);
  }
  </code></pre>
  
  <p>And this is the class itself with the exposed method functionality.</p>
  
  <pre><code>internal class PlayerDomainManager : IPlayerDomainManager
  {
      private readonly HttpClient _client;
  
      private readonly IMappingEngine _mapper;
  
      public PlayerDomainManager(HttpClient client, IMappingEngine mapper)
      {
          this._client = client;
          this._mapper = mapper;
      }
  
      public IPlayer GetPlayerInformation(string playerId)
      {
          var response = this._client
                          .SendAsync
                              (new 
                              HttpRequestMessage(HttpMethod.Get,
                               "/players/" + playerId),
                              CancellationToken.None)
                              .Result;
      }
  }
  </code></pre>