Skip to content

Commit

Permalink
Use HttpClientFactory.
Browse files Browse the repository at this point in the history
  • Loading branch information
twitchax committed Mar 17, 2019
1 parent af34c2a commit aef3d25
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ Latest .NET Standard 2.0.

### Examples

First, you must add the required services.

```csharp
public void ConfigureServices(IServiceCollection services)
{
...
services.AddProxies();
...
}
```

#### Existing Controller

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

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

#### `ProxyRoute` Attribute

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

In your `Configure(IApplicationBuilder, IHostingEnvironment)` method, add the middleware.
First, add the middleware.

```csharp
app.UseProxies();
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseProxies();
...
}
```

Then, create a static method which returns a `Task<string>` or `string` (where the `string` is the URI to proxy).
Expand Down
3 changes: 2 additions & 1 deletion src/Core/AspNetCore.Proxy.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.4.1</Version>
<Version>2.0.0</Version>
<AssemblyName>AspNetCore.Proxy</AssemblyName>
<PackageId>AspNetCore.Proxy</PackageId>
<DocumentationFile>bin\AspNetCore.Proxy.xml</DocumentationFile>
Expand All @@ -10,5 +10,6 @@
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
</ItemGroup>
</Project>
7 changes: 6 additions & 1 deletion src/Core/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;

namespace AspNetCore.Proxy
Expand Down Expand Up @@ -84,7 +85,11 @@ private static HttpRequestMessage CreateProxyHttpRequest(this HttpContext contex
internal static Task<HttpResponseMessage> SendProxyHttpRequest(this HttpContext context, string proxiedAddress)
{
var proxiedRequest = context.CreateProxyHttpRequest(proxiedAddress);
return new HttpClient().SendAsync(proxiedRequest, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);

return context.RequestServices
.GetService<IHttpClientFactory>()
.CreateClient()
.SendAsync(proxiedRequest, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted);
}

internal static async Task CopyProxyHttpResponse(this HttpContext context, HttpResponseMessage responseMessage)
Expand Down
11 changes: 11 additions & 0 deletions src/Core/ProxyRouteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

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

/// <summary>
/// Adds the required services needed for proxying requests.
/// </summary>
/// <param name="services">The application service collection.</param>
/// <returns>The same instance.</returns>
public static IServiceCollection AddProxies(this IServiceCollection services)
{
return services.AddHttpClient();
}

/// <summary>
/// Middleware which instructs the runtime to detect static methods with [<see cref="ProxyRouteAttribute"/>] and route them.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Test/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public class Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
services.AddProxies();
services.AddMvc();
}

Expand Down

0 comments on commit aef3d25

Please sign in to comment.