HttpClient wrapper with Autofac for Web API
.net-4.5, asp.net-web-api2, autofac, c#-4.0, dotnet-httpclient
Solution
As described in the AutoFac Module documentation:
A module is a small class that can be used to bundle up a set of related components behind a 'facade' to simplify configuration and deployment. The module exposes a deliberate, restricted set of configuration parameters that can vary independently of the components used to implement the module.
As per the common use cases of Modules as described in the documentation, a common use case is to:
Configure related services that provide a subsystem, e.g. data access with NHibernate
In the case of your code base, the `PlayersAPIHttpClientModule` is configuring the `PlayerDomainManager` as a service that implements the `IPlayerDomainManager` and configuring its lifetime to act as a singleton. The benefit is that the Module allows the deeply buried configuration requirement of the PlayerDomainManager (the base service url that in turn is required by one of its dependencies) to be surfaced as configuration centralised to the Modules constructor. This benefit would be more obvious if the configuration was more complex.
Modules need to be registered with AutoFac as per any other dependency:
`builder.RegisterModule(new PlayersAPIHttpClientModule("base_service_url));`
In turn, the services they configure are resolved using standard AutoFac dependency resolution.
`scope.Resolve<IDomainPlayerManager>();`
So, to answer your question, no you would not use the PlayersAPIHttpClientModule as per your question.
- Register the `PlayersAPIHttpClientModule` with the AutoFac `ContainerBuilder`
- Use the AutoFac `Container` to resolve the `IDomainPlayerManager` as required
Problem
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. Looking at the Autofac wire-up as a Module below - my question is would the consumer do this: ``` var client = PlayersAPIHttpClientModule("http:/api.players.com"); ``` How does this setup facilitate the consumer to pass the base URI and then access the GetPlayerInformation method? ``` 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 => { var serviceClient = new HttpClient { BaseAddress = new System.Uri(this._serviceBaseUrl) }; return new PlayerDomainManager(serviceClient, ctx.Resolve<IMappingEngine>()); }) .SingleInstance() .As<IPlayerDomainManager>(); } } } ``` This is the interface shared with the core domain. ``` public interface IPlayerDomainManager { IPlayer GetPlayerInformation (string playerId); } ``` And this is the class itself with the exposed method functionality. ``` 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; } } ```