❔ 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
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.Thanks you do you have any source link describes about this ?
I think it would just literally be something like this:
Then you could inject it where you need it:
I tried it and it seems to work fine
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
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.I have an idea
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
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.