Bug #88016 .NET CORE / STANDARD version fails to load on NETSTANDARD 2.0
Submitted: 6 Oct 2017 18:55 Modified: 27 Mar 2018 0:06
Reporter: Tuomas Hietanen Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:6.10.3-rc and 8.0.9 dmr OS:Any
Assigned to: CPU Architecture:Any
Tags: core, standard

[6 Oct 2017 18:55] Tuomas Hietanen
Description:
.NET versions of 6.9.9 and 6.10.3-rc seems to work well. But there is something fishy in .NET CORE / STANDARD versions of 6.10.3-rc and 8.0.9 dmr. 

I got constantly errors: 

 Derived types must either match the security accessibility of the base type or be less accessible.

Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlCommand'. 
Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlConnection'. 
Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlDataReader'.
Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlException'. 
Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlParameter'. 
Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlParameterCollection'. 
Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlClientFactory'.
Inheritance security rules violated by type: 'MySql.Data.MySqlClient.MySqlConnectionStringBuilder'.

How to repeat:
// To reproduce, here is a little sample program: 

using System;

namespace ConsoleApp4
{
	class Program
	{
		//Add first a reference to System.Data.Common.dll

		static void Main(string[] args)
		{
			var pathToDll = @"C:\source\lib\netstandard1.3\MySql.Data.dll";
			var assembly = System.Reflection.Assembly.LoadFrom(pathToDll);
			try
			{
				//Crash:
				var types = assembly.GetTypes();
			}
			catch (System.Reflection.ReflectionTypeLoadException e)
			{
				foreach(var ex in e.LoaderExceptions)
				{
					Console.WriteLine(ex.Message);
				}
			}
			Console.ReadLine();
		}

	}
}

Suggested fix:
As said earlier, the previous versions don't have this error. But I would like to use .NET Standard / .NET Core. 
I expect this is related to [assembly: AllowPartiallyTrustedCallers] and changed assembly.SecurityRuleSet.
[8 Oct 2017 12:56] Tuomas Hietanen
I managed to compile 6.10.3-rc from sources to .NET Standard 2.0 and resolved the problem by following changes:

Files to modify from MySQL.Data/src path:

---

MySQL.Data.csproj:
TargetFrameworks-tag: Add netstandard2.0

PackageReferences and DefineConstants are the same as netstandard1.3

---

AssemvlyInfo.cs:

#if !NET_CORE
[assembly: AllowPartiallyTrustedCallers()]
#endif

---
datareader.cs and ISSchemaProvider.cs: 
Replace: System.Data.SqlTypes.SqlNullValueException with something else e.g. Exception
(Only one throw and catch)
[9 Oct 2017 9:13] Chiranjeevi Battula
Hello Tuomas,

Thank you for the bug report.
I'm glad that you found a solution for your problem and let us know if you are still having the issue with complete repeatable steps.

Thank you for your interest in MySQL.

Thanks,
Chiranjeevi.
[10 Oct 2017 1:15] Tuomas Hietanen
Hi Chiranjeevi,

Thanks. But this is a bug even if I described a fix to it. As long as the original dll is not yet fixed.

Can I somehow send a pull-request or community-contribution to Mysql.Data.dll? Or is there any other way this bug fix described in the comment would end-up to the actual dll? I assume it will be required anyways in the future by most of .NET Standard 2.0 users.
[25 Oct 2017 11:51] Chiranjeevi Battula
Hello Tuomas,

Thank you for the feedback.
As per user manual we are supporting for .NET Core 1.1 for VS 2015/2017 only.
https://dev.mysql.com/doc/connector-net/en/connector-net-versions.html

Thank you for your interest in MySQL.

Thanks,
Chiranjeevi.
[26 Mar 2018 17:44] Dominick Sidiropoulos
+1 My team is facing this exact same issue with all versions of MySql.Data & MySql.EntityFramework from ver 6.10.6 all the way up to bleeding edge (ver. 8.x). Is there some way to get things working? We are targeting .net4.7.1 from within a simple ASP.NET MVC5 solution. Oracle & SqlServer EF drivers are working just fine. Only MySql EF-Drivers are giving us such a hard time. Version 6.9.11 of the MySql EF-Drivers are working however they lack certain bugfixes we desperately need.
[27 Mar 2018 0:06] Tuomas Hietanen
Dominick: I gave up and went for an unofficial open source driver made by a third party. 

It was a right choice as this driver is closed source, slow, and I cannot say anything good about the support either.