Hello Huston ... We have a problem
But what is this error? Have you ever had that moment, in front of your debugger, when you look at the error message, remember what you’ve just done that last modification and … nothing. Nothing makes sense.
Just today I experienced such a moment. I’m currently working on a new application in .NET MAUI. I was making a generic class for managing the application’s pages. I was implementing this in a real case and the BOOM 💥 I get this error displayed on my Visual Studio :
System. Reflection. TargetInvocationException: ‘Exception has been thrown by the target of an invocation.’
I couldn’t figure out what it was. I’ve undone my latest changes on Git, but again, no change. So either it’s an earlier modification, or I’ve got a bigger problem.
If, like me, you do a lot of .NET, you’re used to deleting the bin and obj folders, which are often the cause of many problems. Of course, by reflex I try this trick: but still nothing. So this time I try to activate the debugger’s automatic shutdown to see exactly where the problem lies, hoping that it’s actually my code.
To do this, if you don’t know, open the Exception settings menu.
In this menu I search for the global exception System.Exception and check it.
I then run the application again in debug mode. And as you can see for yourself, the error is much more explicit!
So my automatic dependency injection was simply missing a reference:
application.builder.Services.AddScoped<IDiscussionRepository, FakeDiscussionRepository>();
So it’s no big deal, but it will have caused a lot of research and testing for something so simple. So if you too find yourself in a situation like this, don’t hesitate to apply this tip, which I hope will help you save a lot of time!
Hope to see you soon on my blog!