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

Failing to resolve service with covariant generic parameter for constrained implementation #638

Closed
SpinDOS opened this Issue Nov 30, 2018 · 4 comments

Comments

2 participants
@SpinDOS

SpinDOS commented Nov 30, 2018

I expect the following code to succeed returning only applicable instances of service, but it fails with SimpleInjector.ActivationException trying to construct open generic type. The error occures only with covariant open generic service. If I change IService to IService<in T> or IService<T>, everything works fine.

    public class Program
    {
        public class Base { }
        public class Derived : Base { }
        
        public interface IService<out T> { } // works without out or with in T
        public class ServiceImplBase<T> : IService<T> where T : Base { }
        public class ServiceImplDerived<T> : IService<T> where T : Derived { }
        
        public static void Main()
        {
            var service = typeof(IService<>);
            var implementations = new[] { typeof(ServiceImplBase<>), typeof(ServiceImplDerived<>) };
            
            var container = new Container();
            container.Collection.Register(service, implementations);
            container.Verify();

            Assert.DoesNotThrow(() => container.GetAllInstances<IService<Derived>>()); // Ok
            Assert.AreEqual(2, container.GetAllInstances<IService<Derived>>().Count()); // Ok
            
            Assert.DoesNotThrow(() => container.GetAllInstances<IService<Base>>()); // Error
            Assert.AreEqual(1, container.GetAllInstances<IService<Base>>().Count());
        }
    }

The code throws the following exception:

Unhandled Exception: NUnit.Framework.AssertionException: Expected: No Exception to be thrown
But was: <SimpleInjector.ActivationException: The request for type Program.ServiceImplDerived is invalid because it is an open generic type: it is only possible to instantiate instances of closed generic types. A generic type is closed if all of its type
parameters have been substituted with types that are recognized by the compiler.
Parameter name: serviceType ---> System.ArgumentException: The request for type Program.ServiceImplDerived is invalid because it is an open generic type: it is only possible to instantiate instances of closed generic types. A generic type is closed if all of
its type parameters have been substituted with types that are recognized by the compiler.
Parameter name: serviceType
at SimpleInjector.Container.GetRegistrationEvenIfInvalid(Type serviceType, InjectionConsumerInfo consumer, Boolean autoCreateConcreteTypes)
at SimpleInjector.Internals.ContainerControlledCollection1.GetInstanceProducerThroughUnregisteredTypeResolution(Type implementationType) at SimpleInjector.Internals.ContainerControlledCollection1.GetOrCreateInstanceProducer(Type implementationType)
at SimpleInjector.Internals.ContainerControlledCollection1.<>c__DisplayClass33_0.<ToLazyInstanceProducer>b__0() at System.Lazy1.CreateValue()
at System.Lazy1.LazyInitValue() at System.Lazy1.get_Value()
at SimpleInjector.Internals.ContainerControlledCollection1.<>c.<SimpleInjector.Internals.IContainerControlledCollection.GetRelationships>b__27_0(Lazy1 p)
at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Linq.Enumerable.<SelectManyIterator>d__233.MoveNext()
at System.Linq.Enumerable.d__641.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)
at SimpleInjector.Internals.ContainerControlledCollection1.SimpleInjector.Internals.IContainerControlledCollection.GetRelationships() at SimpleInjector.Decorators.DecoratorHelpers.ContainerControlledCollectionRegistration.GetRelationshipsCore() at SimpleInjector.InstanceProducer.GetRelationships() at SimpleInjector.Diagnostics.Analyzers.LifestyleMismatchAnalyzer.<>c.<Analyze>b__8_0(InstanceProducer producer) at System.Linq.Enumerable.<SelectManyIterator>d__233.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at System.Linq.Buffer1..ctor(IEnumerable1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at SimpleInjector.Diagnostics.Analyzers.LifestyleMismatchAnalyzer.Analyze(IEnumerable1 producers)
at SimpleInjector.InstanceProducer.Analyze()
at SimpleInjector.InstanceProducer.BuildExpressionInternal()
at System.Lazy1.CreateValue() at System.Lazy1.LazyInitValue()
at System.Lazy`1.get_Value()
at SimpleInjector.InstanceProducer.BuildInstanceCreator()
at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()
at SimpleInjector.InstanceProducer.GetInstance()
--- End of inner exception stack trace ---
at SimpleInjector.InstanceProducer.GetInstance()
at SimpleInjector.Container.GetInstanceForRootTypeTService
at SimpleInjector.Container.GetInstanceTService
at SimpleInjector.Container.GetAllInstancesTService
at ConsoleApplication.Program.<>c__DisplayClass5_0.

b__1() in C:\Users\Alexander\Desktop\ConsoleApplication\Program.cs:line 28
at NUnit.Framework.Constraints.ThrowsConstraint.VoidInvocationDescriptor.Invoke() in C:\src\nunit\nunit\src\NUnitFramework\framework\Constraints\ThrowsConstraint.cs:line 266
at NUnit.Framework.Constraints.ThrowsConstraint.ExceptionInterceptor.Intercept(Object invocation) in C:\src\nunit\nunit\src\NUnitFramework\framework\Constraints\ThrowsConstraint.cs:line 178>

at NUnit.Framework.Assert.ReportFailure(String message) in C:\src\nunit\nunit\src\NUnitFramework\framework\Assert.cs:line 394
at NUnit.Framework.Assert.ReportFailure(ConstraintResult result, String message, Object[] args) in C:\src\nunit\nunit\src\NUnitFramework\framework\Assert.cs:line 382
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args) in C:\src\nunit\nunit\src\NUnitFramework\framework\Assert.That.cs:line 247
at NUnit.Framework.Assert.DoesNotThrow(TestDelegate code) in C:\src\nunit\nunit\src\NUnitFramework\framework\Assert.Exceptions.cs:line 217
at ConsoleApplication.Program.Main() in C:\Users\Alexander\Desktop\ConsoleApplication\Program.cs:line 28


@dotnetjunkie

This comment has been minimized.

Collaborator

dotnetjunkie commented Nov 30, 2018

This seems to be a bug. I'll investigate and report back when I have more information on this issue.

@dotnetjunkie

This comment has been minimized.

Collaborator

dotnetjunkie commented Nov 30, 2018

This is absolutely a bug. I'm sorry you had to run into it, but thank you for creating a very clear repro. Much appreciated.

If been able to reproduce this issue both with the latest (v4.4) release and v.3.0. This means this is not a regression. I'm still in the dark on why this is happening in the first place. It might take a few days for me to come with a resolution for this unfortunate bug.

dotnetjunkie added a commit that referenced this issue Dec 1, 2018

@dotnetjunkie

This comment has been minimized.

Collaborator

dotnetjunkie commented Dec 1, 2018

I pushed patch release v4.4.2 to NuGet. That release fixes this bug.

@SpinDOS

This comment has been minimized.

SpinDOS commented Dec 2, 2018

Thanks for your great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment