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
TelerikRootComponentis missing in the Blazor app. - The root component is present, but it's in the same
.razorfile as theDialogFactorycascading parameter. - The Blazor app is using Per Page / Component Interactity Location and the root component is present, but it's in a static
.razorcomponent 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
TelerikRootComponentin a layout.razorfile in the app when using the application types below. If theDialogFactorymust be avalable in the layout file itself, then place theTelerikRootComponentin a parent layout file. - When using a Blazor Web App with Per Page / Component Interactity Location, place the
TelerikRootComponentinside an interactive Razor component, which is a parent of the component that consumes theDialogFactory. - Always consume the
DialogFactoryas a[CascadingParameter], and not as a dependency injection with@inject.