2
votes

I have a strange problem with the Microsoft Azure Mobile Backend for a while. Usually the first query (an automatic user check) fails at first run, after 30-60 sec of waiting it returns an error. After this error it works perfectly. I experienced this issue approximately once in a 24 hours time period.

The code is:

Step1: Initializing the backend (and after the related tables with this backend)

Backend.Primary = new Backend("https://....azurewebsites.net");

Step2: The failed query is

    public async Task<Tuple<Result, TeguUsers>> FindUserPC1NIC(string mac)
    {
        try
        {
            IMobileServiceTableQuery<TeguUsers> query = TeguUsersTable.Where(user => user.M1Mac1 == mac || user.M1Mac2 == mac );

            List<TeguUsers> result = await query.ToListAsync();

            return CreateFoundUsersResult(result);
        }
        catch (MobileServiceConflictException msce)
        {
            return new Tuple<Result, TeguUsers>(new Result("Failed [msce]", msce.Message), null);
        }
        catch (MobileServicePreconditionFailedException mspfe)
        {
            return new Tuple<Result, TeguUsers>(new Result("Failed [mspfe]", mspfe.Message), null);
        }
        catch (MobileServiceInvalidOperationException msioe)
        {
            return new Tuple<Result, TeguUsers>(new Result("Failed [msioe]", msioe.Message), null);
        }
        catch (MobileServiceODataException msode)
        {
            return new Tuple<Result, TeguUsers>(new Result("Failed [msode]", msode.Message), null);
        }

        catch (Exception ex)
        {
            return new Tuple<Result, TeguUsers>(new Result("Failed", ex.Message), null);
        }
    }

Before I was using it with general Exception, today I added the specific Exceptions.

Can you tell me how to fix this problem?

  • Change anything in my code?

  • Change anything at backend side?

  • Anything else more advanced debugging technique?

A similar exception message is:

Message The request could not be completed. (Internal Server Error)

Request {Method: GET, RequestUri: 'https://....azurewebsites.net/tables/TeguUsers?$filter=((((((M1Mac1 eq '####') or (M1Mac2 eq '####')) or (M2Mac1 eq '####')) or (M2Mac2 eq '####')) or (M3Mac1 eq '####')) or (M3Mac2 eq '####'))&$top=1', Version: 1.1, Content: , Headers: { X-ZUMO-FEATURES: TT X-ZUMO-INSTALLATION-ID: 7a813fc5-f5cd-47ce-97a2-9b388cb3e159 Accept: application/json User-Agent: ZUMO/4.0 User-Agent: (lang=Managed; os=Windows; os_version=6.2.0.9200; arch=Win32NT; version=4.0.1.0) X-ZUMO-VERSION: ZUMO/4.0 (lang=Managed; os=Windows; os_version=6.2.0.9200; arch=Win32NT; version=4.0.1.0) ZUMO-API-VERSION: 2.0.0 }}

Request.Header {X-ZUMO-FEATURES: TT X-ZUMO-INSTALLATION-ID: 7a813fc5-f5cd-47ce-97a2-9b388cb3e159 Accept: application/json User-Agent: ZUMO/4.0 (lang=Managed; os=Windows; os_version=6.2.0.9200; arch=Win32NT; version=4.0.1.0) X-ZUMO-VERSION: ZUMO/4.0 (lang=Managed; os=Windows; os_version=6.2.0.9200; arch=Win32NT; version=4.0.1.0) ZUMO-API-VERSION: 2.0.0 }

Request.URI {https://....azurewebsites.net/tables/TeguUsers?$filter=((((((M1Mac1 eq '####') or (M1Mac2 eq '####')) or (M2Mac1 eq '####')) or (M2Mac2 eq 'F0761C70A89B')) or (M3Mac1 eq '####')) or (M3Mac2 eq '####'))&$top=1}

Response {StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Fri, 20 Oct 2017 11:14:25 GMT Set-Cookie: ARRAffinity=3fb8a85fa49f4f848d3e147fa888c4fd566bdbdf451e7fccb0737b58d905c69c;Path=/;HttpOnly;Domain=....azurewebsites.net Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Content-Length: 0 }}

StackTrace " at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.d__24.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.d__26.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.d__18.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.d__20.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.d__18.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.d__81.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.<Execute>d__71.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQuery1.<ToListAsync>d__33.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at Tegu.Azure.AMS.Table.TeguUsers.MST.d__11.MoveNext() in d:\Visual Studio Projects\TeguSolutions\TeguDev.Azure.AMS.Table.TeguUsers\Azure.AMS.Table.TeguUsers.cs:line 144"

2

2 Answers

1
votes

Microsoft Azure Mobile Backend used Windows hosting and IIS with all it is features - one of them is IIS application pool recycling - by default if Application Pool dosen`t receive an request - it unloads all resources it handle and in order to restore functionality it need abut a minute to start. In order to avoid such problem you should be able to control hosting server IIS or at least be able to run a script on it.

You need to run a script on your Web App Instance when it creates:

The default value for idle time-out is 20 minutes - this script disable sleep mode on app pool:

  %windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00

This script disable recycling mode on app pool (default 24 hours):

  %windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00

You also could use Remote Desktop to access instances and there in IIS Manager UI set up needed values

Official site

1
votes

According to your description, there is no problem with your code, but only with Azure configuration. Azure unloads your application if it's idle for a while. In most cases, it's desired behavior: you don't need to waste your Application Plan resources.

You need to take a look at Always On property of your Mobile App. By default, this property is set to Off, but you need to turn it On.

There are few ways to do it:

  1. If you use CI/CD pipelines to maintain your app, consider ARM template for Web Sites. Go to SiteConfig object, property alwaysOn
  2. For powershell, find an -AppSettings section at official site
  3. Or Azure Portal user interface. Navigate to your App Service/Application settings, find a Always On switcher. azure_portal_sh

EDITED

I suppose @Vladimir's answer explains what is happening under the hood meanwhile.