dotnet-architecture / News

News on .NET Architecture Guidance, eShopOnContainers and all the reference apps in dotnet-architecture

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.NET Microservices eBook is being updated to .NET Core 2.1 "wave" of technologies!

CESARDELATORRE opened this issue · comments

The .NET Microservices eBook is being updated to .NET Core 2.1 "wave" of technologies!

Main subjects/chapters updated:

  • HttpClientFactory + Polly for resilient Http communication in .NET Core 2.1
  • API Gateways and implementation with Ocelot
  • Minor updates related to .NET Core 2.1 versions, packages and Docker images

Download from: https://aka.ms/microservicesebook
image

Note that this is still an on-going effort, hence the version "v2.1.01".

In the upcoming weeks it'll be updated in a few more topics and the version will be updated to the next minor versions, such as "v2.1.01", "v2.1.02", etc.

But we think the community appreciates having sooner updates in the guidance instead of a single big update coming later.

Great news! Would you please also update the ePub and mobi versions?

@Sn3b @ayayalar - The eReaders (mobi and epub) versions will be updated in the upcoming weeks, ok? :)

@maulikk2000 - Is there any reason why you cannot use HttpClientFactory in .NET Core 2.1 for your scenario? - That’s the recommended way to use HttpClient so it re-uses the pool of handlers plus it is easi r to use policies.
If possible, use HttpClientFactory, either with typed clients, named clients or even directly.
Take a look to the section about HttpClientFactory in the eBook and also to the references I put about HttpClientFactory.
If you had issues, tell me so we’ll look for further possibilities, ok? :)

var clientBuilder = services.AddHttpClient("openhackAPI", client => { ... });
clientBuilder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() {
   ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
});

Btw, I'm curious about your Bearer token and its lifetime (unless this is an offline_token).
I forward the call to another internal server by providing the received bearer token, with the use of a MessageHandler:

var clientBuilder = services.AddHttpClient("openhackAPI", client => { ... })
      .AddHttpMessageHandler<BearerTokenHandler>();

/// <summary>
/// Provides the Bearer authentication header from the current caller.
/// </summary>
sealed class BearerTokenHandler : DelegatingHandler
{
     private readonly IHttpContextAccessor httpContextAccessor;

     public BearerTokenHandler(IHttpContextAccessor httpContextAccessor)
     {
         this.httpContextAccessor = httpContextAccessor;
     }

     protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
         CancellationToken cancellationToken)
     {
         var context = httpContextAccessor.HttpContext;
         if (context.Request.Headers.TryGetValue("Authorization", out StringValues bearer))
             request.Headers.Authorization = AuthenticationHeaderValue.Parse(bearer.ToString());

         return await base.SendAsync(request, cancellationToken);
     }
}

@onizet @maulikk2000 - Right, comment from @onizet about setting the PrimaryHandler is how you must configure HttpClientHandler.

There's some more info here:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1#configure-the-httpmessagehandler

I told the ASP.NET team that we should have some further documentation bout this topic. 👍