Microsoft.Extensions.Http
An options class for configuring the default .
Gets a list of operations used to configure an .
Gets a list of operations used to configure an .
Gets or sets the length of time that a instance can be reused. Each named
client can have its own configured handler lifetime value. The default value of this property is two minutes.
Set the lifetime to to disable handler expiry.
The default implementation of will pool the
instances created by the factory to reduce resource consumption. This setting configures the amount of time
a handler can be pooled before it is scheduled for removal from the pool and disposal.
Pooling of handlers is desirable as each handler typially manages its own underlying HTTP connections; creating
more handlers than necessary can result in connection delays. Some handlers also keep connections open indefinitly
which can prevent the handler from reacting to DNS changes. The value of should be
chosen with an understanding of the application's requirement to respond to changes in the network environment.
Expiry of a handler will not immediately dispose the handler. An expired handler is placed in a separate pool
which is processed at intervals to dispose handlers only when they become unreachable. Using long-lived
instances will prevent the underlying from being
disposed until all references are garbage-collected.
A builder abstraction for configuring instances.
The is registered in the service collection as
a transient service. Callers should retrieve a new instance for each to
be created. Implementors should expect each instance to be used a single time.
Gets or sets the name of the being created.
The is set by the instructure
and is public for unit testing purposes only. Setting the outside of
testing scenarios may have unpredictable results.
Gets or sets the primary .
Gets a list of additional instances used to configure an
pipeline.
Creates an .
An built from the and
.
Used by the to apply additional initialization to the configure the
immediately before
is called.
Applies additional initialization to the
A delegate which will run the next .
A factory abstraction for a component that can create typed client instances with custom
configuration for a given logical name.
The type of typed client to create.
The is infrastructure that supports the
and
functionality. This type
should rarely be used directly in application code, use instead
to retrieve typed clients.
A default can be registered in an
by calling .
The default will be registered in the service collection as a singleton
open-generic service.
The default uses type activation to create typed client instances. Typed
client types are not retrieved directly from the . See
for details.
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");
};
}
Creates a typed client given an associated .
An created by the for the named client
associated with .
An instance of .
The '{0}' must not contain a null entry.
The '{0}' must not contain a null entry.
The '{0}' property must be null. '{1}' instances provided to '{2}' must not be reused or cached.{3}Handler: '{4}'
The '{0}' property must be null. '{1}' instances provided to '{2}' must not be reused or cached.{3}Handler: '{4}'
The '{0}' must not be null.
The '{0}' must not be null.
The handler lifetime must be at least 1 second.
The handler lifetime must be at least 1 second.
Extension methods for configuring an
Adds a delegate that will be used to configure a named .
The .
A delegate that is used to configure an .
An that can be used to configure the client.
Adds a delegate that will be used to configure a named .
The .
A delegate that is used to configure an .
An that can be used to configure the client.
Adds a delegate that will be used to create an additional message handler for a named .
The .
A delegate that is used to create a .
An that can be used to configure the client.
The delegate should return a new instance of the message handler each time it
is invoked.
Adds a delegate that will be used to create an additional message handler for a named .
The .
A delegate that is used to create a . /// An that can be used to configure the client.
The delegate should return a new instance of the message handler each time it
is invoked.
Adds an additional message handler from the dependency injection container for a named .
The .
An that can be used to configure the client.
The type of the . The handler type must be registered as a transient service.
Adds a delegate that will be used to configure the primary for a
named .
The .
A delegate that is used to create an .
An that can be used to configure the client.
The delegate should return a new instance of the message handler each time it
is invoked.
Adds a delegate that will be used to configure the primary for a
named .
The .
A delegate that is used to create an .
An that can be used to configure the client.
The delegate should return a new instance of the message handler each time it
is invoked.
Configures the primary from the dependency inection container
for a named .
The .
An that can be used to configure the client.
The type of the . The handler type must be registered as a transient service.
Adds a delegate that will be used to configure message handlers using
for a named .
The .
A delegate that is used to configure an .
An that can be used to configure the client.
Configures a binding between the type and the named
associated with the .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The .
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Calling will register a typed
client binding that creates using the .
Configures a binding between the type and the named
associated with the . The created instances will be of type
.
The declared type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The implementation type of the typed client. The type specified by will be instantiated by the
.
The .
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Calling
will register a typed client binding that creates using the
.
Configures a binding between the type and the named
associated with the .
The type of the typed client. They type specified will be registered in the service collection as
a transient service.
The .
A factory function that will be used to construct the typed client.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Calling
will register a typed client binding that creates using the provided factory function.
Configures a binding between the type and the named
associated with the .
The type of the typed client. They type specified will be registered in the service collection as
a transient service.
The .
A factory function that will be used to construct the typed client.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Calling
will register a typed client binding that creates using the provided factory function.
Sets the length of time that a instance can be reused. Each named
client can have its own configured handler lifetime value. The default value is two minutes. Set the lifetime to
to disable handler expiry.
The default implementation of will pool the
instances created by the factory to reduce resource consumption. This setting configures the amount of time
a handler can be pooled before it is scheduled for removal from the pool and disposal.
Pooling of handlers is desirable as each handler typially manages its own underlying HTTP connections; creating
more handlers than necessary can result in connection delays. Some handlers also keep connections open indefinitly
which can prevent the handler from reacting to DNS changes. The value of should be
chosen with an understanding of the application's requirement to respond to changes in the network environment.
Expiry of a handler will not immediately dispose the handler. An expired handler is placed in a separate pool
which is processed at intervals to dispose handlers only when they become unreachable. Using long-lived
instances will prevent the underlying from being
disposed until all references are garbage-collected.
Extensions methods to configure an for .
Adds the and related services to the .
The .
The .
Adds the and related services to the and configures
a named .
The .
The logical name of the to configure.
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
Use as the name to configure the default client.
Adds the and related services to the and configures
a named .
The .
The logical name of the to configure.
A delegate that is used to configure an .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
Use as the name to configure the default client.
Adds the and related services to the and configures
a named .
The .
The logical name of the to configure.
A delegate that is used to configure an .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
Use as the name to configure the default client.
Adds the and related services to the and configures
a binding between the type and a named . The client name
will be set to the full name of .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Use as the name to configure the default client.
Adds the and related services to the and configures
a binding between the type and a named . The client name will
be set to the type name of .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The implementation type of the typed client. They type specified will be instantiated by the
The .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Adds the and related services to the and configures
a binding between the type and a named .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The .
The logical name of the to configure.
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Use as the name to configure the default client.
Adds the and related services to the and configures
a binding between the type and a named . The client name will
be set to the type name of .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The implementation type of the typed client. They type specified will be instantiated by the
The .
The logical name of the to configure.
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Use as the name to configure the default client.
Adds the and related services to the and configures
a binding between the type and a named . The client name will
be set to the type name of .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The .
A delegate that is used to configure an .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Adds the and related services to the and configures
a binding between the type and a named . The client name will
be set to the type name of .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The implementation type of the typed client. They type specified will be instantiated by the
The .
A delegate that is used to configure an .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Use as the name to configure the default client.
Adds the and related services to the and configures
a binding between the type and a named .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The .
The logical name of the to configure.
A delegate that is used to configure an .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Use as the name to configure the default client.
Adds the and related services to the and configures
a binding between the type and a named .
The type of the typed client. They type specified will be registered in the service collection as
a transient service. See for more details about authoring typed clients.
The implementation type of the typed client. They type specified will be instantiated by the
The .
The logical name of the to configure.
A delegate that is used to configure an .
An that can be used to configure the client.
instances that apply the provided configuration can be retrieved using
and providing the matching name.
instances constructed with the appropriate
can be retrieved from (and related methods) by providing
as the service type.
Use as the name to configure the default client.
A builder for configuring named instances returned by .
Gets the name of the client configured by this builder.
Gets the application service collection.
Extensions methods for .
Creates a new using the default configuration.
The .
An configured using the default configuration.
A factory abstraction for a component that can create instances with custom
configuration for a given logical name.
A default can be registered in an
by calling .
The default will be registered in the service collection as a singleton.
Creates and configures an instance using the configuration that corresponds
to the logical name specified by .
The logical name of the client to create.
A new instance.
Each call to is guaranteed to return a new
instance. Callers may cache the returned instance indefinitely or surround
its use in a using block to dispose it when desired.
The default implementation may cache the underlying
instances to improve performance.
Callers are also free to mutate the returned instance's public properties
as desired.