Condividi tramite


ITypedHttpClientFactory<TClient> Interfaccia

Definizione

Astrazione della factory per un componente che può creare istanze del client tipizzato con una configurazione personalizzata per un nome logico specificato.

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

Parametri di tipo

TClient

Tipo di client tipizzato da creare.

Esempio

Questo esempio illustra il modello di base per la definizione di una classe client tipizzata.

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();
    }
}

Questo esempio illustra come usare un client tipizzato da un middleware ASP.NET Core.

// 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);
    });
}

Questo esempio illustra come usare un client tipizzato da un controller MVC di ASP.NET Core.

// 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");
    };
}

Commenti

Infrastruttura ITypedHttpClientFactory<TClient> che supporta le AddHttpClient<TClient>(IServiceCollection, String) funzionalità e AddTypedClient<TClient>(IHttpClientBuilder) . Questo tipo deve essere usato raramente direttamente nel codice dell'applicazione, usare GetService(Type) invece per recuperare i client tipizzato.

Un valore predefinito ITypedHttpClientFactory<TClient> può essere registrato in un IServiceCollection oggetto chiamando AddHttpClient(IServiceCollection). Il valore predefinito ITypedHttpClientFactory<TClient> verrà registrato nella raccolta di servizi come servizio open-generic.

Il valore predefinito ITypedHttpClientFactory<TClient> usa l'attivazione del tipo per creare istanze client tipizzata. I tipi di client tipizzato non vengono recuperati direttamente dall'oggetto IServiceProvider. Per informazioni dettagliate, vedere CreateInstance(IServiceProvider, Type, Object[]).

Metodi

CreateClient(HttpClient)

Crea un client tipizzato in base a un oggetto HttpClient associato.

Si applica a