Nullable Microsoft.Toolkit.Mvvm Ioc.Default.GetService vs. GalaSoft.MvvmLight SimpleIoc.Default.GetInstance #3846
-
What's best practice for addressing
Looked at latest guidance on migration docs: https://github.com/windows-toolkit/MVVM-Samples/blob/01465e5d114441e0f50279853361670a4767e80b/docs/mvvm/MigratingFromMvvmLight.md cc: @Sergio0694, thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Or maybe I should just be using |
Beta Was this translation helpful? Give feedback.
-
Hey @myokeeh, sorry for the delay, missed the notification! 😄 The Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hey @myokeeh, sorry for the delay, missed the notification! 😄
The
GetService
method returnsnull
because that's the official contract from theIServiceProvider
interface (see here). I would imagine that it's not that MvvmLight wasn't returningnull
as well, it's more likely that it was just missing the nullable annotation, so you didn't see the return type as nullable, as you suggested. If you want to ensure the requested type is present and throw otherwise, then yeah I'd recommend usingGetRequiredService
instead, I added that extension specifically for this scenario 👍Hope this helps!