- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
HttpClientFactory 提供以下扩展方法:
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services, string name)
并且我创建了一个类型化的 HttpClient,如下所示:
public class CustomClient {
public CustomClient(HttpClient client,
CustomAuthorizationInfoObject customAuthorizationInfoObject) {
/// use custom authorization info to customize http client
}
public async Task<CustomModel> DoSomeStuffWithClient() {
/// do the stuff
}
}
我可以在程序的 ServiceCollection 中注册这个自定义客户端,如下所示:
services.AddTransient<CustomAuthorizationInfoObject>();
services.AddHttpClient<CustomClient>("DefaultClient");
然后我可以注册这个 CustomClient 的第二个实例,其中包含一些稍微改变的信息:
services.AddHttpClient<CustomClient>("AlternativeAuthInfo", (client) => {
client.DefaultRequestHeaders.Authorization = ...;
});
在程序的其他地方,我现在想要获得一个特定的命名 CustomClient
。这证明了障碍。
我可以通过向服务提供商请求 CustomClient
来获取最后添加到服务的任何 CustomClient
。
调用 IHttpClientFactory.CreateClient("AlternativeAuthInfo")
,例如,返回一个 HttpClient
,所以我无法访问 CustomClient 中的额外方法,并且没有那里似乎没有任何其他方法可以帮助我。
因此,我该如何着手获得命名的 CustomClient?或者我是否滥用了通过原始扩展方法命名和引用类型化客户端的机会?
最佳答案
我看到有一个 ITypedHttpClientFactory<>
可以包装常规 HttpClient
的界面在一个打字的。没有亲自使用过,但这是缺失的部分吗?
例如
/// grab the named httpclient
var altHttpClient = httpClientFactory.CreateClient("AlternativeAuthInfo");
/// get the typed client factory from the service provider
var typedClientFactory = serviceProvider.GetService<ITypedHttpClientFactory<CustomClient>>();
/// create the typed client
var altCustomClient = typedClientFactory.CreateClient(altHttpClient);
关于c# - HttpClientFactory - 通过名称获取命名的类型化客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201914/
我在 vscode 中使用带有 TypeScript 的 Svelte,并且在 vscode 中安装了 Svelte 扩展。 在我的 App.svelte 中有 // a bunch of co
我想延长go-validator返回更好的类型: type Error map[string][]error // Will output the first error when stringifi
在 python 中,您可以定义具有自动值的类型化枚举: import enum from enum import auto class Ordinals(enum.IntEnum): FIRST
我有一个 custom set我想在打字 Racket 中使用它。一世 要求它使用 require/typed与 #:opaque custom-set?操作说明。它工作,除了代码在运行时失败,当我
下面2种设置HttpClient的场景有什么区别吗? 我应该更喜欢一个吗? 输入客户端: public class CatalogService { private readonly Http
我正在尝试创建一个 dbTyped 和大小的 SqlParameters 数组。这工作正常,但如果我需要另一列,则会导致更改两个地方的代码。 SqlParameter[] parameters = {
我有一个用例,其中复杂的 UI 层次结构需要在 iframe 中呈现,但处理它的逻辑(创建、输入文档、事件处理、退出文档)需要在主框架/应用程序中。 我在获取对 iframe 的 Document 实
我最近将 VS 2005 升级到了 2010 年,对 LinQ 还很陌生。也许有人可以把我放在正确的方式。 背景 : 我有一个类型化数据集,并且使用 Table AccessRule 扩展了标准 SQ
我问这个只是为了澄清我的想法是否正确。 静态/动态类型如果变量的类型在编译时已知,则语言是静态类型的。这实际上意味着您作为程序员必须指定每个变量的类型。示例:Java、C、C++。 如果在运行时解释变
当我使用 CultureInfo Typed DataMember 调用我的 WCF 服务的方法时,它抛出 CommunicationException。 我该如何解决这个问题? The InnerE
我想将项目转换为字符串数组或用于填充 ListBox.DataSource 的类型。该类型已覆盖 ToString(),但我似乎无法将其转换,甚至无法转换为 String[]。 String[] a
如何获取/打印(键入的)查询后面的 JPQL 查询字符串,即设置之后参数? (例如,用于调试目的) 一个简单的 toString() 似乎并不能解决问题... 谢谢 最佳答案 没有“最终被翻译成最终
这是 Scala 2.8.0 beta 对这个问题的跟进: What is a proper way to manage flexible, typed, immutable data structu
我是一名优秀的程序员,十分优秀!