Skip to content

Commit aef3d25

Browse files
committed
Use HttpClientFactory.
1 parent af34c2a commit aef3d25

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,20 @@ Latest .NET Standard 2.0.
2525

2626
### Examples
2727

28+
First, you must add the required services.
29+
30+
```csharp
31+
public void ConfigureServices(IServiceCollection services)
32+
{
33+
...
34+
services.AddProxies();
35+
...
36+
}
37+
```
38+
2839
#### Existing Controller
2940

3041
You can use the proxy functionality on an existing `Controller` by leveraging the `Proxy` extension method.
31-
If this is the only feature that you use, then you do not need to configure anything in `Configure` and `ConfigureServices`.
3242

3343
```csharp
3444
public class MyController : Controller
@@ -54,12 +64,17 @@ app.UseProxy("api/{arg1}/{arg2}", async (args) => {
5464

5565
#### `ProxyRoute` Attribute
5666

57-
You can also make the proxy look and feel almost like a route.
67+
You can also make the proxy look and feel almost like a route, but as part of a static method.
5868

59-
In your `Configure(IApplicationBuilder, IHostingEnvironment)` method, add the middleware.
69+
First, add the middleware.
6070

6171
```csharp
62-
app.UseProxies();
72+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
73+
{
74+
...
75+
app.UseProxies();
76+
...
77+
}
6378
```
6479

6580
Then, create a static method which returns a `Task<string>` or `string` (where the `string` is the URI to proxy).

src/Core/AspNetCore.Proxy.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<Version>1.4.1</Version>
3+
<Version>2.0.0</Version>
44
<AssemblyName>AspNetCore.Proxy</AssemblyName>
55
<PackageId>AspNetCore.Proxy</PackageId>
66
<DocumentationFile>bin\AspNetCore.Proxy.xml</DocumentationFile>
@@ -10,5 +10,6 @@
1010
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
1111
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
1212
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
1314
</ItemGroup>
1415
</Project>

src/Core/Helpers.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.Routing;
9+
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.DependencyModel;
1011

1112
namespace AspNetCore.Proxy
@@ -84,7 +85,11 @@ private static HttpRequestMessage CreateProxyHttpRequest(this HttpContext contex
8485
internal static Task<HttpResponseMessage> SendProxyHttpRequest(this HttpContext context, string proxiedAddress)
8586
{
8687
var proxiedRequest = context.CreateProxyHttpRequest(proxiedAddress);
87-
return new HttpClient().SendAsync(proxiedRequest, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);
88+
89+
return context.RequestServices
90+
.GetService<IHttpClientFactory>()
91+
.CreateClient()
92+
.SendAsync(proxiedRequest, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);
8893
}
8994

9095
internal static async Task CopyProxyHttpResponse(this HttpContext context, HttpResponseMessage responseMessage)

src/Core/ProxyRouteExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.Mvc;
99
using Microsoft.AspNetCore.Routing;
10+
using Microsoft.Extensions.DependencyInjection;
1011

1112
namespace AspNetCore.Proxy
1213
{
@@ -31,6 +32,16 @@ public static Task ProxyAsync(this Controller controller, string uri, Func<HttpC
3132
return Helpers.HandleProxy(controller.HttpContext, uri, onFailure);
3233
}
3334

35+
/// <summary>
36+
/// Adds the required services needed for proxying requests.
37+
/// </summary>
38+
/// <param name="services">The application service collection.</param>
39+
/// <returns>The same instance.</returns>
40+
public static IServiceCollection AddProxies(this IServiceCollection services)
41+
{
42+
return services.AddHttpClient();
43+
}
44+
3445
/// <summary>
3546
/// Middleware which instructs the runtime to detect static methods with [<see cref="ProxyRouteAttribute"/>] and route them.
3647
/// </summary>

src/Test/UnitTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public class Startup
141141
public void ConfigureServices(IServiceCollection services)
142142
{
143143
services.AddRouting();
144+
services.AddProxies();
144145
services.AddMvc();
145146
}
146147

0 commit comments

Comments
 (0)