Del via


ITypedHttpClientFactory<TClient> Interface

Definition

A factory abstraction for a component that can create typed client instances with custom configuration for a given logical name.

generic <typename TClient>
public interface class ITypedHttpClientFactory
public interface ITypedHttpClientFactory<TClient>
type ITypedHttpClientFactory<'Client> = interface
Public Interface ITypedHttpClientFactory(Of TClient)

Type Parameters

TClient

The type of typed client to create.

Examples

This sample shows the basic pattern for defining a typed client class.

class ExampleClient
{
    private readonly HttpClient _httpClient;
    private readonly ILogger _logger;

    // typed clients can use constructor injection to access additional services
    public ExampleClient(HttpClient httpClient, ILogger<ExampleClient> logger)
    {
        _httpClient = httpClient;
        _logger = logger;
    }

    // typed clients can expose the HttpClient for application code to call directly
    public HttpClient HttpClient => _httpClient;

    // typed clients can also define methods that abstract usage of the HttpClient
    public async Task SendHelloRequest()
    {
        var response = await _httpClient.GetAsync("/helloworld");
        response.EnsureSuccessStatusCode();
    }
}

This sample shows how to consume a typed client from an ASP.NET Core middleware.

// in Startup.cs
public void Configure(IApplicationBuilder app, ExampleClient exampleClient)
{
    app.Run(async (context) =>
    {
        var response = await _exampleClient.GetAsync("/helloworld");
        await context.Response.WriteAsync("Remote server said: ");
        await response.Content.CopyToAsync(context.Response.Body);
    });
}

This sample shows how to consume a typed client from an ASP.NET Core MVC Controller.

// in Controllers/HomeController.cs
public class HomeController : ControllerBase(IApplicationBuilder app, ExampleClient exampleClient)
{
    private readonly ExampleClient _exampleClient;

    public HomeController(ExampleClient exampleClient)
    {
        _exampleClient = exampleClient;
    }

    public async Task<IActionResult> Index()
    {
        var response = await _exampleClient.GetAsync("/helloworld");
        var text = await response.Content.ReadAsStringAsync();
        return Content("Remote server said: " + text, "text/plain");
    };
}

Remarks

The ITypedHttpClientFactory<TClient> is infrastructure that supports the AddHttpClient<TClient>(IServiceCollection, String) and AddTypedClient<TClient>(IHttpClientBuilder) functionality. This type should rarely be used directly in application code, use GetService(Type) instead to retrieve typed clients.

A default ITypedHttpClientFactory<TClient> can be registered in an IServiceCollection by calling AddHttpClient(IServiceCollection). The default ITypedHttpClientFactory<TClient> will be registered in the service collection as a singleton open-generic service.

The default ITypedHttpClientFactory<TClient> uses type activation to create typed client instances. Typed client types are not retrieved directly from the IServiceProvider. See CreateInstance(IServiceProvider, Type, Object[]) for details.

Methods

CreateClient(HttpClient)

Creates a typed client given an associated HttpClient.

Applies to