`IServiceProviderIsService` not returning `true` for various registrations
See original GitHub issueI 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 multipleT
registeredIGenericService<T>
open genericConcreteClass
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:
- Created 10 months ago
- Comments:5 (2 by maintainers)
Top 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 >
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 Free
Top 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
Think this will be in a new Lamar 10.0 soon.
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