Bug #46180 Cast IDbConnection to MySqlConnection fails
Submitted: 14 Jul 2009 19:05 Modified: 16 Aug 2009 7:07
Reporter: Marco Antonio Orestes Teixeira Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.0.4 OS:Windows (XP, 2003 Server)
Assigned to: Assigned Account CPU Architecture:Any
Tags: cast, IDbConnection, TResult, unable

[14 Jul 2009 19:05] Marco Antonio Orestes Teixeira
Description:
The problem occurs in the production environment where, perhaps, is not installed the 6.0.4 connector. In my web application, set a reference to the assembly MySql.Data which is in a folder named Resources within the web application. If you try to perform a cast to type TResult to an object created by the method DbProviderFactories.CreateConnection () it throws an error "Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to type 'MySql.Data.MySqlClient.MySqlConnection'. " Do know what to do...suggestions?

p.s.: Sorry about the bad english...

How to repeat:
Use this method:

public TResult GetConnection<TResult>()
		{
			IDbConnection result = null;
			ConnectionStringSettings connectionStringSettings = null;
			DbProviderFactory providerFactory = null;

			connectionStringSettings = ConfigurationManager.ConnectionStrings[_connectionString];

			providerFactory = DbProviderFactories.GetFactory(connectionStringSettings.ProviderName);
			result = providerFactory.CreateConnection();
			result.ConnectionString = connectionStringSettings.ConnectionString;
			return (TResult)result;
		}

Then refere to MySql.Data assembly in a folder within the application. Try to run this method.

Something like this... MySqlConnection conn = myClass.GetConnection<MySqlConnection>();
[15 Jul 2009 13:20] Marco Antonio Orestes Teixeira
Hi...me again...

Uhm...seems that the problem is in the DbProviderFactory...i don't know what's happen, but if i create a method the creates a connection (MySqlConnection), pass it as a object and try to cast (explicit) in the other side, the problem does not occurs.

Problem may be at DbProviderFactory? Any suggestion? My bad?
[15 Jul 2009 14:15] Vladislav Vaintroub
Tried to reproduce this against current trunk (which is 6.1.0, yet I think not much has changed since in this area)

by adding following fragment into 
MySql.Data\Tests\Source\InterfaceTests.cs  

      public TResult GetConnection<TResult>()
        {
            IDbConnection result = null;
           
            DbProviderFactory providerFactory = null;

            providerFactory = new MySqlClientFactory();
            result = providerFactory.CreateConnection();
            result.ConnectionString = GetConnectionString(true);
            return (TResult)result;
        }

        [Test]
        public void TestCast()
        {
            MySqlConnection c = GetConnection<MySqlConnection>();
        }

it passes just fine.
[15 Jul 2009 14:28] Tonci Grgin
Marco, please try Wlad's way and inform us of result.
[15 Jul 2009 16:29] Marco Antonio Orestes Teixeira
Hi Vladislav,

Thanks for your reply...=)

Uhm...tried your approach in dev environment and works fine, as expected, but didn't work in production server.
Let me explains better...in my machine, at dev environment, it works great...but when i try to publish the application in my server, i get the error "Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to
type 'MySql.Data.MySqlClient.MySqlConnection'."...so, tried to discover the connector version on server, and it's 5.2.3 version...but since i uploaded the assemblies (MySql.Data.dll...And used it, too, in my application reference in a folder called Resources, that is too on server) to the server, and point to it in my web.config file i didn't expect this error.

So...the workaround was, create a method that returns a MySqlConnection, as an object, and then try explicit cast to a MySqlConnection...well...it's works. But, why am i asking to do this way? Because i'm using Unity (Microsoft Unity) to create repositories for the application on whatever database (that may be installed on server)...and i want a generic way to create a connection for this "whatever" database.

Keep on trying...thanks!! =)
[16 Jul 2009 7:07] Tonci Grgin
Marco, believe it or not, we had similar discussion yesterday. Please try using same c/NET version in both "resources" folder and in GAC and inform us of result. You might be running into versioning issues because providers registered in machine.config and retrieved with DbProviderFactories have the full assembly version attached.
[16 Aug 2009 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".