Reputation: 3188
I try using the best approach to use services in Catel, which is to inject services in the viewmodel's constructor:
public MyViewModel(IMessageService msgService)
{
Argument.IsNotNull(() => msgService);
this.messageService = msgService;
//stuff
}
Yet when I put arguments in this constructor, my program throws an NullReferenceException, supposedly at the Argument.IsNoNull line (debugging through Catel's code seems to be a pain). When I remove the argument or the IsNotNull validation, it launches fine.
So I do not receive a IMessageService, msgService is always null. What am I doing wrong? Missing assembly?
EDIT: It seems like the IMessageService type is registered. I manage to get a reference by using ResolveType:
messageService = ServiceLocator.Default.ResolveType<IMessageService>();
So as far as my project is concerned, the problem is solved. But it doesn't answer the original question: why isn't dependency injection working?
Upvotes: 0
Views: 169
Reputation: 649
The code for Argument can be found on Git, The usage is correct, i fear you have to check the injection. Try to set a breakpoint in the constructor and go through the code until it reaches the breakpoint.
Are you using a special DI-Framework? Yes - then check the registration/setup otherwise find the code that composes the ViewModel
I've just seen in the documentation that the types are set up automatically. Is it possible to create the object-graph for your service (are there any circular dependencies, or dependencies that cannot be created due to own dependencies or not matching constructors?)
Upvotes: 0
Reputation: 5724
If you get a null, it means the dependency cannot be resolved. If you are using the default ServiceLocator of Catel, enable logging to see what's going on under the hood in your application startup:
#if DEBUG
LogManager.AddDebugListener();
#endif
You can also check if the type is registered by using this code:
var serviceLocator = ServiceLocator.Default;
serviceLocator.IsTypeRegistered<IMessageService>();
Upvotes: 3
Reputation: 76
Have you tried this?
Argument.IsNotNull("msgService", msgService);
In the documentation video they do similar with Argument.ArgumentIsNullOrWhitespace
Upvotes: 0