0

I'm moving some code from .net framework to .net standard and i'm struggling to replicate some code that creates a HttpClient.

private HttpClient CreateHttpClient(Guid userId, SiteHandler siteHandler)
{
    List<DelegatingHandler> handlers = new List<DelegatingHandler>
    {
        new AccessTokenHandler(_authorisationService, userId)
    };

    HttpClient client = HttpClientFactory.Create(handlers.ToArray());
    client.BaseAddress = _baseAddressUri;
    client.DefaultRequestHeaders
        .Accept
        .Add(new MediaTypeWithQualityHeaderValue("application/json"));
    return client;
}

public class AccessTokenHandler : DelegatingHandler
{
    private readonly IAuthorisationService _authorisationService;
    private readonly Guid _userId;

    public AccessTokenHandler(IAuthorisationService authorisationService, Guid userId)
    {
        _authorisationService = authorisationService ?? throw new ArgumentNullException(nameof(authorisationService));
        _userId = userId;
    }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        string token = await _authorisationService.GetValidTokenAsync(_userId);

        if (token == null)
        {
            throw new ApiTokenException();
        }

        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
        return await base.SendAsync(request, cancellationToken);
    }
}

What this code does is it sets up some middleware on the request so that when a request is made using the HttpClient the AccessTokenHandler adds an Access Token for the user to the request at the time of the call.

I can't seem to find anything that allows me to do this using .net standard. I can't find HttpClientFactory outside of a .net framework project.

Can anyone help?

chris31389
  • 8,414
  • 7
  • 55
  • 66
  • Why use `HttpClientFactory` just to create an HttpClient instead of passing a handler to the constructor? – Panagiotis Kanavos Sep 15 '17 at 10:26
  • I used it to add multiple DelegatingHandlers to the HttpClient that could be re-used for other HttpClients – chris31389 Sep 15 '17 at 10:28
  • Do you have an example of how I could use a handler instead so that it adds the access token at the time of the request? – chris31389 Sep 15 '17 at 10:28
  • 1
    Check [this](https://stackoverflow.com/questions/18976042/httpclientfactory-create-vs-new-httpclient) question which explains that the factory is only a helper. Each HttpMessageHandler-derived class can accept another handler itself, or you can speficy one afterwards by setting the [InnerHandler](https://msdn.microsoft.com/en-us/library/system.net.http.delegatinghandler.innerhandler(v=vs.118).aspx) property. – Panagiotis Kanavos Sep 15 '17 at 10:40
  • If you check the code of [HttpClientFactory.CreatePipeline](https://github.com/mono/aspnetwebstack/blob/master/src/System.Net.Http.Formatting/HttpClientFactory.cs#L57) from Mono, you'll see that the factory is nothing more than a loop that sets one handler's InnerHandler property to the next. This isn't worth the name "Factory". *Maybe* they moved that to a utility or extension class. It's very easy to duplicate the loop though – Panagiotis Kanavos Sep 15 '17 at 10:41

0 Answers0