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.

`IServiceProviderIsService` not returning `true` for various registrations

See original GitHub issue

I was just playing using Lamar with minimal APIs and I’ve found there are various cases where injected services aren’t being inferred because Lamar’s IServiceProviderIsService implementation isn’t return true for various cases:

  • IEnumerable<T> where multiple T registered
  • IGenericService<T> open generic
  • ConcreteClass when not explicitly registered

Sample app demonstrating the issue below. Note that in all cases, add [FromServices] fixes the issue - it’s not the registration at issue, it’s the call to IServiceProviderIsService that’s wrong. This repros on .NET 6 and .NET 7, giving the following error (indicating none of the args were resolved from DI)

InvalidOperationException: Body was inferred but the method does not allow inferred body parameters.
Below is the list of parameters that we found:

Parameter | Source
---------------------------------------------------------------------------------
services  | Body (Inferred)
concrete  | UNKNOWN
generic   | UNKNOWN

I haven’t done an extensive search for all the cases that are/aren’t found, but these seem to be the main culprits

Thanks!

using Lamar;
using Lamar.Microsoft.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseLamar(services =>
{
    services.Scan(s =>
    {
        s.AssemblyContainingType(typeof(Program));
        s.WithDefaultConventions();
        s.AddAllTypesOf<IService>();
    });
});

var app = builder.Build();

app.MapGet("/", (IEnumerable<IService> services,
    ConcreteClass concrete,
    IGenericService<ConcreteClass> generic) => "Hello World!");

app.Run();

public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { }

public class ConcreteClass {}

public interface IGenericService<T> {}
public class GenericService<T> {}

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jeremydmillercommented, Dec 1, 2022

Think this will be in a new Lamar 10.0 soon.

1reaction
jeremydmillercommented, Nov 15, 2022

Ugh, @andrewlock I didn’t realize that was part of their “spec” for how that was supposed to work. Okay. This is one of those times when I curse the conforming container behavior that’s rammed down our throats

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does IServiceProvider.GetServices<T>() always returns ...
Short answer is yes since internally GetServices* extension methods resolves IEnumerable<T> same as in constructors that have IEnumerable<T> ...
Read more >
Add ability to detect if a service is registered in the DI ...
Open generics supported by the container will always return true. e.g.: void Tests(IServiceProvider sp) { // Assuming the ...
Read more >
Resolving concrete service type does not work in ...
Trying to resolve a concrete type with dependencies does not work when I pass the IServiceProvicer in as an argument to the Invoke-method....
Read more >
Dependency injection guidelines - .NET
When designing services for dependency injection: Avoid stateful, static classes and members. Avoid creating global state by designing apps ...
Read more >
Dependency injection in ASP.NET Core
Registration of the dependency in a service container. ASP.NET Core provides a built-in service container, IServiceProvider.
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