❔ Properly calling "await login()" in web service constructor
Hello. Kinda noob, but I am looking for the best way to call a async login function for an external library in a web service constructor. I posted my code below. I want to use this service in DI singleton and make sure the _client.ConnectAsync() is called once correctly. AFAIK this approach would require me to call EnsureInitializedAsync(); everytime I use this service in my api controller. I just want to know if this is the best approach, or if there is something easier to use. Thank you!
6 Replies
are you guaranteed that
InitializeAsync
has already completed? If so then you don't need to call EnsureInitialized
Thanks for the reply. not sure exactly what you mean. If the Connect method wasnt asynchronous I would just throw it in the constructor directly, and then as a singleton Connect would just be called once on program start automatically, correct? I'm pretty much just asking how to replicate that behaviour. Since it is asynchronous and contructor cant call asynchronous methods directly, this is the workaround to do it. The only problem now is that I have to call InitializeAsync somewhere. So if im in the web controller, instead of just accessing SSService with DI and _client would be connected already, I have to make sure that it is connected by calling EnsureInitializedAsync. - If connected, do nothing, if not InitializeAsync will be called automatically. If there is another place where I can just call InitializeAsync once on program start, that would be ideal instead of calling EnsureInitializedAsync everytime SSService is used. Hopefully I am clear enough, because again i'm kind of a beginner so just trying to wrap my head around this. Thanks!
the standard approach to this is to hide the normal ctor for this class and only have a static async factory function
e.g.
re: DI, i don't think microsoft's framework supports async initialization. so you have to do something like this
^ that, or use the factory pattern. I wasn't sure if you were just injecting the service with DI or using some other method. I've been digging into razor recently and likely what I would do is
for just asp.net web server, you can achieve this without a factory since you can separate initialize and run
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
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.