ITypedHttpClientFactory<TClient> 인터페이스

정의

지정된 논리적 이름에 대한 사용자 지정 구성을 사용하여 형식화된 클라이언트 인스턴스를 만들 수 있는 구성 요소에 대한 팩터리 추상화입니다.

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

형식 매개 변수

TClient

만들 형식화된 클라이언트의 형식입니다.

예제

이 샘플에서는 형식화된 클라이언트 클래스를 정의하기 위한 기본 패턴을 보여 줍니다.

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

이 샘플에서는 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);
    });
}

이 샘플에서는 ASP.NET Core MVC 컨트롤러에서 형식화된 클라이언트를 사용하는 방법을 보여줍니다.

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

설명

ITypedHttpClientFactory<TClient>AddTypedClient<TClient>(IHttpClientBuilder) 기능을 지원하는 AddHttpClient<TClient>(IServiceCollection, String) 인프라입니다. 이 형식은 애플리케이션 코드에서 직접 사용되지 않아야 합니다. 대신 를 사용하여 GetService(Type) 형식화된 클라이언트를 검색합니다.

기본값 ITypedHttpClientFactory<TClient> 은 를 호출AddHttpClient(IServiceCollection)하여 에 IServiceCollection 등록할 수 있습니다. 기본값 ITypedHttpClientFactory<TClient> 은 서비스 컬렉션에 단일 오픈 제네릭 서비스로 등록됩니다.

기본값 ITypedHttpClientFactory<TClient> 은 형식 활성화를 사용하여 형식화된 클라이언트 인스턴스를 만듭니다. 형식화된 클라이언트 형식은 에서 IServiceProvider직접 검색되지 않습니다. 자세한 내용은 CreateInstance(IServiceProvider, Type, Object[])를 참조하세요.

메서드

CreateClient(HttpClient)

연결된 HttpClient를 지정하는 형식화된 클라이언트를 만듭니다.

적용 대상