RadzenTheme Dispose null reference exception

From time to time I get a null reference exception from RadzenTheme.Dispose(). Here's the exception:

fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HNA3JDUNOT88", Request id "0HNA3JDUNOT88:00000001": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
at Radzen.Blazor.RadzenTheme.Dispose()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.DisposeAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Http.Features.RequestServicesFeature.g__Awaited|9_0(RequestServicesFeature servicesFeature, ValueTask vt)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.g__ProcessEvents|242_0(HttpProtocol protocol, Stack`1 events)
Microsoft.AspNetCore.Server.Kestrel: Error: Connection id "0HNA3JDUNOT88", Request id "0HNA3JDUNOT88:00000001": An unhandled exception was thrown by the application.

System.NullReferenceException: Object reference not set to an instance of an object.
at Radzen.Blazor.RadzenTheme.Dispose()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.DisposeAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Http.Features.RequestServicesFeature.g__Awaited|9_0(RequestServicesFeature servicesFeature, ValueTask vt)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.g__ProcessEvents|242_0(HttpProtocol protocol, Stack`1 events)
The program 'dotnet' has exited with code 0 (0x0).

When I look at the code of RadzenTheme.cs in GitHub I see 2 possible null reference possibilities: ThemeService is null or persistingSubscription is null.

        /// <summary>
        /// Releases all resources used by the component.
        /// </summary>
        public void Dispose()
        {
            ThemeService.ThemeChanged -= OnThemeChanged;
            persistingSubscription.Dispose();
        }

Don't know why this could ever happen but I've added a check to prevent it: RadzenTheme could throw a null reference exception during dispose in … · radzenhq/radzen-blazor@011e761 · GitHub

persistingSubscription is a struct so it can't be null. ThemeService is injected and should have always had a value. I guess RadzenTheme is being disposed shortly after created.

No, I don't think it is immediatly disposed after creation. It takes a while to happen. If I leave the screen alone for let's say a few minutes the exception suddenly happens. I'll try and pinpoint the moment it happens.

I added the code below to the program.cs and it solves the null reference exception.

builder.Services.AddSingleton<RadzenTheme>(provider =>
{
    var theme = new RadzenTheme();
    return theme;
});

This code isn't needed as nothing should inject RadzenTheme (unless you do so for some reason). RadzenTheme is also a Blazor component and should be registered as a service.

The only thing in the Dispose method that could be null is ThemeService. It is already registered via builder.Services.AddRadzenComponents(). For some reason the Dispose method is called before the ThemeService instance is injected to RadzenTheme.

When is this patch going to be released?

It is already live: Release 5.9.7 · radzenhq/radzen-blazor · GitHub

1 Like