DialogFactory is Null
Environment
Product |
Dialog for Blazor, RootComponent for Blazor |
Description
The following exception may occur when calling the AlertAsync()
, ConfirmAsync()
, or PromptAsync()
methods of the Telerik Blazor DialogFactory
:
System.NullReferenceException: Object reference not set to an instance of an object
The following exception may occur on initial page load:
InvalidOperationException: Cannot provide a value for property '...' on type '...'. There is no registered service of type 'Telerik.Blazor.DialogFactory'
Cause
The null reference exception occurs if the DialogFactory
CascadingParameter
was not populated by the TelerikRootComponent
. This in turn indicates one of the following:
- The
TelerikRootComponent
is missing in the Blazor app. - The root component is present, but it's in the same
.razor
file as theDialogFactory
cascading parameter. - The Blazor app is using Per Page / Component Interactity Location and the root component is present, but it's in a static
.razor
component or layout.
The invalid operation exception occurs if the app tries to inject the DialogFactory
as a dependency injection (service), which is not the intended way to use the predefined Telerik dialogs.
Solution
The following technical requirements ensure correct DialogFactory
setup:
- Place a
TelerikRootComponent
in a layout.razor
file in the app when using the application types below. If theDialogFactory
must be avalable in the layout file itself, then place theTelerikRootComponent
in a parent layout file. - When using a Blazor Web App with Per Page / Component Interactity Location, place the
TelerikRootComponent
inside an interactive Razor component, which is a parent of the component that consumes theDialogFactory
. - Always consume the
DialogFactory
as a[CascadingParameter]
, and not as a dependency injection with@inject
.