C
C#13mo ago
Rob

✅ WPF

Im using a navigation service that uses MVVM community toolkit and dependency injection and im trying to pass an object from one view model to another view model but I keep getting this error. If I do manage to pass it how can I use the items in the viewmodel that recieves it?
7 Replies
HimmDawg
HimmDawg13mo ago
Seems like your service isn't being registered correctly
public static object GetRequiredService(this IServiceProvider provider, Type serviceType)
{
ThrowHelper.ThrowIfNull(provider);
ThrowHelper.ThrowIfNull(serviceType);

if (provider is ISupportRequiredService requiredServiceSupportingProvider)
{
return requiredServiceSupportingProvider.GetRequiredService(serviceType);
}

object? service = provider.GetService(serviceType);
if (service == null)
{
throw new InvalidOperationException(SR.Format(SR.NoServiceRegistered, serviceType));
}

return service;
}
public static object GetRequiredService(this IServiceProvider provider, Type serviceType)
{
ThrowHelper.ThrowIfNull(provider);
ThrowHelper.ThrowIfNull(serviceType);

if (provider is ISupportRequiredService requiredServiceSupportingProvider)
{
return requiredServiceSupportingProvider.GetRequiredService(serviceType);
}

object? service = provider.GetService(serviceType);
if (service == null)
{
throw new InvalidOperationException(SR.Format(SR.NoServiceRegistered, serviceType));
}

return service;
}
this is the implementation of GetRequiredService. It throws that InvalidOperationException when GetServices returns null. Which is the case when no service is being found
Florian Voß
Florian Voß13mo ago
the object details param of the constructor ClientDetailsViewModel cannot be resolved when Microsofts DI/IoC container tries to invoke this Constructor. You haven't registered anything of type object in your serviceprovider so either take this param away or register something that matches the param's type in the service provider. I suggest if you choose the later option that you use a type thats more strict/concrete than object
Rob
Rob13mo ago
So how would I pass in the details to the ClientDetailsViewModel and use it in that class?
Florian Voß
Florian Voß13mo ago
via a param in the constructor, just like you did. The issue is you havent registered a Service that matches it's type in your service provider. So register one
Rob
Rob13mo ago
Could you give me a visual representation of how I would register said service to take in an arbitrary parameter. Sorry I'm still new to all of this.
Florian Voß
Florian Voß13mo ago
I see an issue with wanting to take an arbitary parameter of type object here, which is why I suggested to use a more concrete type. something of type object isn't very useful unless you cast it to something more concrete somewhere down the calling stack. But then you might as well just choose that more concrete type for the dependency injection in the first place 🤷‍♂️ I've never injected something using the object type and I dont know how that would look either or if thats even possible tbh. But thinking about this, the reason why you chose an to accept an arbitary parameter in the first place is because you wanted to the viewmodel X to be able to pass anything to the viewmodel Y right? how about injecting viewmodel X into viewmodel Y, not using object type but using ViewModelX type instead. Then one viewmodel can just take what it needs from the other one using composition just register both the viewmodels in the DI container. Then take one of the viewmodels in the ctor of the other one as a dependency and use it with composition I’ve never seen Registration of a service in DI container using object type personally
Rob
Rob13mo ago
Thanks a lot 😄
Want results from more Discord servers?
Add your server
More Posts