Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify interface to support HttpClientFactory users #494

Merged
merged 18 commits into from Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,11 +1,9 @@
// <auto-generated />
using System;
using System.Net.Http;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
{{#UsingList}}
using {{Item}};
{{/UsingList}}
Expand Down Expand Up @@ -52,7 +50,6 @@ namespace {{Namespace}}
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly ConcurrentDictionary<string, Func<HttpClient, object[], object>> methodImpls = new ConcurrentDictionary<string, Func<HttpClient, object[], object>>();
readonly IRequestBuilder requestBuilder;

public AutoGenerated{{GeneratedClassSuffix}}(HttpClient client, IRequestBuilder requestBuilder)
Expand All @@ -70,7 +67,7 @@ namespace {{Namespace}}
{
{{#IsRefitMethod}}
var arguments = new object[] { {{ArgumentList}} };
var func = methodImpls.GetOrAdd({{#MethodTypeParameterNames}}${{/MethodTypeParameterNames}}"{{Name}}{{#MethodTypeParameterNames}}<{{.}}>{{/MethodTypeParameterNames}}({{ArgumentListWithTypes}})", _ => requestBuilder.BuildRestResultFuncForMethod("{{Name}}", new Type[] { {{ArgumentTypesList}} }{{#MethodTypeParameterList}}, new Type[] { {{.}} }{{/MethodTypeParameterList}}));
var func = requestBuilder.BuildRestResultFuncForMethod("{{Name}}", new Type[] { {{ArgumentTypesList}} }{{#MethodTypeParameterList}}, new Type[] { {{.}} }{{/MethodTypeParameterList}});
return ({{ReturnType}})func(Client, arguments);
{{/IsRefitMethod}}
{{^IsRefitMethod}}
Expand Down
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -579,3 +579,49 @@ Which can be used like this:
// than one type (unless you have a different domain for each type)
var api = RestService.For<IReallyExcitingCrudApi<User, string>>("http://api.example.com/users");
```

### Using HttpClientFactory

Refit has first class support for the ASP.Net Core 2.1 HttpClientFactory. Simply call the provided extension method in your `ConfigureServices` method to configure your Refit interface:

```csharp
services.AddRefitClient<IWebApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));
// Add additional IHttpClientBuilder chained methods as required here:
// .AddHttpMessageHandler<MyHandler>()
// .SetHandlerLifetime(TimeSpan.FromMinutes(2));
```

Optionally, a `RefitSettings` object can be included:
```csharp
var settings = new RefitSettings();
// Configure refit settings here

services.AddRefitClient<IWebApi>(settings)
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));
// Add additional IHttpClientBuilder chained methods as required here:
// .AddHttpMessageHandler<MyHandler>()
// .SetHandlerLifetime(TimeSpan.FromMinutes(2));
```
Note that some of the properties of `RefitSettings` will be ignored because the `HttpClient` and `HttpClientHandlers` will be managed by the `HttpClientFactory` instead of Refit.

You can then get the api interface using constructor injection:

```csharp
public class HomeController : Controller
{
public HomeController(IWebApi webApi)
{
_webApi = webApi;
}

private readonly IWebApi _webApi;

public async Task<IActionResult> Index(CancellationToken cancellationToken)
{
var thing = await _webApi.GetSomethingWeNeed(cancellationToken);

return View(thing);
}
}
```
172 changes: 75 additions & 97 deletions Refit.Tests/RefitStubs.Net46.cs

Large diffs are not rendered by default.

172 changes: 75 additions & 97 deletions Refit.Tests/RefitStubs.NetCore2.cs

Large diffs are not rendered by default.