Blazor Server Side Custom CultureInfo getting overridden when using AzureSignalR

C-Jesse Bryan 0 Reputation points
2024-06-25T23:30:39.3+00:00

I have a Blazor interactive server side .net 8 application. I wanted to change the default DateTimeFormat across the entire application for certain Cultures, so I created some custom CultureInfo's shown here:

public static List<CultureInfo> CustomCultures { get; set; } = new List<CultureInfo>()
{
    new CultureInfo("en-US", false) { DateTimeFormat = { ShortDatePattern = "MM/dd/yyyy" } },
    new CultureInfo("fr-CA", false) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd" } }
};

I then added those cultures to our supported cultures in Program.cs

public static void AddPortalLocalization(this WebApplicationBuilder builder)

{
    builder.Services.Configure<RequestLocalizationOptions>(options =>
    {
        var supportedCultures = CommonConstantsAndReadOnlys.CustomCultures;
        options.DefaultRequestCulture = new RequestCulture(CommonConstantsAndReadOnlys.CustomCultures.First());
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
    });
    builder.Services.AddLocalization();
}

I also have a dropdown selector in the application which lets users choose their culture, and it adds a cookie to the browser:

public IActionResult SetCulture(string cultureName, string redirectUri)
{
    var customCulture = CommonConstantsAndReadOnlys.CustomCultures.FirstOrDefault(c => c.Name == cultureName);
    if (customCulture != null)
    {
        HttpContext.Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(customCulture)));
    }
    return LocalRedirect(redirectUri);
}

I notice when running the application it uses my custom culture for half a second, then updates and goes back to the default CultureInfo properties for whatever culture i'm using. I am using AzureSignalR and set it up in Program.cs as shown here:

public static void ConfigureSharedSignalR(
   this WebApplicationBuilder builder,
    string? connectionString = null)
{
    connectionString = string.IsNullOrWhiteSpace(connectionString) ?
        builder.Configuration.GetConnectionString("SharedSignalR") : connectionString;
    builder.Services.AddSignalR().AddAzureSignalR(connectionString);
}  

When I remove

.AddAzureSignalR(connectionString);

the custom cultures that I want to use work correctly and don't get overridden. Why does adding AzureSignalR override my custom cultures?
Also, is there a better/easier way to set my own default DateTimeFormat's that don't use CultureInfo across the application?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,009 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,636 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
151 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.