Skip to content

RegisterSingleton behaves inconsistently. #1008

@dotnetjunkie

Description

@dotnetjunkie

Considering the following example:

container.Register(typeof(Foo<>)); // class Foo<T> { }
container.RegisterSingleton(typeof(Bar<>)); // class Bar<T> { }
// Class Consumer<T>(Foo<T> f, Bar<T> b) { }
container.Register<Consumer<int>>();
container.Verify();

Verification fails complaining that Bar<int> is not registered.

Reason for this is that the RegisterSingleton overload called is RegisterSingleton(Type openGenericServiceType, params Assembly[] assemblies), which means that the following call is made:

container.RegisterSingleton(openGenericServiceType: typeof(Bar<>), assemblies: new Assembly[0]);

This results in the assembly scanning of non-generic implementations of Bar<T> within zero assemblies.

This actually costed me 10 minutes to figure out what was going on. If this happens to me... it can happen to anyone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions