Bug #70480 Exception when debugging an ASP.NET application
Submitted: 1 Oct 2013 19:59 Modified: 30 Jan 2018 20:49
Reporter: Sean Nolan Email Updates:
Status: Analyzing Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.8.3 OS:Any
Assigned to: Assigned Account CPU Architecture:Any

[1 Oct 2013 19:59] Sean Nolan
Description:
In the ProgramName attribute get accessor in MySqlConnectAttrs there is code that tries to determine the name of the running program like this
          string path = Environment.CommandLine.Substring(0, Environment.CommandLine.IndexOf("\" ")).Trim('"');
          name = System.IO.Path.GetFileName(path);

In an ASP.NET application the path is typically going to be
c:\windows\system32\inetsrv\w3wp.exe -ap "App pool name

This results in the try/catch falling into the catch.

Given that the code is using a try/catch it does not raise runtime errors but it means that the ProgramName is not correctly determined and if you are debugging then you will see the exception each time a database call is made.

How to repeat:
Use the NET connector in an ASP.NET application. Attach the Visual Studio debugger and make a database call.

Suggested fix:
I'd suggest something like this as an alternative for getting the path

               string path = "";
               if (Environment.CommandLine.StartsWith("\""))
               {
                   path = Environment.CommandLine.Substring(0, Environment.CommandLine.IndexOf("\" ")).Trim('"');
               }
               else
               {
                   path = Environment.CommandLine.Substring(0, Environment.CommandLine.IndexOf(" "));
               }
[23 May 2014 23:17] Francisco Alberto Tirado Zavala
Hello.

Sorry for the long delay, after some test and research I was not able to replicate the issue.
Please can you share the code or the project where you have the problem?

Thanks for your time.
[2 Jun 2014 22:51] Sean Nolan
Attaching the debugger to the w3wp.exe process

Attachment: AttachDebugger.PNG (image/png, text), 49.50 KiB.

[2 Jun 2014 22:52] Sean Nolan
The exception that is caught

Attachment: Exception.PNG (image/png, text), 193.33 KiB.

[2 Jun 2014 22:53] Sean Nolan
I can repro with a simple ASP.NET app. Any use of a MySql.Data.MySqlClient.MySqlConnection should make it happen. I put this code in the Page_Load of the default.aspx code behind:

        MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection("server=localhost;database=test;uid=user;password=pass");
        conn.Open();
        MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select @@version", conn);
        Response.Write(cmd.ExecuteScalar());

I added the MySql .NET Connector source project to my solution so that I could reference the source from the ASP.NET project and then put a breakpoint in ProgramName in MysqlDefs.cs (line 475 in the 6.8.3 source).

The key thing is to add a web site for your ASP.NET project in IIS Manager, don't just run it in Visual Studio because that will use IIS Express and the command line will work.

Now hit the page in a browser and verify that it works and displays the version from your MySQL server. Then use the Attach to process option to connect the Visual Studio debugger and connect to the w3wp.exe process.

When I do this, then the value in Environment.CommandLine is:
c:\windows\system32\inetsrv\w3wp.exe -ap "Bug70480" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm010da7de-aeaa-4fdb-bf01-0ef0ab5ce3a7 -h "C:\inetpub\temp\apppools\Bug70480\Bug70480.config" -w "" -m 0 -t 20

Note that the program path is not enclosed in quotes, this results in the value in the path local variable being wrong because the code looks for the text in quotes:
c:\windows\system32\inetsrv\w3wp.exe -ap "Bug70480

The value in Environment.CommandLine when I use the IIS Express built in web server with Visual Studio is:
"C:\Program Files (x86)\IIS Express\iisexpress.exe"  /config:"C:\Users\user\Documents\IISExpress\config\applicationhost.config"  /site:"Bug70480" /apppool:"Clr4IntegratedAppPool"

The value in the path local variable is then correct:
C:\Program Files (x86)\IIS Express\iisexpress.exe

Basically, what it comes down to is that the current way the code in ProgramName is written expects that the program path will always be enclosed in quotes when it is returned by Environment.CommandLine, but that is not true, it may be returned without quotes and that causes the exception.
[9 Jan 2015 2:37] J Crim
I see this too.  Sean's analysis exactly describes the cause of the issue.

Run Visual Studio as an administrator, debug a webapp that uses MySQL connector, and under Debug | Exceptions... select break on "System.ArgumentException" thrown.
[11 Feb 2016 9:26] Jan Veres
I have the similar exception:

Length cannot be less than zero.
Parameter name: length

Environment.CommandLine.IndexOf(string) get -1 value for me.
[4 May 2016 23:35] André LFS Bacci André
It's 2016 and this problem persists.

Length cannot be less than zero. Parameter name: length
  at System.String.Substring (Int32 startIndex, Int32 length) [0x00053] in <filename unknown>:0 
  at MySql.Data.MySqlClient.MySqlConnectAttrs.get_ProgramName () [0x0001b] in <filename unknown>:0 

Cannot debug code with "stop on all exceptions" because of this.
[30 Jan 2018 20:49] Sean Nolan
Looking at the source for the 6.10.x connector the ProgramName attribute has been removed so I guess this bug will go away if you're using 6.10.x - I can't verify because I'm stuck with using 6.9.x because of a critical bug introduced in 6.10.x (https://bugs.mysql.com/88660).