Reputation: 2534
In my Blazor-server app, one of the injected services will perform some system checking and throw an exception if the checking failed. The plan is to use this exception to remind the programmers that a certain condition is needed for the app to run correctly. However, Blazor simply shows
Error: The circuit failed to initialize.
in the browser console, and the message of my exception is nowhere to be found. Also, Visual Studio does not break on my exception in debug mode with the default breaking settings, supposedly because Blazor caught my exception. Therefore the only way I found so far that could actually display my exception is to change the breaking setting to "break on all runtime exceptions", but I don't think this is good enough since the future programmers on this project might not know it.
So is there any way I can configure Blazor to display the startup exception details or to stop catching my exceptions? I've tried
services.AddServerSideBlazor(options => {
options.DetailedErrors = true;
});
but it's not helping. I don't think it's relevant to my case here.
Upvotes: 1
Views: 515
Reputation: 3063
One way of doing this is to add some custom code in your Startup.cs
file, right at the end of your Configure
method. By the point that the startup sequence gets that far, all your services should be registered including your testing service. To get access in the Configure
method, first you need to add the service type and a variable name to the method signature, and then call your methods on the service at the end of the method. See below:
public void Configure(IApplicationBuilder app, ...other things, ITestingService testService)
{
// all your other startups configs
testService.RunTestMethods();
}
the ITestingService
represents your service to do the checking. The DI framework will pick up that you need that service in the Configure
method and give you an instance of it, and then you can execute any methods that you need, throw or propagate exceptions and log them if needed, etc. the key here is that if you throw the exception before the end of the Configure
method, the app will not start, so make sure to get your logging on even if it's a console log to scold a developer in the CLI that they need to correct something.
Upvotes: 1