Share via


ITypedHttpClientFactory<TClient> Arabirim

Tanım

Belirli bir mantıksal ad için özel yapılandırmayla yazılan istemci örnekleri oluşturabilen bir bileşen için fabrika soyutlaması.

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

Tür Parametreleri

TClient

Oluşturulacak yazılan istemcinin türü.

Örnekler

Bu örnek, yazılan bir istemci sınıfını tanımlamaya yönelik temel deseni gösterir.

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

Bu örnek, bir ASP.NET Core ara yazılımından türlenmiş bir istemcinin nasıl tüketıldığını gösterir.

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

Bu örnek, bir ASP.NET Core MVC Denetleyicisinden türlenmiş bir istemcinin nasıl tüketıldığını gösterir.

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

Açıklamalar

ITypedHttpClientFactory<TClient> ve işlevlerini destekleyen AddHttpClient<TClient>(IServiceCollection, String)AddTypedClient<TClient>(IHttpClientBuilder) bir altyapıdır. Bu tür nadiren doğrudan uygulama kodunda kullanılmalıdır, bunun yerine yazılan istemcileri almak için kullanın GetService(Type) .

Bir varsayılanITypedHttpClientFactory<TClient>, çağrılarak AddHttpClient(IServiceCollection)bir IServiceCollection öğesine kaydedilebilir. Varsayılan değer ITypedHttpClientFactory<TClient> , tekil bir açık genel hizmet olarak hizmet koleksiyonuna kaydedilir.

Varsayılan değer ITypedHttpClientFactory<TClient> , yazılan istemci örnekleri oluşturmak için tür etkinleştirmeyi kullanır. Yazılan istemci türleri doğrudan öğesinden IServiceProvideralınmaz. Ayrıntılar için bkz. CreateInstance(IServiceProvider, Type, Object[]).

Yöntemler

CreateClient(HttpClient)

İlişkili HttpClientbir verilen yazılan bir istemci oluşturur.

Şunlara uygulanır