Unable to resolve service for type 'System.Collections.Generic.List`1[Microsoft.Extensions.Logging.ILoggerProvider]'
See original GitHub issueHi there!
First of all, thank you for developing this library. I’ve used it in many projects and it’s really convenient not having to register all services by hand.
In the current project, I am batteling a problem. I have a class which extends List<T>
:
public class PagedList<T> : List<T>
{
public int CurrentPage { get; }
public int TotalPages { get; }
// ...
Now whenever this class exists, I think what happens is, scrutor tries to register List<T>
in the DI container. This is not my intention and it fails obviously. Here is the beginning of the (long) stacktrace:
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostApplicationLifetime Lifetime: Singleton ImplementationType: Microsoft.Extensions.Hosting.Internal.ApplicationLifetime': Unable to resolve service for type 'System.Collections.Generic.List`1[Microsoft.Extensions.Logging.ILoggerProvider]' while attempting to activate 'icpms.Shared.Pagination.PagedList`1[Microsoft.Extensions.Logging.ILoggerProvider]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostLifetime Lifetime: Singleton ImplementationType: Microsoft.Extensions.Hosting.Internal.ConsoleLifetime': Unable to resolve service for type 'System.Collections.Generic.List`1[Microsoft.Extensions.Options.IConfigureOptions`1[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions]]' while attempting to activate 'icpms.Shared.Pagination.PagedList`1[Microsoft.Extensions.Options.IConfigureOptions`1[Microsoft.Extensions.Hosting.ConsoleLifetimeOptions]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHost Lifetime: Singleton ImplementationType: Microsoft.Extensions.Hosting.Internal.Host': Unable to resolve service for type 'System.Collections.Generic.List`1[Microsoft.Extensions.Logging.ILoggerProvider]' while attempting to activate 'icpms.Shared.Pagination.PagedList`1[Microsoft.Extensions.Logging.ILoggerProvider]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Logging.ILoggerFactory Lifetime: Singleton ImplementationType: Microsoft.Extensions.Logging.LoggerFactory': Unable to resolve service for type 'System.Collections.Generic.List`1[Microsoft.Extensions.Logging.ILoggerProvider]' while attempting to activate 'icpms.Shared.Pagination.PagedList`1[Microsoft.Extensions.Logging.ILoggerProvider]'.)
...
I am probably missing something obvoious here. How can I tell Scrutor to ignore PagedList<T>
within my project?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Unable to resolve ILogger from Microsoft.Extensions.Logging
public void ConfigureServices(IServiceCollection services) { var serviceProvider = services.BuildServiceProvider(); var logger = serviceProvider ...
Read more >Unable to resolve service for type microsoft.extensions ...
The Problem. A runtime error is triggered when using the Microsoft ILogger class in your .Net applications. System.InvalidOperationException: ' ...
Read more >Unable to resolve ILogger from Microsoft.Extensions.Logging ...
cs ): public void configureservices(iservicecollection services) { var serviceprovider = services.buildserviceprovider(); var logger = serviceprovider.
Read more >Unable to resolve service for type 'Microsoft.Extensions. ...
I created an ASP.NET Core 3.0 Web Application with the default template in Visual Studio 2019 Preview 2.2 and tried to inject an...
Read more >ASP.Net Core API Error (Unable to resolve service for type ' ...
System.InvalidOperationException : Unable to resolve service for type 'TapAPI.Models.TodoContext' while attempting to activate 'TapAPI.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
First, I wouldn’t recommend using
AddClasses
without any filter. It will definitely register way more than you need, which will cost both in terms of CPU and memory. Especially when usingpublicOnly: false
.I’m not at a computer right now, so it’s a bit hard to write the code, but essentially you want to enumerate through the service collection and check if the
ServiceType
is assignable toIEnumerable
.Hi @riscie!
I’m not sure what your setup looks like, but it looks like it’s registering a bit too much, yes. We’ve talked about adding additional filters, like
IEnumerable<T>
, but because it’s a breaking change, we haven’t done it yet. See https://github.com/khellang/Scrutor/issues/65.I suggest you supply a filter when calling
AddClasses
that will register only the types you’re after for now.