Skip to content

Edgecases

Patrick Klaeren edited this page Dec 11, 2022 · 3 revisions

Multiple lifetime registration

AutoRegisterInject will not allow multiple lifetime registration on the same type. ARI will select the first registration lifetime it finds on a type to generate the registration statement.

[RegisterScoped, RegisterSingleton]
public class Foo;

will result in

serviceCollection.AddScoped<Foo>();

Foo will not be registered as singleton. It is the opinion of ARI that this is not a valid scenario and it becomes an output based on first come, first served.

On the contrary, reordering to prioritise RegisterSingleton over RegisterScoped:

[RegisterSingleton, RegisterScoped]
public class Foo;

will result in

serviceCollection.AddSingleton<Foo>();

Partial class exception

In the unlikely event you do wish to register the same type as multiple lifetimes, Foo can be made partial with each partial declaration having its own registration.

[RegisterScoped]
public partial class Foo;
[RegisterSingleton]
public partial class Foo;

will result in

serviceCollection.AddScoped<Foo>();
serviceCollection.AddSingleton<Foo>();

Clone this wiki locally