'Thread.Sleep(5000) Causes Azure App Service to Shut Down

I have an app service which is hosted on Azure. I have code which creates an app managed certificate and then binds the certificate to the app. It could take several minutes for Azure to create the certificate, so I have to wait for the certificate to be created before I can bind the certificate to the app custom domain.

public static async Task<bool> CreateCustomDomainAndCertificate(string sHostName, string sUserEmail)
{
    bool bRet = false;

    HttpResponseMessage responseMessage = await CreateCustomDomain(sHostName);

    if (responseMessage.IsSuccessStatusCode)
    {
        Log.Logger.Information($"{sUserEmail} created hostname {sHostName}");

        bool bCertificateExists = await DoesCertificateExistAsync(sHostName);

        if(!bCertificateExists)
            responseMessage = await CreateAppManagedCertificate(sHostName);

        /*
             it can take a good 5 minutes to create the certificate
             but you get the 202 status code right away.
             
             You cannot bind the certificate to the custom domain
             name until after the certificate actually exists.
         */

        if ((long)responseMessage.StatusCode == 202 || (bCertificateExists && (long)responseMessage.StatusCode == 200))// 202 = Accepted, 200 = Ok
        {
            Log.Logger.Information($"{sUserEmail} created app managed certificate for hostname {sHostName}");
            DateTime dtStart = DateTime.Now;

            while ((long)responseMessage.StatusCode != 200 && DateTime.Now < dtStart.AddMinutes(10))
            {//Wait until the certificate has been created, up to 10 minutes

                Log.Logger.Information($"{sUserEmail} starting 10 second wait");

                Thread.Sleep(10000);//10 seconds
                Log.Logger.Information($"{sUserEmail} finish 10 second wait");
                responseMessage = await BindCertificateToCustomDomain(sHostName);

                if ((long)responseMessage.StatusCode == 200)
                {
                    bRet = true;
                    Log.Logger.Information($"{sUserEmail} binded app managed certificate to hostname {sHostName}");
                    return true;
                }
                else
                {
                    Log.Logger.Information($"{sUserEmail} failed to bind app managed certificate to hostname {sHostName}");                      
                }
            }                                            
        }
        else
        {
            Log.Logger.Information($"{sUserEmail} failed to create app managed certificate for hostname {sHostName}");
            return false;
        }
    }
    else
    {
        Log.Logger.Information($"{sUserEmail} failed to create hostname {sHostName}");
        return false;
    }
    return bRet;
}

After I execute the Thread.Sleep method, the app service starts shutting down. See the logs below:

2022-02-05 19:43:01.452 +00:00 [INF] [email protected] created hostname mysite
2022-02-05 19:43:06.322 +00:00 [INF] [email protected] created app managed certificate for hostname mysite
2022-02-05 19:43:06.323 +00:00 [INF] [email protected] starting 10 second wait
2022-02-05 19:43:07.368 +00:00 [INF] Request starting HTTP/1.1 GET http://myapp20211028195113.azurewebsites.net/ - -
2022-02-05 19:43:07.377 +00:00 [INF] Request finished HTTP/1.1 GET http://myapp20211028195113.azurewebsites.net/ - - - 307 - - 9.3353ms
2022-02-05 19:43:07.470 +00:00 [INF] Application is shutting down...
2022-02-05 19:43:12.492 +00:00 [INF] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
2022-02-05 19:43:12.496 +00:00 [INF] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
2022-02-05 19:43:12.508 +00:00 [INF] Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED Shutdown complete.
2022-02-05 19:43:16.334 +00:00 [INF] [email protected] finish 10 second wait
2022-02-05 19:43:17.952 +00:00 [INF] [email protected] failed to bind app managed certificate to hostname mysite
2022-02-05 19:43:17.954 +00:00 [INF] [email protected] starting 10 second wait
2022-02-05 19:43:27.961 +00:00 [INF] [email protected] finish 10 second wait
2022-02-05 19:43:29.124 +00:00 [INF] [email protected] failed to bind app managed certificate to hostname mysite
2022-02-05 19:43:29.125 +00:00 [INF] [email protected] starting 10 second wait
2022-02-05 19:43:39.138 +00:00 [INF] [email protected] finish 10 second wait
2022-02-05 19:43:39.989 +00:00 [INF] [email protected] failed to bind app managed certificate to hostname mysite
2022-02-05 19:43:39.992 +00:00 [INF] [email protected] starting 10 second wait
2022-02-05 19:43:50.233 +00:00 [INF] [email protected] finish 10 second wait
2022-02-05 19:43:51.791 +00:00 [INF] [email protected] failed to bind app managed certificate to hostname mysite
2022-02-05 19:43:51.997 +00:00 [INF] [email protected] starting 10 second wait
2022-02-05 19:44:02.211 +00:00 [INF] [email protected] finish 10 second wait
2022-02-05 19:44:03.335 +00:00 [INF] [email protected] failed to bind app managed certificate to hostname mysite
2022-02-05 19:44:03.336 +00:00 [INF] [email protected] starting 10 second wait
2022-02-05 19:44:13.354 +00:00 [INF] [email protected] finish 10 second wait
2022-02-05 19:44:14.443 +00:00 [INF] [email protected] failed to bind app managed certificate to hostname mysite
2022-02-05 19:44:14.444 +00:00 [INF] [email protected] starting 10 second wait
2022-02-05 19:44:24.457 +00:00 [INF] [email protected] finish 10 second wait
2022-02-05 19:44:25.574 +00:00 [INF] [email protected] failed to bind app managed certificate to hostname mysite
2022-02-05 19:44:25.577 +00:00 [INF] [email protected] starting 10 second wait

Notice that after I start the first 10 second wait, the app starts to shut down. I have to assume it is because of Thread Sleep.

Should I use another approach? This works fine when I am debugging with Visual Studio, but I get this weird behavior only when running on Azure. Any help would be appreciated.

Thanks

Update: I ended up creating a Job to look for for certificates that are created and need to be bound.

I created 3 boolean fields in a table. 1 field holds the status of the domain being associated with the web app. 1 field holds the status of the certificate created. 1 field holds the status of the certificate being associated with the custom domain name.



Solution 1:[1]

Use Task.Delay(10000) instead of Thread.Sleep(10000).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jaydeep Suryawanshi