dotnet-architecture / News

News on .NET Architecture Guidance, eShopOnContainers and all the reference apps in dotnet-architecture
MIT License
1.1k stars 79 forks source link

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

Open CESARDELATORRE opened 6 years ago

CESARDELATORRE commented 6 years ago

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

Main subjects/chapters updated:

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.

Sn3b commented 6 years ago

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

ayayalar commented 6 years ago
    +1

    Arif

On Mon, Jun 18, 2018 at 11:08 PM -0700, "Sebastien de Salvador" notifications@github.com wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

maulikk2000 commented 6 years ago

hi Cesar,

great news. Thanks for updating the book.

I have a quick question about how to add HttpClientHandler object to client factory. In .net core 2.0, it was easy to create object of HttpClientHandler and add it to the HttpClient but as things have changed now, couldnt find an easier way to do it in .net core 2.1.

The reason I want HttpClientHandler added to the HttpClient is, I went to one of the MS Kubernetes OpenHack events here in Sydney and one of the tasks was to create an API to replace KubeCtl. So I created a .net core 2.1 application ( source code at https://github.com/maulikk2000/openhack ) but on hitting GET on KubeController, getting " Internal Server Error The remote certificate is invalid according to the validation procedure" error. One of the suggestions from MS team was to use HttpClientHandler's ServerCertificateCustomValidationCallback method , but as things have changed in 2.1, there wasnt any easy way to do it. We thought of using typed client, but it was proving bit challenging. So just want to know, is there any easy already backed way of using it, or will have to take a bit different approach and create a Typed Client and add HttpClientHandler there.

Thanks, Maulik Khandwala

On Tue, Jun 19, 2018 at 9:40 AM Cesar De la Torre notifications@github.com wrote:

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: image] https://user-images.githubusercontent.com/1712635/41567367-ae6ebe58-7314-11e8-92e8-439698e8919a.png

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 to have sooner updates in the guidance instead of a single big update coming later.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dotnet-architecture/News/issues/17, or mute the thread https://github.com/notifications/unsubscribe-auth/AEsZoJRemAAJEmwykl2KLNS7v9So5Iatks5t-DptgaJpZM4UsonK .

CESARDELATORRE commented 6 years ago

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

CESARDELATORRE commented 6 years ago

@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? :)

ayayalar commented 6 years ago
    Thank you Cesar!!!

    Arif

On Tue, Jun 19, 2018 at 7:41 PM -0700, "Cesar De la Torre" notifications@github.com wrote:

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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

maulikk2000 commented 6 years ago

Hi Ceasar,

thanks for the prompt reply.

Actually I am using HttpClientFactory in my API ( source code at https://github.com/maulikk2000/openhack ) . But my problem is " Internal Server Error The remote certificate is invalid according to the validation procedure" and to overcome in dev I need to use ServerCert ificateCustomValidationCallback which returns true and that method is on HttpClientHandler.

Excerpt from my startup.cs

services.AddHttpClient("openhackAPI", client => {

            var uri = "http://127.0.0.1:8080/";
            client.BaseAddress = new Uri(uri);
            client.DefaultRequestHeaders.Add("Accept",

"application/json"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken); });

Controller

public async Task Get() { var client = _httpClientFactory.CreateClient("openhackAPI");

        var data = await

client.GetAsync("api/v1/namespaces/default/services"); return Ok(data);

    }

In .net core 2.0 I could write like (from https://stackoverflow.com/questions/38138952/bypass-invalid-ssl-certificate-in-net-core )

using (var httpClientHandler = new HttpClientHandler()){ httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; using (var client = new HttpClient(httpClientHandler)) { // Make your request... }}

But I couldnt find a similar way in HttpClientFactory to do it.

Basically my question is, how do I use ServerCert ificateCustomValidationCallback with HttpClientFactory?

thanks, Maulik

On Wed, Jun 20, 2018 at 12:46 PM Cesar De la Torre notifications@github.com wrote:

@maulikk2000 https://github.com/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? :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet-architecture/News/issues/17#issuecomment-398607046, or mute the thread https://github.com/notifications/unsubscribe-auth/AEsZoFJ6OKQnKL3DiwoBgF-goquhtiq6ks5t-beSgaJpZM4UsonK .

onizet commented 6 years ago
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);
     }
}
sashcatk commented 6 years ago

Re: [dotnet-architecture/News] .NET Microservices eBook is being updated to .NET Core 2.1 "wave" of technologies! (#17)

What the fuck ?

Вы писали 20 июня 2018 г., 13:42:20:

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();

///

/// Provides the Bearer authentication header from the current caller.

///

sealed class BearerTokenHandler : DelegatingHandler

{

    private readonly IHttpContextAccessor httpContextAccessor;

    public BearerTokenHandler(IHttpContextAccessor httpContextAccessor)

    {

        this.httpContextAccessor = httpContextAccessor;

    }

    protected override async Task 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);

    }

} — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

-- Зайцев Александр                        mailto:sasha@tk-chel.ru

maulikk2000 commented 6 years ago

awesome..

thanks for the help. It works like a charm. It's the thing what I was after.

thanks heaps, Maulik

On Wed, Jun 20, 2018 at 6:41 PM Olivier Nizet notifications@github.com wrote:

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(); ///

/// Provides the Bearer authentication header from the current caller./// 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);
 }

}

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet-architecture/News/issues/17#issuecomment-398671561, or mute the thread https://github.com/notifications/unsubscribe-auth/AEsZoJ4woOtwQiwC9tIIp8yzxykCtL1Oks5t-grUgaJpZM4UsonK .

CESARDELATORRE commented 6 years ago

@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. 👍