-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autofac behaves differently than the Microsoft ServiceProvider with lambda registrations that return null #60
Comments
Hey @scottt732, thanks for reporting this. I am not sure which constellation led to your error but what you are trying to do actually works the same as the service-provider created by Microsoft and is therefor indeed a full-replacement for it. Here the code I've used public class DepTests
{
private class SomeDependency {}
[Fact]
public void DependencyRegisteredResolved()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<SomeDependency>();
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(serviceCollection);
var container = containerBuilder.Build();
var autofacServiceProvider = new AutofacServiceProvider(container);
var service = autofacServiceProvider.GetService<SomeDependency>();
Assert.NotNull(service);
}
[Fact]
public void DependencyOptionalResolved()
{
var serviceCollection = new ServiceCollection();
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(serviceCollection);
var container = containerBuilder.Build();
var autofacServiceProvider = new AutofacServiceProvider(container);
var service = autofacServiceProvider.GetService<SomeDependency>();
Assert.Null(service);
}
} I've created a repository that you can clone and try to find out what went wrong for you. |
@alsami Note the first test in the @scottt732 example doesn't use Autofac: serviceCollection.AddTransient<ConcreteSampleService1>(sp => null);
var serviceProvider = serviceCollection.BuildServiceProvider();
var service = serviceProvider.GetService<ConcreteSampleService1>(); The issue here is that the Microsoft service provider apparently allows you to register a dependency that returns null while Autofac does not. I'm not sure this is something we actually want to fix, though, so I think there's still no action required.
Long story already too long, I think this is a "won't fix." If Microsoft adds this to their tests and requires all of the conforming container implementations to support it, we can revisit it at that time. |
Yeah missed that one thanks for the hint :) |
Thanks for the quick responses.
I just wanted to clarify a few points a bit above for future Googlers' sake. "the Microsoft service provider apparently allows you to register a dependency that returns null while Autofac does not." [Fact]
public void BuildAutofacServiceProvider_WhenLambdaServiceRegistrationViaAutofac1_UnfortunatelyThrowsException()
{
var containerBuilder = new ContainerBuilder();
containerBuilder.Register<ConcreteSampleService1>(cc => null);
var container = containerBuilder.Build();
var serviceProvider = new AutofacServiceProvider(container);
var service = serviceProvider.GetService<ConcreteSampleService1>();
// Fail --> throws DependencyResolutionException (see below)
Assert.Null(service);
} "The difference between [Fact]
public void BuildAutofacServiceProvider_WhenLambdaServiceRegistrationViaMs_ReturnsNullServiceReference()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<ConcreteSampleService1>(sp => null);
var serviceProvider = serviceCollection.BuildServiceProvider();
var service = serviceProvider.GetService<ConcreteSampleService1>();
Assert.Null(service);
Assert.Throws<InvalidOperationException>(() => serviceProvider.GetRequiredService<ConcreteSampleService1>());
}
|
Describe the bug
I'm not sure this is a bug or maybe something worth calling out in the documentation. I just wanted to bring it to your attention in case you're trying to be a 100% drop-in replacement for Microsoft's
IServiceProvider
implementation.I ran into a runtime exception resolving a service graph after introducing Autofac to an existing .NET Core app. It was a straightforward fix & a code smell in our codebase, but it was surprising nonetheless.
To reproduce
Reproduction of the issue, ideally in a unit test format.
Full exception with stack trace:
Assembly/dependency versions:
I was also able to reproduce this in Autofac 4.9.4 + Autofac.Extensions.DependencyInjection 4.4.0
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: