I have a KeyedService, sometimes injected as Singleton if the page is anonymous, and other times as Scoped if the page requires authentication.
builder.Services.AddKeyedSingleton<IClientService, ClientService>(Constants.Singleton) ;
builder.Services.AddKeyedScoped<IClientService, ClientService>(Constants.Scoped);
within the ClientService
implementation, I would like to know which ServiceKey
was used to retrieve the instance, so that I change the details of the code within the constructor accordingly.
I'm not finding any way that I can get the value of the ServiceKey
that was used to obtain the ClientService
instance. Anyone have any ideas?
ps, just in case anyone is reading, this post is not the same question.
I have a KeyedService, sometimes injected as Singleton if the page is anonymous, and other times as Scoped if the page requires authentication.
builder.Services.AddKeyedSingleton<IClientService, ClientService>(Constants.Singleton) ;
builder.Services.AddKeyedScoped<IClientService, ClientService>(Constants.Scoped);
within the ClientService
implementation, I would like to know which ServiceKey
was used to retrieve the instance, so that I change the details of the code within the constructor accordingly.
I'm not finding any way that I can get the value of the ServiceKey
that was used to obtain the ClientService
instance. Anyone have any ideas?
ps, just in case anyone is reading, this post is not the same question.
Share Improve this question edited Feb 6 at 0:27 pthomson asked Feb 6 at 0:22 pthomsonpthomson 7851 gold badge7 silver badges13 bronze badges 7- If implementation types are different, you can check the type. Although the whole thing smells of antipatterns to me. – Tanveer Badar Commented Feb 6 at 7:47
- thanks for trying to help Tanveer, but you did not understand the question. You should be able to tell in the two lines of code provided that the implementation types are the same. It's the whole point of the question. – pthomson Commented Feb 6 at 14:22
- which is why I added "antipattern", but you do you. – Tanveer Badar Commented Feb 7 at 10:06
- the main antipattern here is the entire Microsoft provided KeyedService solution as a whole, using magic strings to identify and request the services. – pthomson Commented Feb 7 at 17:42
- You know you can inject http context and check for current user principal and if it is unauthenticated, right? That's why asking for service key and deciding how to proceed is the antipattern. – Tanveer Badar Commented Feb 7 at 18:04
1 Answer
Reset to default 2ServiceProvider does not natively expose the key to the service instance. A workaround here is to add key parameter when create the service so you could access.
public interface IClientService
{
string GetKey();
}
public class ClientService : IClientService
{
private readonly string _key;
public ClientService(string key)
{
_key = key;
}
public string GetKey() => _key;
}
Register
builder.Services.AddKeyedSingleton<IClientService, ClientService>("key1", (sp, key) => new ClientService("key1"));
builder.Services.AddKeyedSingleton<IClientService, ClientService>("key2", (sp, key) => new ClientService("key2"));