C # async deadlock - why is this happening in this case?

I have an ASP.Net application (actually Lightswitch is involved here, not sure if it matters in this case).

I have the following code. It runs in LogIn.aspx, which calls RegisterUser on the OnLoggedIn event.

    public static void RegisterUser(string userName)
    {
        using (var connection = new SqlConnection(IzendaSettings.ConnectionString))
        using (var command = connection.CreateCommand())
        {
            connection.Open();
            command.CommandText = @"SELECT CU.DisplayName, CU.ClientId, C.Name FROM ClientUser CU INNER JOIN Client C on C.ClientId = CU.ClientId WHERE CU.UserName = @userName AND Rights = 0";
            command.Parameters.Add("userName", SqlDbType.NVarChar).Value = userName; 
            using (var reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    var unused = RegisterUserAsync(userName, reader.GetString(0), reader.GetInt32(1).ToString(), reader.GetString(0));
                }
            }
        }
    }

    internal static async Task RegisterUserAsync(string userName, string fullName, string clientId, string clientName)
    {
        try
        {
            var token = TokenAuthorization.GetSystemAdministratorToken();
            while (Bootstrap.Initalizing)
            {
                Thread.Yield();
            }
            var izenda = await TenantProcess.Initialize(clientId, clientName, token);
            var connection = await izenda.SetUpConnection(IzendaSettings.ConnectionString);
            await izenda.PutUserInRole("User", userName, fullName, "~/BI/tenantRolePermission.json", connection);
        }
        catch (Exception e)
        {
            Logger.Error("Register User", e);
        }
    }

      

Asynchronous code syntax. I am trying to just run it on a separate thread, I am not expecting this on purpose.

Changing the call to RegisterUserAsync to this (with appropriate SQL capture prior to starting the thread):

Task.Run (() => RegisterUserAsync (username, user.FullName, user.ClientId, user.ClientName));

Solves the problem. I understand why calling Task.Run allows the asynchronous call you want to wait without deadlock, but I don't understand why the deadlock occurs in this case. (The initialization variable was bogus - I registered the debug statement to make sure I never actually executed Thread.Yield, but I wouldn't understand why that would affect this anyway).

Why was there a dead end in the old code?

+3


source to share





All Articles