Skip to content

Respect ServiceLifeTime during resolution #36050

Closed
@alexanderjung-sdxag

Description

@alexanderjung-sdxag

Currently it is not possible to register a service interface scoped and singleton with different implementations, e.g.

services.AddSingelton<IMyInterface, GlobalEngine>();
services.AddScoped<IMyInterface, ScopedEngine>();

A call to "sp.GetService()" on the global service provider will either raise
an exception ("InvalidOperationException: Cannot resolve scoped service '{0}' from root provider.")
or resolve ScopedEngine, depending on ServiceProviderOptions.ValidateScopes.

Also I have no way of working around that issue, since there is no way to ask the IServiceProvider whether it is scoped or not.

Best solution would be, if a resoluton on the global service provider ignored scoped definitions.
Next best solution would be some way to determine if the current solution provider is scoped or not.

PS: I realize that this is a corner case and I'm usually fine with restrictions - as long as I can find workarounds.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions