|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.ServiceProcess; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities; |
| 7 | +using Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests; |
| 8 | +using Microsoft.AspNetCore.Server.IntegrationTesting; |
| 9 | +using Microsoft.AspNetCore.Server.IntegrationTesting.IIS; |
| 10 | +using Microsoft.AspNetCore.Testing; |
| 11 | +using Microsoft.AspNetCore.Testing.xunit; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace IIS.FunctionalTests |
| 15 | +{ |
| 16 | + [Collection(PublishedSitesCollection.Name)] |
| 17 | + public class ApplicationInitializationTests : IISFunctionalTestBase |
| 18 | + { |
| 19 | + private readonly PublishedSitesFixture _fixture; |
| 20 | + |
| 21 | + public ApplicationInitializationTests(PublishedSitesFixture fixture) |
| 22 | + { |
| 23 | + _fixture = fixture; |
| 24 | + } |
| 25 | + |
| 26 | + [ConditionalTheory] |
| 27 | + [RequiresIIS(IISCapability.ApplicationInitialization)] |
| 28 | + [InlineData(HostingModel.InProcess)] |
| 29 | + [InlineData(HostingModel.OutOfProcess)] |
| 30 | + public async Task ApplicationInitializationInitializedInProc(HostingModel hostingModel) |
| 31 | + { |
| 32 | + var baseDeploymentParameters = _fixture.GetBaseDeploymentParameters(hostingModel); |
| 33 | + EnableAppInitialization(baseDeploymentParameters); |
| 34 | + |
| 35 | + var result = await DeployAsync(baseDeploymentParameters); |
| 36 | + |
| 37 | + // Allow IIS a bit of time to complete starting before we start checking |
| 38 | + await Task.Delay(100); |
| 39 | + // There is always a race between which Init request arrives first |
| 40 | + // retry couple times to see if we ever get the one comming from ApplicationInitialization module |
| 41 | + await result.HttpClient.RetryRequestAsync("/ApplicationInitialization", async message => await message.Content.ReadAsStringAsync() == "True"); |
| 42 | + |
| 43 | + StopServer(); |
| 44 | + EventLogHelpers.VerifyEventLogEvent(result, EventLogHelpers.Started(result)); |
| 45 | + } |
| 46 | + |
| 47 | + private static void EnableAppInitialization(IISDeploymentParameters baseDeploymentParameters) |
| 48 | + { |
| 49 | + baseDeploymentParameters.ServerConfigActionList.Add( |
| 50 | + (config, _) => { |
| 51 | + config |
| 52 | + .RequiredElement("configSections") |
| 53 | + .GetOrAdd("sectionGroup", "name", "system.webServer") |
| 54 | + .GetOrAdd("section", "name", "applicationInitialization") |
| 55 | + .SetAttributeValue("overrideModeDefault", "Allow"); |
| 56 | + |
| 57 | + config |
| 58 | + .RequiredElement("system.applicationHost") |
| 59 | + .RequiredElement("sites") |
| 60 | + .RequiredElement("site") |
| 61 | + .RequiredElement("application") |
| 62 | + .SetAttributeValue("preloadEnabled", true); |
| 63 | + |
| 64 | + config |
| 65 | + .RequiredElement("system.webServer") |
| 66 | + .GetOrAdd("applicationInitialization") |
| 67 | + .GetOrAdd("add", "initializationPage", "/ApplicationInitialization?IISInit=true"); |
| 68 | + }); |
| 69 | + |
| 70 | + baseDeploymentParameters.EnableModule("ApplicationInitializationModule", "%IIS_BIN%\\warmup.dll"); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments