Call to Application_Start after starting application pool in IIS
Asked Answered
S

1

5

I would like to know how can I setup the IIS, or the application if needed, for the next requirement: - When the application pool starts in IIS it should call to Application_Start in Global.asax

I was playing around with applicationHost.config getting the following code:

<applicationPools>
        <add name="mySite" autoStart="true" managedRuntimeVersion="v4.0" startMode="AlwaysRunning" />
        <applicationPoolDefaults>
            <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" />
        </applicationPoolDefaults>
    </applicationPools>

. .

 <site name="mySite" id="2" serverAutoStart="true">
            <application path="/" serviceAutoStartEnabled="true" applicationPool="mySite">
                <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\mySite" />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="127.0.0.1:8080:" />
            </bindings>
        </site>

So far the Application_Start is called only when a request is done.

Sanctity answered 11/12, 2012 at 16:13 Comment(3)
do you mean this: iis.net/downloads/microsoft/application-initializationSolis
It looks like something that I need. I will take a look. Thank you!Sanctity
@Solis What is the equivalent in .net Core ?Falconer
C
9

I just want to supplement what @paul said and agree that I was never able to get what Scott Guthrie said on his blog to fully function. Using:

<applicationPools>

    <add name="MyAppWorkerProcess" managedRuntimeVersion="v4.0" startMode="AlwaysRunning" />

</applicationPools>

would get the Application Pool to preload after recycling (as exemplified by seeing the w3wp.exe process reload on recycling the application pool).

But I never was able to get the second part to work:

<sites>

     <site name="MySite" id="1">

          <application path="/" applicationPool="MyAppWorkerProcess" serviceAutoStartEnabled="true" />

     </site>

</sites>

This may be because when using serviceAutoStartEnabled using serviceAutoStartProvider is also neccessary and that was just overkill for me since I simply wanted the Global.asax's Application_Start to be initialized.

Luckily, after reading this post and installing the Application Initialization Module and using this value in the Application configuration instead:

<sites>

     <site name="MySite" id="1">

          <application path="/" applicationPool="MyAppWorkerProcess" preloadEnabled="true" />

     </site>

</sites>

I was able to see that Application_Start is being invoked during initialization. This turns my 10 second initial web service call into a 750 ms initial web service call. Using preloadEnabled is exactly what I needed. I hope it helps others as well.

Contributory answered 20/11, 2013 at 21:9 Comment(2)
I came here looking for a way to do this with Hangfire to keep it running and ended up finding this forum post where odinserj explains the difference between the two methods very well.Pomade
What is the equivalent in .net Core ?Falconer

© 2022 - 2024 — McMap. All rights reserved.