C# 如何将HttpClient依赖项注入与IdentityServer4一起使用?

C# 如何将HttpClient依赖项注入与IdentityServer4一起使用?,c#,asp.net-core,dependency-injection,identityserver4,C#,Asp.net Core,Dependency Injection,Identityserver4,我有以下IResourceOwnerPasswordValidator的实现: public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator { private HttpClient HttpClient { get; set; } public ResourceOwnerPasswordValidator(HttpClient httpClient) { th

我有以下IResourceOwnerPasswordValidator的实现:

public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{
    private HttpClient HttpClient { get; set; }

    public ResourceOwnerPasswordValidator(HttpClient httpClient)
    {
        this.HttpClient = httpClient;
    }
}
我在Startup.cs中的ConfigureServices中有以下代码:

public void ConfigureServices(IServiceCollection services)
{
    var identityServerBuilder = services.AddIdentityServer();

    identityServerBuilder.Services.AddHttpClient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

    identityServerBuilder
        .AddInMemoryIdentityResources(ConfigurationData.IdentityResources)
        .AddInMemoryApiResources(ConfigurationData.ApiResources)
        .AddInMemoryClients(ConfigurationData.Clients)
        .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()
        .AddProfileService<ProfileService>();

    // not recommended for production - you need to store your key material somewhere secure
    identityServerBuilder.AddDeveloperSigningCredential();
}
public void配置服务(IServiceCollection服务)
{
var identityServerBuilder=services.AddIdentityServer();
identityServerBuilder.Services.AddHttpClient();
标识服务器生成器
.AddInMemoryIdentityResources(配置数据.IdentityResources)
.AddInMemoryApiResources(ConfigurationData.ApiResources)
.AddInMemoryClient(ConfigurationData.Client)
.AddResourceOwnerValidator()
.AddProfileService();
//不建议用于生产-您需要将关键材料存放在安全的地方
identityServerBuilder.AddDeveloperSigningCredential();
}
我已经尝试了几种不同的方法来替代注册HttpClient的线路,但没有成功:

identityServerBuilder.Services.AddHttpClient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

services.AddHttpClient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

services.AddHttpClient<ResourceOwnerPasswordValidator>();
identityServerBuilder.Services.AddHttpClient();
services.AddHttpClient();
services.AddHttpClient();
我总是遇到以下错误:

尝试激活“ResourceOwnerPasswordValidator”时,无法解析类型“System.Net.Http.HttpClient”的服务

如果我自己在ResourceOwnerPasswordValidator类中实例化HttpClient,那么一切都正常工作,但我已经读到这样做是不好的做法,我想找出依赖注入不起作用的原因。我已经能够在其他项目中使用类型化的HttpClient依赖项注入,没有任何问题

如果有帮助,请参阅IdentityServer4扩展方法


如果您能提供任何帮助,我们将不胜感激。

仅仅读到一些不好的做法是不够的。要给某件事贴上“坏习惯”的标签,你必须找出优点和缺点,并决定优点是否大于缺点。你不能简单地给某件事贴上“坏习惯”的标签,因为有人在不理解原因的情况下这么说;沿着这条路走下去的是疯狂的货物崇拜。@RobertHarvey这是微软在他们的文档中推荐的方法。这是我查找的内容,因为我知道多年前手动实例化HttpClient是另一个.NET应用程序中出现性能问题的原因。在任何情况下,即使只是自己创建一个新实例是一个可行的解决方案,我仍然想知道为什么推荐的方法不起作用。那篇文章讨论了使用
IHttpClientFactory
创建
HttpClient
实例。它只是顺便提到依赖注入,只是说你可以做,而不是说你应该做。仅仅读到一些不好的做法是不够的。要给某件事贴上“坏习惯”的标签,你必须找出优点和缺点,并决定优点是否大于缺点。你不能简单地给某件事贴上“坏习惯”的标签,因为有人在不理解原因的情况下这么说;沿着这条路走下去的是疯狂的货物崇拜。@RobertHarvey这是微软在他们的文档中推荐的方法。这是我查找的内容,因为我知道多年前手动实例化HttpClient是另一个.NET应用程序中出现性能问题的原因。在任何情况下,即使只是自己创建一个新实例是一个可行的解决方案,我仍然想知道为什么推荐的方法不起作用。那篇文章讨论了使用
IHttpClientFactory
创建
HttpClient
实例。它只是顺便提到依赖注入,并且只说您可以做到,而不是说您应该做到。
identityServerBuilder.AddResourceOwnerValidator<ResourceOwnerPasswordValidator>();
builder.Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
// register the IResourceOwnerPasswordValidator as an HTTP client
services.AddHttpClient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

// do not register anther IResourceOwnerPasswordValidator
services.AddIdentityServer()
    .AddInMemoryIdentityResources(ConfigurationData.IdentityResources)
    .AddInMemoryApiResources(ConfigurationData.ApiResources)
    .AddInMemoryClients(ConfigurationData.Clients)
    .AddProfileService<ProfileService>();