question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to resolve service for type 'System.Collections.Generic.List`1[Microsoft.Extensions.Logging.ILoggerProvider]'

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
khellangcommented, Apr 1, 2020

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 using publicOnly: 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 to IEnumerable.

1reaction
khellangcommented, Apr 1, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found