Sqlclient: Consistently fails to open a conenction on windows when Globalization.Invariant is set to true

Created on 22 Jul 2020  路  9Comments  路  Source: dotnet/SqlClient

Describe the bug

When I try to use SqlClient when Globalization.Invariant is true., It fails to open a sql connection (tried with mssql localdb)

If you are seeing an exception, include the full exceptions details (message and stack trace).

Exception message: System.NotSupportedException: Globalization Invariant Mode is not supported.
Stack trace:  
System.NotSupportedException
  HResult=0x80131515
  Message=Globalization Invariant Mode is not supported.
  Source=Microsoft.Data.SqlClient
  StackTrace:
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open()
   at ConsoleApp1.Program.Main(String[] args) in D:\Source Code\dotNet\PracticeProject01\ConsoleApp1\Program.cs:line 16

To reproduce

Here is the complete code, It uses a mssql local db as a database to connect.

// ConsoleApp1.csproj 

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <!--You need to rebuild the project after adding/removing the following line, for it to take effect-->
    <RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.0" />

  </ItemGroup>
</Project>

```c#
// Program.cs

using System;
using Microsoft.Data.SqlClient;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// MSSQLLocalDB is custom local db on my machine, it may not exist in your machine
var cStr = "Server=(localdb)\MSSQLLocalDB;Database=master;Integrated Security=true";
var sqlString = "Select 1";

        using (var conn = new SqlConnection(cStr)) {

            conn.Open();
            var command = conn.CreateCommand();
            command.CommandText = sqlString;
            var res = command.ExecuteScalar().ToString();

            Console.WriteLine("Response is: {0}", res);
        }
    }
}

}

```

Expected behavior

I expect the connection to succed and respond with "1" even when "Globalization Invariant Mode" is on.

Further technical details

Microsoft.Data.SqlClient version: v2.0.0
.NET target: .NET Core v3.1
SQL Server version: . SQL Server 2017
Operating system: Windows 10

By Design

All 9 comments

This is not a bug. It is by design. SqlClient is dependent on the Globalization libraries and can throw unexpected errors when it is not present. Thus the exception, "Globalization Invariant Mode is not supported."

@David-Engel thanks for quick response. is this documented somewhere?

It is documented in the error message.

@ErikEJ I mean, like acutal documentation. which I can link to as a resource to others, as of right now the best source for this info is this issue and #220 which is far from ideal.

Should it have been 07-23 ??

No, I wrote it two months ago to get my blog up and running and I haven't got around to putting anything else on there yet. It's not documentation per-se but it's better than trying to find the related information in various issues.

Great, shared on Twitter.

Closing this issue as it is a expected behavior. and will use @Wraith2 article as a reference

Was this page helpful?
0 / 5 - 0 ratings