Skip to content

AddApexCharts registers ApexChartsServiceOptions as singleton #654

@mwerberacs

Description

@mwerberacs

Was running into an issue with the app I work on where users would occasionally see charts with the incorrect theme (light mode users see dark mode charts after a dark mode user used the app). Did a bunch of digging and realized it was because the global options were shared between all users -- because when using the AddApexCharts() extension method, the object is registered as a singleton service, meaning one instance is created at start-up and re-used for all connections.

I fixed this myself by replacing AddApexCharts with:

builder.Services
    .AddScoped(_ => new ApexChartsServiceOptions
    {
        GlobalOptions = new ApexChartBaseOptions
        {
            Theme = new()
            {
                Palette = PaletteType.Palette1,
                Mode = Mode.Light
            }
        }
    })
    .AddScoped<IApexChartService, ApexChartService>()
    .AddHttpClient<ApexChartService>();

Is registering the options as singleton within AddApexCharts an intentional choice? If not, it may be best to change it to scoped; if it is, it might be helpful to future users to at least document this design choice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions