Bug #95217 Only in trace listener (System.ObjectDisposedException: Cannot access a disposed
Submitted: 1 May 2019 22:59 Modified: 26 May 23:14
Reporter: Faisal Baqai Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.16 OS:Any
Assigned to: CPU Architecture:Any

[1 May 2019 22:59] Faisal Baqai
Description:
Using MySqlConnection class, if developer doesn't close connection then once Program ends, developer receive TraceError about Cannot access a disposed. 

How to repeat:
Create C# Console application:
Inside Main:

//This line is important to Repro, otherwise you won't see Error in Console.
void Main()
{
            Trace.Listeners.Add(new ConsoleTraceListener());

            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); //<==== I'm not using a 'using' block and no close at the end of program.
            builder.Server = serverName;
            builder.Database = databaseName;
            builder.Pooling = false;
            builder.DefaultCommandTimeout = 3600; 
            builder.SslMode = MySqlSslMode.None;
            builder.UserID = userName;
            builder.Password = password;

            using (MySqlConnection con = new MySqlConnection(builder.ToString()))
            {
                con.Open();
                ReadDatabaseProperty(con, "ReleaseID", "select SystemValueValue from SystemValue where SystemValueName='ReleaseID'");
            }

}
//////////////////////
public static string ReadDatabaseProperty(MySqlConnection connection, string propertyName, string query)
        {
            string propertyValue = string.Empty;

            try
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.CommandTimeout = 0;
                    cmd.Connection = (MySqlConnection)connection;
                    cmd.CommandText = query;

                    using (MySqlDataReader result = cmd.ExecuteReader())
                    {
                        while (result.Read())
                        {
                            propertyValue = GetSqlValue("SystemValueValue", result);
                        }
                    }
                }
            }
            catch (MySqlException msx)
            {
                return "";
            }

            return propertyValue;
        }

        private static string GetSqlValue(string columnName, MySqlDataReader reader)
        {
            object o = reader[columnName];
            if (o != null)
            {
                return o.ToString();
            }
            else
            {
                return "";
            }
        }
[1 May 2019 23:00] Faisal Baqai
Once you execute program, you will see following exception trace...

I'm using .NET 4.5

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at MySql.Data.MySqlClient.TimedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.SendPacket(MySqlPacket packet)
   at MySql.Data.MySqlClient.NativeDriver.ExecutePacket(MySqlPacket packetToExecute)
   at MySql.Data.MySqlClient.NativeDriver.Close(Boolean isOpen)
[1 May 2019 23:02] Faisal Baqai
Updating code... initially added code was when it DOES  NOT repro if I add 'using' block.

void Main()
{
            Trace.Listeners.Add(new ConsoleTraceListener());

            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(); 
            builder.Server = serverName;
            builder.Database = databaseName;
            builder.Pooling = false;
            builder.DefaultCommandTimeout = 3600; 
            builder.SslMode = MySqlSslMode.None;
            builder.UserID = userName;
            builder.Password = password;

            MySqlConnection con = new MySqlConnection(builder.ToString(); //<==== I'm not using a 'using' block and no close at the end of program.
            con.Open();
            ReadDatabaseProperty(con, "ReleaseID", "select SystemValueValue from SystemValue where SystemValueName='ReleaseID'");

}
[2 May 2019 7:03] MySQL Verification Team
Hello Faisal Baqai,

Thank you for the report.

thanks,
Umesh
[26 May 23:14] Omar Chavez
Posted by developer:
 
Not reproducible using Connector/NET 9.3.0 nad .NET 8