Aspnetcore: [Components, Blazor] Better error handling when setting up the DI container

Created on 20 Feb 2019  路  4Comments  路  Source: dotnet/aspnetcore

Is your feature request related to a problem? Please describe.

When injecting a component with a service that hasn't been setup correctly in the DI container (using Startup.ConfigureServices()), a blank page will be rendered for the affected component. No user-facing error is shown.

[ In addition, if the project is a Razor Components project, no exception will be logged anywhere even for the developer (for a Blazor / client-side project, an exception will be logged in the browser console). This will presumably improve as part of #7165 / #4964. ]

As a simple test case:

  1. create a fresh Razor Components or Blazor project
  2. change Startup.ConfigureServices() in the components project to add a singleton:
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<MyService>();
        }
  1. generate this class MyService. It can be left empty.
  2. inject MyService into Counter.cshtml: @inject MyService service
  3. launch this in the browser, and navigate to Counter. Confirm that this works.
  4. change Startup.ConfigureServices() to do nothing.
  5. launch this in the browser again. The initial page will render fine, but Counter will now yield an empty page.

Describe the solution you'd like

This should raise an exception.

Where applicable, the developer exception page or a user-friendly error page should be shown.

Additional context

  • The original scenario that brought this up was slightly more complicated (a service in turn depended on a second-level missing service), and thus the root issue was significantly harder to track down.

  • 5150 may also be related, but as of Blazor 0.8, I'm not getting a missing service exception at all.

Working area-blazor bug

All 4 comments

Thanks for contacting us, @chucker.
@rynowak, is this expected, or should we indeed throw?

If we throw you would get a blank screen. I'm not sure we've done any work to make the error experience reasonable yet so we should hang on to this feedback for now.

Hi @chucker - we plan to make this better in the immediate term and at least log an error to the console.

We have a more extensive long term plan to make this better as well: https://github.com/aspnet/AspNetCore/issues/8613 - but for the next release we will be fixing annoying issues like what you reported. Thanks!

HI @chucker

I've tested this out on the latest build and the current state of this is the following:

Blazor - an error is logged to the browser console.
Razor Components (Prerendering) - The developer exception page shows up. We fixed this for preview 4
Razor Components (without Prerendering) - an error is logged to the browser console.

We have some longer term plans to provide a better experience as part of #8613.

I'm closing this because a browser console error is currently what we expect to happen in preview 4.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rmarinho picture rmarinho  路  78Comments

pekkah picture pekkah  路  200Comments

danroth27 picture danroth27  路  130Comments

moodya picture moodya  路  153Comments

zorthgo picture zorthgo  路  136Comments