2

I have a website with some webservices written with web api 2 and .net 4.6. I want the application to be initialized when I restart the site or when I spin up a new instance (on Azure app service).

I have this code in webconfig:

<configuration>
 ,,,,
  <system.webServer>

       ...

  <applicationInitialization doAppInitAfterRestart="true">
  <add initializationPage="/"  />
  <add initializationPage="/api-v2/warmup/get"  />
</applicationInitialization>

I have this controller:

public class WarmupController : ApiController
{
   [HttpGet]
    [RequireRole(UserRole.None)]
    [IgnoreTypeScriptGeneration]
    public IHttpActionResult Get()
    {
        Log.Information($"Warming up started. {Environment.MachineName}");
         // my warm up code
         Log.Information($"Warming up succeeded. {Environment.MachineName}");
        return Ok();
    }

I cannot see any logs. Not when I restart the site nor when I do scale out. I've tried to call my api using postman without any headers (just a get to http://xxx//api-v2/warmup/get and it worked fine and also wrote to my logs.

I've already read this thread: Cannot warm up pages using applicationInitialization in webconfig

-There is no Ip restrictions on the Azure nor on the webconfig

-There is no url rewrites in webconfig ( like for https or for www.)

-There is no external system involved (like a CMS) so I shouldn't need ot send the hostName but I've also tried to send that and it didn't work

Ashkan S
  • 10,464
  • 6
  • 51
  • 80

2 Answers2

1

You are missing the hostName in your initializationPage module where hostname will be your actual website name i.e xx.azurewebsites.net

<system.webServer>
  <applicationInitialization
    doAppInitAfterRestart="true"
    skipManagedModules="true">
    <add initializationPage="/default.aspx" hostName="myhost"/>
  </applicationInitialization>

Refer here

Jayendran
  • 9,638
  • 8
  • 60
  • 103
  • 1
    but in the same link that you have sent it clearly says "(this is optional and if not specified the “localhost” will be used as a host name)" – Ashkan S Aug 16 '18 at 14:56
  • 1
    @AshkanSirous Oh, I missed that, did you give it a try of defining the `hostName` for some reason that may not work look https://forums.iis.net/t/1197884.aspx?question+Is+initializationPage+value+limited+to+calling+localhost+only+ – Jayendran Aug 16 '18 at 15:07
  • 1
    yes I made it work but it didn't have anything to do with host name. There was a part of the code that has been stupping the process from the local host to change the port which I should disabled and then it started working – Ashkan S Aug 20 '18 at 12:32
  • 1
    @AshkanSirous Great.! in such case you can provide your own answer and accept it – Jayendran Aug 20 '18 at 13:06
1

So I've finally managed to make it work!

The problem was in my code and not in the settings.

There was a part in the start up code that was adding a check to the requests and if it was from the localhost it was stopping the processes.

So lesson learned the hard way! :)

Check webconfig for redirects and rewrites Check the code for stuff that do the same thing

Ashkan S
  • 10,464
  • 6
  • 51
  • 80