Skip to content

Commit

Permalink
re-work for creating new httpclient using httpclienthandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
VeritasSoftware committed Jul 1, 2024
1 parent 1de04b4 commit 815a3c6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion AspNetCore.ApiGateway/Application/ApiGatewayOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class ApiGatewayOptions
public bool UseResponseCaching { get; set; }
public ApiGatewayResponseCacheSettings ResponseCacheSettings { get; set; }
public Action<IServiceProvider, HttpClient> DefaultHttpClientConfigure { get; set; }
public Func<IServiceProvider, MyHttpClient> DefaultMyHttpClient { get; set; }
public Func<HttpClientHandler> DefaultMyHttpClientHandler { get; set; }
}
}
12 changes: 7 additions & 5 deletions AspNetCore.ApiGateway/Application/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using AspNetCore.ApiGateway.Application;
using AspNetCore.ApiGateway.Application.ActionFilters;
using AspNetCore.ApiGateway.Application.ExceptionFilters;
using AspNetCore.ApiGateway.Middleware;
using AspNetCore.ApiGateway.Application.HubFilters;
using AspNetCore.ApiGateway.Application.ResultFilters;
using AspNetCore.ApiGateway.Authorization;
using AspNetCore.ApiGateway.Middleware;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Net.Http.Headers;
using AspNetCore.ApiGateway.Application.HubFilters;
using Microsoft.AspNetCore.SignalR;
using System.Text.Json;

namespace AspNetCore.ApiGateway
Expand Down Expand Up @@ -45,9 +45,11 @@ public static void AddApiGateway(this IServiceCollection services, Action<ApiGat
.AddHubFilters();


if (Options.DefaultMyHttpClient != null)
if (Options.DefaultMyHttpClientHandler != null)
{
services.AddHttpClient<IHttpService, MyHttpClient>();
services
.AddHttpClient<IHttpService, HttpService>()
.ConfigurePrimaryHttpMessageHandler(Options.DefaultMyHttpClientHandler);
}
else if (Options.DefaultHttpClientConfigure != null)
{
Expand Down
10 changes: 0 additions & 10 deletions AspNetCore.ApiGateway/Application/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,4 @@ public HttpService(HttpClient client)

public HttpClient Client { get; }
}

public class MyHttpClient : IHttpService
{
public HttpClient Client { get; private set; }

public MyHttpClient(HttpClientHandler clientHandler)
{
Client = new HttpClient(clientHandler);
}
}
}
10 changes: 7 additions & 3 deletions Docs/README_Customizations.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ You can create the default **HttpClient** used by **all the routes**, to hit the
Eg. below code adds Proxy info to a new HttpClient.

```C#
var proxyHost = "http://localhost";
var proxyPort = 80;
var proxyUserName = "testUser";
var proxyPassword = "testPwd";

var proxy = new WebProxy
{
Address = new Uri($"http://{proxyHost}:{proxyPort}"),
Expand All @@ -33,15 +38,14 @@ Eg. below code adds Proxy info to a new HttpClient.

var httpClientHandler = new HttpClientHandler
{
Proxy = proxy,
Proxy = proxy
};

//Api gateway
services.AddApiGateway(options =>
{
options.DefaultMyHttpClient = (sp => new MyHttpClient(httpClientHandler));
options.DefaultMyHttpClientHandler = () => httpClientHandler;
});

```

Also, the library provides hooks to
Expand Down

0 comments on commit 815a3c6

Please sign in to comment.