Azure Sql Database connection refused CultureNotFound

Harrison Wood 0 Reputation points
2024-01-13T02:05:42.88+00:00

I'm trying to connect my local API service to Azure Sql Database so I can start developement.
I've gone into the Azure Database connection string that they have provided and copied the following;

"ConnectionStrings": {   "AZURE_SQL_CONNECTIONSTRING": "Server=tcp:<Database.Server.Name>.database.windows.net,1433;Initial Catalog=<Database.Table>;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication=\"Active Directory Default\";" }

I did replace the

<Database.Server.Name>
<Database.Table>

With their correct values.

Here's the context initiation;

builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("AZURE_SQL_CONNECTIONSTRING")));

I can connect to the database via Sql Server Management Studio on my local machine, so I proved that my IP address has been added as an exception to connect to the database.

But when I try to connect via the API service using the connection string above I get this exception;
System.Globalization.CultureNotFoundException: 'Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')

Any help would be much appreciated.

Azure SQL Database
{count} votes

2 answers

Sort by: Most helpful
  1. Harrison Wood 0 Reputation points
    2024-01-13T03:04:30.5966667+00:00
    <PropertyGroup>
      <TargetFramework>net8.0</TargetFramework>
      <Nullable>enable</Nullable>
      <ImplicitUsings>enable</ImplicitUsings>
      <InvariantGlobalization>false</InvariantGlobalization>
    </PropertyGroup>
    

    Set it to false, and it now I get a different error, but at least I roughly know what the issue is now.
    Found in the csproj file.


  2. Pinaki Ghatak 3,905 Reputation points Microsoft Employee
    2024-03-11T14:26:31.76+00:00

    Hello @Harrison Wood

    The error message suggests that the globalization mode is set to invariant, which means that only the invariant culture is supported. This can cause issues when trying to connect to the database. To resolve this issue, you can try setting the globalization mode to auto in your application's configuration file.

    Here's an example of how you can do this in the appsettings.json file:

    {
      "ConnectionStrings": {
        "AZURE_SQL_CONNECTIONSTRING": "Server=tcp:.database.windows.net,1433;Initial Catalog=;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication=Active Directory Default;"
      },
      "Globalization": {
        "SupportedCultures": ["en-US"],
        "SupportedUICultures": ["en-US"],
        "RequestLocalizationOptions": {
          "DefaultRequestCulture": {
            "Culture": "en-US",
            "UICulture": "en-US"
          },
          "SupportedCultures": ["en-US"],
          "SupportedUICultures": ["en-US"]
        }
      }
    }
    

    In this example, we have added a Globalization section to the appsettings.json file, which specifies the supported cultures and UI cultures. We have also set the RequestLocalizationOptions to use the en-US culture by default. You can also try setting the globalization mode to auto in the web.config file, if you are using ASP.NET.

    0 comments No comments

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.