SqlClient: Mono: System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper'
Error to connect on SQL Server using Microsoft.Data.SqlClient with Mono on linux.
Exception message: The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper' threw an exception.
Stack trace:
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper' threw an exception. ---> System.EntryPointNotFoundException: GetModuleHandle assembly:<unknown assembly> type:<unknown type> member:(null)
at (wrapper managed-to-native) Microsoft.Data.Common.SafeNativeMethods.GetModuleHandle(string)
at Microsoft.Data.SqlClient.InOutOfProcHelper..ctor () [0x00006] in <de6019a43b5544ecb987a2a77d294ad3>:0
at Microsoft.Data.SqlClient.InOutOfProcHelper..cctor () [0x00000] in <de6019a43b5544ecb987a2a77d294ad3>:0
--- End of inner exception stack trace ---
at Microsoft.Data.SqlClient.SqlConnectionString..ctor (System.String connectionString) [0x0000d] in <de6019a43b5544ecb987a2a77d294ad3>:0
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions (System.String connectionString, Microsoft.Data.Common.DbConnectionOptions previous) [0x00000] in <de6019a43b5544ecb987a2a77d294ad3>:0
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup (Microsoft.Data.Common.DbConnectionPoolKey key, Microsoft.Data.ProviderBase.DbConnectionPoolGroupOptions poolOptions, Microsoft.Data.Common.DbConnectionOptions& userConnectionOptions) [0x0003e] in <de6019a43b5544ecb987a2a77d294ad3>:0
at Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set (Microsoft.Data.Common.DbConnectionPoolKey key) [0x00008] in <de6019a43b5544ecb987a2a77d294ad3>:0
at Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString (System.String value) [0x000c8] in <de6019a43b5544ecb987a2a77d294ad3>:0
at Microsoft.Data.SqlClient.SqlConnection..ctor (System.String connectionString, Microsoft.Data.SqlClient.SqlCredential credential) [0x00006] in <de6019a43b5544ecb987a2a77d294ad3>:0
at Microsoft.Data.SqlClient.SqlConnection..ctor (System.String connectionString) [0x00000] in <de6019a43b5544ecb987a2a77d294ad3>:0
at (wrapper remoting-invoke-with-check) Microsoft.Data.SqlClient.SqlConnection..ctor(string)
at MonoCrash.Program.Main (System.String[] args) [0x00002] in <b0c88f4fc5724b4fbf81cd0ce25f6959>:0
To reproduce
Gist: https://gist.github.com/arhughes/8b15d7ee88da250cc4492814cac6db52
public static void Main(string[] args)
{
try
{
using (var connection = new SqlConnection("Server=mssql;User Id=sa;Password=Password1!"))
{
connection.Open();
Console.WriteLine("Connection opened successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Expected behavior
Connect to the database.
Further technical details
Microsoft.Data.SqlClient version: 3.0.0 .NET target: Mono with .NET Framework 4.7.1 SQL Server version: SQL Server 2017 Operating system: Docker container - Linux
Additional context
Reported: https://github.com/fluentmigrator/fluentmigrator/issues/1483
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 17 (11 by maintainers)
The netfx implementation requires an unmanaged component to implement network connectivity. That implementaion is closed source non-portable and not provided for non-windows platforms. In the cases above people are restoring the netfx implementation and trying to use it on linux and that won’t work. If you want to run the code on linux you need to use the correct platform implementation assembly.
Here is one for Linux container with .netcore 3.1 and mono https://gist.github.com/Kaur-Parminder/92b92ef2152ba3dcbdb168e4983a1e2c