Skip to content

Commit

Permalink
fixed: #432
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Oct 20, 2021
1 parent 311ed1e commit 04ecca4
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<IValidator<ExtraSettings>, 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<in T>
{
public bool IsValid(T settings);
}

public abstract class AbstractValidator<T> : IValidator<T>
{
public bool IsValid(T settings)
{
return true;
}
}

public class SettingsValidator : AbstractValidator<Settings>
{

}

}
}

0 comments on commit 04ecca4

Please sign in to comment.