From 04ecca4a691612e37d52cdad35266d0c27cd3711 Mon Sep 17 00:00:00 2001 From: dadhi Date: Thu, 21 Oct 2021 00:49:03 +0300 Subject: [PATCH] fixed: #432 --- ...ntationNotAssignableToServiceType_error.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue432_Resolving_interfaces_with_contravariant_type_parameter_fails_with_RegisteringImplementationNotAssignableToServiceType_error.cs diff --git a/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue432_Resolving_interfaces_with_contravariant_type_parameter_fails_with_RegisteringImplementationNotAssignableToServiceType_error.cs b/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue432_Resolving_interfaces_with_contravariant_type_parameter_fails_with_RegisteringImplementationNotAssignableToServiceType_error.cs new file mode 100644 index 00000000..e808d188 --- /dev/null +++ b/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue432_Resolving_interfaces_with_contravariant_type_parameter_fails_with_RegisteringImplementationNotAssignableToServiceType_error.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Microsoft.Extensions.DependencyInjection; + +namespace DryIoc.Microsoft.DependencyInjection.Specification.Tests +{ + [TestFixture] + public class GHIssue432_Resolving_interfaces_with_contravariant_type_parameter_fails_with_RegisteringImplementationNotAssignableToServiceType_error + { + [Test] + public void Test1() + { + var type = typeof(IValidator<>).MakeGenericType(typeof(ExtraSettings)); + var services = new ServiceCollection(); // Microsoft.Extensions.DependencyInjection + + services.AddSingleton, SettingsValidator>(); + + var container = new Container(); + var adaptedContainer = container.WithDependencyInjectionAdapter(services); // this throws + IServiceProvider serviceProvider = adaptedContainer; + + var validator = serviceProvider.GetService(type); + Assert.NotNull(validator); + } + + public class Settings + { + public string Address { get; set; } + } + + public class ExtraSettings : Settings + { + public string Key { get; set; } + } + + public interface IValidator + { + public bool IsValid(T settings); + } + + public abstract class AbstractValidator : IValidator + { + public bool IsValid(T settings) + { + return true; + } + } + + public class SettingsValidator : AbstractValidator + { + + } + + } +}