Why is this rear exception happening "System.IO.IOException" in MySql.Data.MySqlClient.MySqlConnection.Open()?

3

The stack trace details:

System.Web.HttpUnhandledException (0x80004005): 
Exception of type 'System.Web.HttpUnhandledException' was thrown. 
---> System.AggregateException: One or more errors occurred. 
---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at MySql.Data.MySqlClient.NativeDriver.StartSSL()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at MySqlShell.ExecuteScalar(String query, MySqlConnection mySqlConnection, MySqlParameter[] mySqlParameters) in D:\myApp\MySqlShell.cs:line 47
at MyLibrary.GetAccountData() in D:\MyLibrary.cs:line 94
at MyClass.<>c_DisplayClass118_0.<FillDataGrid>b_1(Int32 i) in D:\myApp\MySqlShell.cs\EmployeeAccounts.aspx.cs:line 0
at System.Threading.Tasks.Parallel.<>c_DisplayClass17_0`1.<ForWorker>b_1()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c_DisplayClass176_0.<ExecuteSelfReplicating>b_0(Object )

I am getting this rear exception but surely approximately 1 or 2 times in every 1000 requests. Though I tried to rid this in different ways, but could not get any solution instead of dilemmas.

I went through this question similar to my exception, but there is no solution yet. I also found this question, but just fall in dilemma now what would be the solution for my exception. And I tried to write this piece of line in method Global.asax.cs/Application_BeginRequest():

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

Note: my application's target .NET framework version is 4.7, and I have to fix this exception in my application code. I don't have right to work on the server. So, please help me if I am in the correct way or not. Also, please ensure if this line of code is relevant to my exception. If this is correct, should I use bitwise shorthand operator '|=' in place '=' at the above line of code?

Thanks in advance.

asp.net
eof
mysqlconnection
servicepointmanager
httpunhandledexception
asked on Stack Overflow Jul 10, 2020 by X-Coder • edited Jul 13, 2020 by X-Coder

1 Answer

1

Change:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

To:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)12288
                                     | (SecurityProtocolType)3072
                                     | (SecurityProtocolType)768;
                                     | SecurityProtocolType.Tls;

Obviously you need to run this code before making the first HTTP request. To make sure the most recent TLS version is used you need to try catch it. Something like this:

try {
    ServicePointManager.SecurityProtocol = (SecurityProtocolType) 12288
            | (SecurityProtocolType) 3072
            | (SecurityProtocolType) 768
            | SecurityProtocolType.Tls;
} catch (NotSupportedException) {
    try {
        ServicePointManager.SecurityProtocol = (SecurityProtocolType) 3072
                | (SecurityProtocolType) 768
                | SecurityProtocolType.Tls;
    } catch (NotSupportedException) {
        try {
            ServicePointManager.SecurityProtocol = (SecurityProtocolType) 768
                    | SecurityProtocolType.Tls;
        } catch (NotSupportedException) {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
        }
    }
}
answered on Stack Overflow Jul 19, 2020 by James Mead

User contributions licensed under CC BY-SA 3.0