在HttpContext中解析服务配置操作 [英] Resolve service in HttpContext configure Action

查看:63
本文介绍了在HttpContext中解析服务配置操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net核心中,我正在注册键入的 HttpClient ,我需要来自服务提供商的解决服务.我该如何解决那里的服务?

In asp.net core I am registering typed HttpClient and I need resolve service from service provider. How can I resolve services there?

示例:

services
    .AddHttpClient<TTypedHttpClient, TTypedHttpClientImpl>(httpClient =>
    {
        // how to resolve service on next line?
        var config = RESOLVE_SERVICE<IConfig>();
        httpClient.BaseAddress = config.GetBaseUrl();
    });

推荐答案

我假设实际的问题是,在注册类型化的HttpClient和HttpContext(特定HTTP请求的上下文)时,如何访问配置或任何其他服务?相关.

I assume the actual question is how to access configuration or any other service when registering a typed HttpClient and HttpContext (the context of a specific HTTP request) isn't relevant.

更新在.NET Core 2.1中,尽管文档显示了内容,但AddHttpClient并没有带有两个类型参数和 IServiceCollection 的重载.通过在AddHttpClient之后对 ConfigureHttpClient 进行校准,可以执行相同的工作.

UPDATE In .NET Core 2.1 AddHttpClient doesn't have an overload with two type parameters and aIServiceCollection despite what the documentation shows. The same job is performed by caling ConfigureHttpClient after AddHttpClient.

services
    .AddHttpClient<TTypedHttpClient, TTypedHttpClientImpl>()
    .ConfigureHttpClient(svc,httpClient =>
    {
        // how to resolve service on next line?
        var config = svc.GetRequiredService<IConfig>();
        httpClient.BaseAddress = config.GetBaseUrl();
    });

原始

您可以使用 AddHttpClient(IServiceCollection,String,Action< IServiceProvider,HttpClient>)重载,将 IServiceProvider 传递给可用于解析其他服务的操作:

You can use the AddHttpClient(IServiceCollection, String, Action< IServiceProvider,HttpClient>) overload to pass an IServiceProvider to the action that can be used to resolve other services :

services
    .AddHttpClient<TTypedHttpClient, TTypedHttpClientImpl>((svc,httpClient =>
    {
        // how to resolve service on next line?
        var config = svc.GetRequiredService<IConfig>();
        httpClient.BaseAddress = config.GetBaseUrl();
    });

我建议阅读Steve Gordon的有关类型化HttpClients和HttpClientFactory的文章系列.使用Singleton Services中的类型化客户端显示了如何使用 AddHttpClient 来配置客户端.他的其余文章展示了如何使用HttpClientFactory和Polly对其进行配置,以进行正确的缓冲和重试

I'd suggest reading Steve Gordon's article series on typed HttpClients and HttpClientFactory. Using Typed Clients from Singleton Services shows how to use AddHttpClient to configure the client. The rest of his articles show how to configure it with HttpClientFactory and Polly for correct pooling and retries

配置

Steve Gordon的示例使用IOption从配置基础结构读取数据.

Steve Gordon's example uses IOption to read the data from the configuration infrastrucure.

services.AddHttpClient<IConfigurationService, ConfigurationService>()
.ConfigureHttpClient((serviceProvider, client) =>
{
    var baseAddress = serviceProvider.GetRequiredService<IOptions<SdkOptions>>().Value.BaseAddress;
    client.BaseAddress = new Uri(baseAddress);
});

这是一个主意-HttpClient注册不应该也不应该依赖于其配置值的来源. IConfig 应该只不过是在配置阶段加载的配置DTO.

That's a good idea - HttpClient registration shouldn't know and shouldn't depend on the souce of its config values. IConfig should be nothing more than a configuration DTO loaded in the configuration phase.

.NET Core配置可以从json/xml/ini文件,数据库,外部服务,环境变量以及可以以"section:subsection"-value 形式返回值的任何内容中加载值.它还允许组合和重载设置,这可能非常有用.

.NET Core configuration can load values from json/xml/ini files, databases, external services, environment variables, anything that can return values in "section:subsection" - value form. It also allows combining and overloading settings, which can be very useful.

我有一些应用程序可以从文件(基本配置),其他团队管理的数据库(外部服务端点和凭据)和命令行(可能的替代)中加载设置.端点配置用于创建N个名为HttpClient的对象,每个端点一个.我已经使用此示例,以创建用于加载端点设置的EF Core提供程序.

I have applications that load settings from files (basic configuration), databases managed by other teams (external service endpoints and credentials) and the command line (possible overrides). The endpoint configurations are used to create N named HttpClients, one per endpoint. I've used this example to create an EF Core provider that loads the endpoint settings.

这也允许在应用程序启动时验证设置,这可能是一个很大的好处.没有它,在第一次请求特定的HttpClient之前,应用程序不会知道出了什么问题.

This allows validation of the settings when the application starts too, which can be a significant benefit. Without it, the application wouldn't know something's wrong until the first time a specific HttpClient was requested.

这篇关于在HttpContext中解析服务配置操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆