Even with the typed HttpClient registration in Startup: services.AddHttpClient<IMyCustomService, MyCustomService>(); I am having the following error:
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'WebApplication4.Services.MyCustomService'.
Steps to reproduce the behavior:
I woul like to have the injected dependency working.
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using 'C:\Users\mvrmoreira\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Development
Content root path: C:\Users\mvrmoreira\source\repos\WebApplication4\WebApplication4
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:5000/api/values
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Route matched with {action = "Get", controller = "Values"}. Executing action WebApplication4.Controllers.ValuesController.Get (WebApplication4)
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action WebApplication4.Controllers.ValuesController.Get (WebApplication4) in 22.2283ms
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'WebApplication4.Services.MyCustomService'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 576.0877ms 500 text/html; charset=utf-8

Entire project on GitHub: https://github.com/mvrmoreira/WebApplication4
Don't do this, AddHttpClient already handles the registration of the service for you in this scenario. You're overriding the registration of that service with your own, the http client registered by the AddHttpClient call isn't available in the global scope, hence the error you're getting.
https://github.com/mvrmoreira/WebApplication4/blob/master/WebApplication4/Startup.cs#L24
Nice! It worked.
Abraço,
Matheus Moreira
Em qui, 7 de fev de 2019 Ã s 13:51, Oliver Neal notifications@github.com
escreveu:
Don't do this, AddHttpClient already handles the registration of the
service for you in this scenario. You're overriding the registration of
that service with your own, the http client registered by the
AddHttpClient call isn't available in the global scope, hence the error
you're getting.https://github.com/mvrmoreira/WebApplication4/blob/master/WebApplication4/Startup.cs#L24
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/Extensions/issues/1079#issuecomment-461480818,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABRewYqCtcjDsyLsXg27cU63LWohOOF2ks5vLEuSgaJpZM4alkNU
.
Don't do this,
AddHttpClientalready handles the registration of the service for you in this scenario. You're overriding the registration of that service with your own, the http client registered by theAddHttpClientcall isn't available in the global scope, hence the error you're getting.
This wasn't clear to me when reading the documentation. Maybe is should state something like.
services.AddHttpClient<IYourService,YourService>();
// This also registers your servce, so the following line should be removed.
// services.AddTransient<IYourService,YourService>();
Most helpful comment
This wasn't clear to me when reading the documentation. Maybe is should state something like.