❔ 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
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
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
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
More Posts
❔ So I'm hammering my head against this Lambda API I trying get workingSo I followed the instructions out of one of the articles I read on AWS Lambda C# API did the conver❔ Best architectural design choice?If I have multiple instances of multiple apps all working to do similar things, and they all need th✅ In WPF using a custom dialog service, how would i "validate" the dialog data before closing it?I have a `NewUserWindow`, whose data context is a `NewUserViewModel` and then i have a dialog servic❔ xamarin forms displaying text in 1 line with spacinghow can i distribute text that first is at the start of the line second is in the middle and third i✅ Should I use unity to create a app?Trying to make app, should I make it in unity? It'll be for creating characters and putting them on ❔ System.Linq.Expressions custom expressionHi guys! I'm parsing a custom language and building an Expression Tree based on it, but I came into❔ ✅ IndexOutOfRangeExceptionI just tried to code out a simple app that reads a .csv file (as per screenshot) and displays the c❔ Getting a variable from a GameObject without dragging and dropping it inForenote: For unity Is there any way to get a variable from a GameObject without dragging and droppi❔ ✅ Singleton, thread-safe logger with absolutely 0 runtime performance penalty when turned offI've got an application that lazily discovers its state-space, as it needs it. Carrying through a lo❔ Can i not call a non static Method in a interface?```cs public interface IRegistry<B> { public virtual void Initialize() { }