❔ Services collection life time

Hi guys, is it possible to register some services later when the application up and running ? I stuck into a scenario like this I have WPF or MAUI application that use signalR for realtime communication, the problem is the application requirement doesn't want to force users to login to connect to the signalR hub, they can do it later. And this is where the problem happen, if you register signalR when you boot up the app it will lack all the information about that user needed to connect to the hub because many scenarios users don't want to login first but they can do that when they want to This question is not about SignalR it's about the life time of registering services to ServiceCollection.
7 Replies
mtreit
mtreit2y ago
My first thought is to register a Lazy<T> using the overload that takes an initialization function to invoke when you actually need the value. @TotechsStrypper you could also register the IServiceCollection and just inject it into whatever class you later want to use to add to it later in the program.
TotechsStrypper
TotechsStrypperOP2y ago
Thanks you do you have any source link describes about this ?
mtreit
mtreit2y ago
I think it would just literally be something like this:
services.AddSingleton(services);
services.AddSingleton(services);
Then you could inject it where you need it:
public TestController(
ILogger<TestController> logger,
IServiceCollection collection)
{
_logger = logger;
_serviceCollection = collection;
_serviceCollection.AddSingleton<Test>(new Test());
}
public TestController(
ILogger<TestController> logger,
IServiceCollection collection)
{
_logger = logger;
_serviceCollection = collection;
_serviceCollection.AddSingleton<Test>(new Test());
}
I tried it and it seems to work fine
TotechsStrypper
TotechsStrypperOP2y ago
Ah I see let me try that out thanks you so much I think this is what I am looking for but what I see in the document this is for thread benefit
mtreit
mtreit2y ago
There are multiple reasons to use Lazy<T>, thread safety is one of them. If you need to defer assignment to some later point in time it's a good choice.
TotechsStrypper
TotechsStrypperOP2y ago
I have an idea
hubConnection = new HubConnectionBuilder()
.WithAutomaticReconnect()
.WithUrl(ChatConstants.LocalBaseUrl, options =>
{
options.HttpMessageHandlerFactory = (handler) =>
{
if (handler is HttpClientHandler clientHandler)
{
clientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
}
return handler;
};
options.AccessTokenProvider = () =>
{
return Task.FromResult(accessToken);
};

}).Build();
hubConnection = new HubConnectionBuilder()
.WithAutomaticReconnect()
.WithUrl(ChatConstants.LocalBaseUrl, options =>
{
options.HttpMessageHandlerFactory = (handler) =>
{
if (handler is HttpClientHandler clientHandler)
{
clientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
}
return handler;
};
options.AccessTokenProvider = () =>
{
return Task.FromResult(accessToken);
};

}).Build();
this is the object I wanna register If I use Scope life time it will create a new one when requested but no I just return null and all the place that using this service will have to check null
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server