So I have ( and had ) this list of dependencies in my .NET core C# project :
... Many other dependencies
services.AddSingleton<ISessionService, SessionServiceProvider>();
services.AddSingleton<IAuthFetcher, AuthFetcher>();
... Many other dependencies
I did not have any relationship between AuthFetcher and SessionServiceProvider. And the code was working fine. Then I went ahead and I did this :
public AuthFetcher(ISomePreviousService somePreviousService,
ISessionService sessionService)
{
this.somePreviousService = somePreviousService;
this.sessionService = sessionService;
}
So in my class AuthFetcher I added a new dependency (ISessionService), I had not the need of modifying my startup class as both services were already declared and working fine.
Nevertheless, everything blows up now and I get a dependency injection error stating that :
Unable to resolve service for type 'ISessionService' while attempting to activate 'AuthFetcher'.
Source=Microsoft.Extensions.DependencyInjection
How can I have this problem now if everything was working before? What am I doing wrong here?
public class SessionServiceProvider : ISessionService
{
readonly IEnvironmentVariableProvider environmentVariableProvider;
public SessionServiceProvider(IEnvironmentVariableProvider environmentVariableProvider)
{
this.environmentVariableProvider = environmentVariableProvider;
}
}
By the way, SessionServiceProvider comes from a Nuget package, its not in my own code, but it works fine if I remove the relationship with AuthFetcher.
Full stacktrace :
System.InvalidOperationException
HResult=0x80131509
Message=Unable to resolve service for type 'ISessionService' while attempting to activate 'AuthFetcher'.
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
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.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.TryCreateEnumerable(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.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetServices[T](IServiceProvider provider)
at Carter.CarterExtensions.UseCarter(IApplicationBuilder builder, CarterOptions options)
at Session.Api.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in C:\session\Api\Startup.cs:line 57
So thanks to everyone who commented on this. It was a dumb mistake from my part. I had a class in the Nuget package and a class with the same name in my code. So when I imported the classes I mixed up both references to my own code and to the Nuget package. Really dumb mistake but hard to see. I lost 5 hours on this.
User contributions licensed under CC BY-SA 3.0