Why doesn’t .NET’s DI system understand generic interface variance?

Consider this example:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddTransient<IHook<SpecificEvent>, SpecificEventHook>();
builder.Services.AddTransient<IHook<IEvent>, GeneralEventHook>();

var app = builder.Build();

var hooks = app.Services.GetServices<IHook<SpecificEvent>>();
Console.WriteLine(hooks.Length); // 1 (bummer...)

public interface IEvent;

public interface IHook<in TEvent> where TEvent : IEvent
{
    void On(TEvent @event);
}

public record SpecificEvent(int Foo) : IEvent;

public class SpecificEventHook : IHook<SpecificEvent>
{
    public void On(SpecificEvent @event)
    {

    }
}
public class GeneralEventHook : IHook<IEvent>
{
    public void On(IEvent @event)
    {

    }
}

When I try to resolve a list of IHook<SpecificEvent>s from the service provider, I was expecting to get the GeneralEventHook (which is a IHook<IEvent>) as well, since IHook‘s generic parameter is contravariant (in TEvent); and therefore, an IHook<IEvent>, is, in fact, assignable to an IHook<SpecificEvent>.

But it seems like the standard .NET dependency injection system does not support this. There’s surprisingly very little information on the web about this type of scenario.

I’m curious why? Isn’t there a way to customize the DI container to achieve this? If not, what would be a reasonable workaround for this type of requirement?

You did not register GeneralEventHook as IHook<SpecificEvent>, and dependency injection will only return for types that were used during registration.

Similairly, if you would have:

public interface IService;
public class Service : IService { }

and you would register it as self:

services.AddScoped<Service>();

DI will fail to resolve IService.

In order to achieve what you want, you need to regsiter GeneralEventHook as IHook<SpecificEvent>:

builder.Services.AddTransient<IHook<SpecificEvent>, GeneralEventHook>();

This method will understand your contravariance allowing for such registration.

The .NET Dependency Injection system doesn’t integrate with generic interface variance because it’s designed for simplicity and performance, using exact type matching rather than considering variance relationships.
To work around this, you have a few options:

Create a custom resolution method that combines both specific and general hooks.
Use the Adapter pattern to wrap general hooks as specific ones or you can
Implement an event aggregator that manages all hooks and dispatches events.

For example

public static IEnumerable<IHook<TEvent>> GetAllHooks<TEvent>(this IServiceProvider services)
    where TEvent : IEvent
{
    var specificHooks = services.GetServices<IHook<TEvent>>();
    var generalHooks = services.GetServices<IHook<IEvent>>();
    return specificHooks.Concat(generalHooks);
}

// Usage:
var hooks = app.Services.GetAllHooks<SpecificEvent>();

This approach maintains the existing DI setup while allowing you to retrieve both specific and general hooks when needed.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật