Unable to resolve service for type 'System.Net.Http.HttpClient'
我创建了一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class ProductsViewComponent : ViewComponent { private readonly HttpClient _client; public ProductsViewComponent(HttpClient client) { _client = client ?? throw new ArgumentNullException(nameof(client)); } public async Task;lt;IViewComponentResult;gt; InvokeAsync(string date) { using(var response = await _client.GetAsync($"/product/get_products/{date}")) { response.EnsureSuccessStatusCode(); var products = await response.Content.ReadAsAsync;lt;List;lt;Products;gt;;gt;(); return View(products); } } } |
我收到此错误:
InvalidOperationException: Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate MyApp.ViewComponents.ProductsViewComponent'
我以这种方式将
1 2 3 4 | services.AddHttpClient;lt;FixturesViewComponent;gt;(options =;gt; { options.BaseAddress = new Uri("http://80.350.485.118/api/v2"); }); |
更新:
我也注册了
我有一个类似的问题-问题出在双重注册中:
1 2 | services.AddHttpClient;lt;Service;gt;(); services.AddSingleton;lt;Service;gt;(); // fixed by removing this line |
TLDR;
经过一段漫长的聊天,之后又无法重现您的问题,我们最初确定观察到的问题是特定于
但是,在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddHttpClient<SomeService>(); // ... } // SomeService.cs public class SomeService { public SomeService(HttpClient httpClient) { // ... } } // SomeViewComponent.cs public class SomeViewComponent { public SomeViewComponent(SomeService someService) { // ... } } |
正如我已经说过的,这种方法行之有效-ASP.NET Core DI系统非常高兴创建
要重述原始问题,请使用以下示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public void ConfigureServices(IServiceCollection services) { services.AddHttpClient<SomeViewComponent>(); // ... } public class SomeViewComponent { public SomeViewComponent(HttpClient httpClient) { // ... } } |
在这种情况下,ASP.NET Core DI系统由于无法解析
有趣的是,以下内容也适用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public void ConfigureServices(IServiceCollection services) { services.AddHttpClient<SomeViewComponent>(); // ... } public class SomeViewComponent { public SomeViewComponent(IHttpClientFactory httpClientFactory) { var httpClient = httpClientFactory.CreateClient("SomeViewComponent") // ... } } |
在此示例中,我们利用了对
为了能够将
1 2 3 4 5 6 | public void ConfigureServices(IServiceCollection services) { services.AddMvc(...) .AddViewComponentsAsServices(); // ... } |
也可以调用
如果我们更仔细地研究文档,很明显那里没有示例将
不幸的是,我对ASP.NET Core DI系统了解不足,无法确切解释其工作原理:上面提供的信息仅说明了解决方案。克里斯·普拉特(Chris Pratt)在Github中打开了一个问题,要求对文档进行更新以扩展此范围。
我在
Microsoft.Extensions.DependencyInjection.Abstractions: Unable to
resolve service for type 'System.Net.Http.IHttpClientFactory' while
attempting to activate
'OServiceBus.Adapter.FetchDataFromSubscription1'
问题是我没有重写Configure函数以将
使用Microsoft.Azure.Functions.Extensions.DependencyInjection;
使用Microsoft.Extensions.DependencyInjection;
1 2 3 4 5 6 7 8 | [assembly: FunctionsStartup(typeof(ServiceBus.Adapter.Startup))] namespace ServiceBus.Adapter { public class Startup: FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddHttpClient(); } } } |
添加后,我的功能开始正常工作。希望能帮助到你。
看来您将两个视图组件混在一起了。您正在将
将HttpClient注册更改为
1 2 3 4 | services.AddHttpClient<ProductsViewComponent>(options => { options.BaseAddress = new Uri("http://80.350.485.118/api/v2"); }); |
我有一个类似的错误消息,试图将外部REST服务的包装器作为接口注入控制器。我需要在ConfigureServices中更改以下内容:
1 2 3 4 | services.AddHttpClient;lt;IMyServiceWrapper;gt;("MyServiceWrapper", client =;gt; { client.BaseAddress = new Uri("http://some_service/api"); } |
至
1 2 3 4 | services.AddHttpClient;lt;IMyServiceWrapper, MyServiceWrapper;gt;("MyServiceWrapper", client =;gt; { client.BaseAddress = new Uri("http://some_service/api"); } |
为了能够在我的控制器的构造函数中使用该接口:
1 2 3 4 | public MyController(IMyServiceWrapper myService) { _myService = myService; } |
对于使用模拟服务测试myController很有用。