Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient wrapper with Autofac for Web API

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;
    }
}
like image 895
GilliVilla Avatar asked Jul 16 '14 18:07

GilliVilla


People also ask

How to integrate with Autofac web API?

Do all the stuff for standard Web API integration - register controllers, set the dependency resolver, etc. Set up your app with the base Autofac OWIN integration. Add a reference to the Autofac.WebApi2.Owin NuGet package. In your application startup class, register the Autofac Web API middleware after registering the base Autofac middleware.

What is httpconfiguration in Autofac?

For OWIN integration, the HttpConfiguration is the one you create in your app startup class and pass to the Web API middleware. At application startup, while building your Autofac container, you should register your Web API controllers and their dependencies.

Can I use httpclient with Autofac 6?

Cyril's implementation of using an Autofac module works wonderfully, but unfortunately is not compatible with Autofac 6.0+. In order to configure an HttpClient in Autofac 6.0+ for a specific service, an Autofac middleware needs to be implemented:

What is httpclient in Salesforce?

Summary HttpClient is used to sendan HTTP request, using a URL. HttpClient can be used to make Web API requests from the console Application, Winform Application, Web form Application, Windows store Applicatio,n etc.


1 Answers

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.

  1. Register the PlayersAPIHttpClientModule with the AutoFac ContainerBuilder
  2. Use the AutoFac Container to resolve the IDomainPlayerManager as required
like image 154
Matt Caton Avatar answered Sep 17 '22 10:09

Matt Caton