Bagikan melalui


ITypedHttpClientFactory<TClient> Antarmuka

Definisi

Abstraksi pabrik untuk komponen yang dapat membuat instans klien yang diketik dengan konfigurasi kustom untuk nama logis tertentu.

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

Jenis parameter

TClient

Jenis klien yang dititik untuk dibuat.

Contoh

Sampel ini menunjukkan pola dasar untuk mendefinisikan kelas klien yang ditik.

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

Sampel ini menunjukkan cara menggunakan klien yang ditik dari 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);
    });
}

Sampel ini menunjukkan cara menggunakan klien yang diketik dari Pengontrol MVC Inti ASP.NET.

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

Keterangan

ITypedHttpClientFactory<TClient> adalah infrastruktur yang mendukung AddHttpClient<TClient>(IServiceCollection, String) fungsionalitas dan AddTypedClient<TClient>(IHttpClientBuilder) . Jenis ini jarang digunakan langsung dalam kode aplikasi, gunakan GetService(Type) sebagai gantinya untuk mengambil klien yang ditik.

Default ITypedHttpClientFactory<TClient> dapat didaftarkan dalam dengan IServiceCollection memanggil AddHttpClient(IServiceCollection). Default ITypedHttpClientFactory<TClient> akan didaftarkan dalam koleksi layanan sebagai layanan open-generic singleton.

Defaultnya ITypedHttpClientFactory<TClient> menggunakan aktivasi jenis untuk membuat instans klien yang ditik. Jenis klien yang ditik tidak diambil langsung dari IServiceProvider. Lihat CreateInstance(IServiceProvider, Type, Object[]) untuk detailnya.

Metode

CreateClient(HttpClient)

Membuat klien berjenis yang diberikan terkait HttpClient.

Berlaku untuk