Leonardo
Leonardo

Reputation: 11391

.Net Core - Application Crashing while debugging

Something really strange is happening. If i place a break point after a certain line of code, when debugging, the debug session stops as if i hit the "Stop" button.
What's really strange is that, if i skip that function altogether, jumping over it, the code does not crash...

I'm using .Net Core 2, running in a fully updated VS For Mac on any VS (windows and Mac), using C# 7.1

heres the code:

        var connectionToUse = new SqlConnection(string.Format(str, dbName));
        try
        {
            SqlCommand command = new SqlCommand();
            command.Connection = connectionToUse;
            command.CommandText = @"SELECT * from myTable";
            await connectionToUse.OpenAsync(); //CANT GET PAST THIS LINE HERE
            var r1 = await command.ExecuteReaderAsync();
            while (await r1.ReadAsync())
            {
                //MORE CODE
            }
            r1.Close();
            await Task.Delay(15000);
        }
        catch (Exception ex)
        {
            //NEVER ENTERS HERE
        }
        finally
        {
            if (connectionToUse.State != ConnectionState.Closed)
            {
                connectionToUse.Close();
            }
        }

Edit1:
Using .Open(), no async, works perfectly. But the problem moves to the r1.ReadAsync() line...

Edit2:
prior to that code, the following one runs perfectly

    private async Task<Dictionary<int, string>> MapDatabases()
    {
        Dictionary<int, string> databases = new Dictionary<int, string>();

        SqlConnection mainConn = new SqlConnection(string.Format(str, "Master"));
        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT name,database_id FROM sys.databases WHERE database_id = 5";
        command.Connection = mainConn;
        try
        {
            await mainConn.OpenAsync();
            SqlDataReader reader = await command.ExecuteReaderAsync();
            while (await reader.ReadAsync())
            {
                databases.Add(reader.GetInt32(1), reader.GetString(0));
            }
            reader.Close();
            mainConn.Close();
        }
        finally
        {
            if (mainConn.State != ConnectionState.Closed)
            {
                mainConn.Close();
            }
        }
        return databases;
    }

EDIT3:
Also reproducible on VS 2017 fully updated.

EDIT4:
Apparently theres something wrong with the await statement. I found out that the code will crash in the first subsequent await, no matter where it is

Upvotes: 3

Views: 1068

Answers (1)

Leonardo
Leonardo

Reputation: 11391

I kinda figured out... I was spanning new threads using Task.StartNew. After changing to Task.Run things started working properly. Can't figure out why the debug session was crashing though... Since the code is working as expected, i'm accepting this as answer.

Upvotes: 1

Related Questions