【发布时间】:2020-01-20 23:07:34
【问题描述】:
我有以下 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();
}
我已经尝试了几种不同的替代方法来注册 HttpClient 没有任何运气:
identityServerBuilder.Services.AddHttpClient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
services.AddHttpClient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
services.AddHttpClient<ResourceOwnerPasswordValidator>();
我总是收到以下错误:
在尝试激活“ResourceOwnerPasswordValidator”时无法解析“System.Net.Http.HttpClient”类型的服务
如果我自己在 ResourceOwnerPasswordValidator 类中实例化 HttpClient 一切正常,但我读过这样做是不好的做法,我想弄清楚为什么依赖注入不起作用。我已经能够在其他项目中使用类型化的 HttpClient 依赖注入,没有任何问题。
如果有帮助,here is the source IdentityServer4 扩展方法。
任何帮助将不胜感激。
【问题讨论】:
-
仅仅阅读一些不好的做法是不够的。要将某事标记为“不良做法”,您必须确定优点和缺点,并确定优点是否大于缺点。你不能简单地给某事贴上“不好的做法”的标签,因为有人在不理解原因的情况下这么说;沿着这条路走下去就是疯狂的货物崇拜。
-
@RobertHarvey 这是微软在其文档中推荐的方法。这是我查找的内容,因为我知道手动实例化 HttpClient 会导致几年前另一个 .NET 应用程序中的性能问题。 docs.microsoft.com/en-us/aspnet/core/fundamentals/… 无论如何,即使我自己简单地创建一个新实例是一个可行的解决方案,我仍然很想知道为什么推荐的方法不起作用。
-
那篇文章讨论了使用
IHttpClientFactory创建您的HttpClient实例。它只是顺便提到了依赖注入,只是说你可以做到,而不是你应该。
标签: c# asp.net-core dependency-injection identityserver4