ASP.NET Core IHostedService + IIS自动启动功能不起作用

7z5jn7bk  于 2023-06-23  发布在  .NET
关注(0)|答案(1)|浏览(365)

我有一个.NET 6 IHostedService托管在IIS 10 Windows Server 2019上。
我的目标是

  • 在服务器重新启动后启动应用程序
  • iisreset后启动应用程序
  • iisreset /start后启动应用程序

我关注SO帖子here和这个博客post
关于SO post的回答:

  • 我重启了服务器
  • 遵循所有步骤

关于博客文章:

  • 我尝试将CLR设置为“无托管代码”和“v4.0”

我在其他网站上发现了一些提到我必须启用匿名身份验证-这也是测试的(见下面的applicationhost.config
但是我不能让IIS的这个基本功能启动和运行。根据SO帖子和博客帖子,它似乎帮助了大多数人。
web.config

<?xml version="1.0" encoding="utf-8"?>
   <configuration>
       <location path="." inheritInChildApplications="false">
           <system.webServer>
               <handlers>
                   <remove name="aspNetCore" />
                   <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
               </handlers>
               <aspNetCore processPath="dotnet" arguments=".\<obfuscated>.dll" 
                           stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" 
                           hostingModel="inprocess">
                   <environmentVariables>
                       <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
                   </environmentVariables>
               </aspNetCore>
            </system.webServer>
        </location>
    </configuration>

applicationhost.config

<?xml version="1.0" encoding="UTF-8"?>
< ... />
<configuration>
    <system.applicationHost>
        <applicationPools>
            <add name="obfuscated" autoStart="true" managedRuntimeVersion="v4.0" startMode="AlwaysRunning">
                <processModel identityType="SpecificUser" userName="obfuscated" password="obfuscated" idleTimeout="00:00:00" />
            </add>
            <applicationPoolDefaults managedRuntimeVersion="v4.0">
                <processModel identityType="ApplicationPoolIdentity" />
                <recycling logEventOnRecycle="Time, Requests, Schedule, Memory, IsapiUnhealthy, OnDemand, ConfigChange, PrivateMemory">
                    <periodicRestart time="00:00:00" />
                </recycling>
            </applicationPoolDefaults>
        </applicationPools>

        < ... />
      
        <sites>
            <site name="obfuscated" id="1">
                <application path="/" applicationPool="obfuscated" preloadEnabled="true">
                    <virtualDirectory path="/" physicalPath="obfuscated" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="obfuscated" />
                </bindings>
            </site>
            <siteDefaults>
                <logFile logExtFileFlags="Date, Time, ClientIP, UserName, SiteName, ComputerName, ServerIP, Method, UriStem, UriQuery, HttpStatus, Win32Status, TimeTaken, ServerPort, UserAgent, Referer, Host, HttpSubStatus" logFormat="W3C" directory="obfuscated">
                </logFile>
                <traceFailedRequestsLogging directory="obfuscated" />
            </siteDefaults>
            <applicationDefaults applicationPool="DefaultAppPool" />
            <virtualDirectoryDefaults allowSubDirConfig="true" />
        </sites>

        <webLimits />
    </system.applicationHost>

    <system.webServer>

        <asp />

        <caching enabled="true" enableKernelCache="true">
        </caching>

        <cgi />

        <defaultDocument enabled="true">
            <files>
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="index.htm" />
                <add value="index.html" />
                <add value="iisstart.htm" />
                <add value="default.aspx" />
            </files>
        </defaultDocument>

        <directoryBrowse enabled="false" />

        <fastCgi />
        <!--
          The <globalModules> section defines all native-code modules.
          To enable a module, specify it in the <modules> section.
        -->
        <globalModules>
            <add name="HttpLoggingModule" image="%windir%\System32\inetsrv\loghttp.dll" />
            <add name="UriCacheModule" image="%windir%\System32\inetsrv\cachuri.dll" />
            <add name="FileCacheModule" image="%windir%\System32\inetsrv\cachfile.dll" />
            <add name="TokenCacheModule" image="%windir%\System32\inetsrv\cachtokn.dll" />
            <add name="HttpCacheModule" image="%windir%\System32\inetsrv\cachhttp.dll" />
            <add name="DynamicCompressionModule" image="%windir%\System32\inetsrv\compdyn.dll" />
            <add name="StaticCompressionModule" image="%windir%\System32\inetsrv\compstat.dll" />
            <add name="DefaultDocumentModule" image="%windir%\System32\inetsrv\defdoc.dll" />
            <add name="DirectoryListingModule" image="%windir%\System32\inetsrv\dirlist.dll" />
            <add name="ProtocolSupportModule" image="%windir%\System32\inetsrv\protsup.dll" />
            <add name="HttpRedirectionModule" image="%windir%\System32\inetsrv\redirect.dll" />
            <add name="StaticFileModule" image="%windir%\System32\inetsrv\static.dll" />
            <add name="AnonymousAuthenticationModule" image="%windir%\System32\inetsrv\authanon.dll" />
            <add name="UrlAuthorizationModule" image="%windir%\System32\inetsrv\urlauthz.dll" />
            <add name="WindowsAuthenticationModule" image="%windir%\System32\inetsrv\authsspi.dll" />
            <add name="IpRestrictionModule" image="%windir%\System32\inetsrv\iprestr.dll" />
            <add name="DynamicIpRestrictionModule" image="%windir%\System32\inetsrv\diprestr.dll" />
            <add name="RequestFilteringModule" image="%windir%\System32\inetsrv\modrqflt.dll" />
            <add name="CustomErrorModule" image="%windir%\System32\inetsrv\custerr.dll" />
            <add name="RequestMonitorModule" image="%windir%\System32\inetsrv\iisreqs.dll" />
            <add name="IsapiModule" image="%windir%\System32\inetsrv\isapi.dll" />
            <add name="IsapiFilterModule" image="%windir%\System32\inetsrv\filter.dll" />
            <add name="ManagedEngine64" image="%windir%\Microsoft.NET\Framework64\v2.0.50727\webengine.dll" preCondition="integratedMode,runtimeVersionv2.0,bitness64" />
            <add name="ManagedEngine" image="%windir%\Microsoft.NET\Framework\v2.0.50727\webengine.dll" preCondition="integratedMode,runtimeVersionv2.0,bitness32" />
            <add name="ManagedEngineV4.0_32bit" image="%windir%\Microsoft.NET\Framework\v4.0.30319\webengine4.dll" preCondition="integratedMode,runtimeVersionv4.0,bitness32" />
            <add name="ManagedEngineV4.0_64bit" image="%windir%\Microsoft.NET\Framework64\v4.0.30319\webengine4.dll" preCondition="integratedMode,runtimeVersionv4.0,bitness64" />
            <add name="ConfigurationValidationModule" image="%windir%\System32\inetsrv\validcfg.dll" />
            <add name="WebSocketModule" image="%windir%\System32\inetsrv\iiswsock.dll" />
            <add name="RewriteModule" image="%SystemRoot%\system32\inetsrv\rewrite.dll" />
            <add name="AspNetCoreModuleV2" image="%ProgramFiles%\IIS\Asp.Net Core Module\V2\aspnetcorev2.dll" />
        </globalModules>

        < ... />

        <security>

            <access sslFlags="None" />

            <applicationDependencies />

            <authentication>

                <anonymousAuthentication enabled="true" userName="IUSR" />

                <basicAuthentication />

                <clientCertificateMappingAuthentication />

                <digestAuthentication />

                <iisClientCertificateMappingAuthentication />

                <windowsAuthentication enabled="false" authPersistNonNTLM="true">
                    <providers>
                        <add value="Negotiate" />
                        <add value="NTLM" />
                    </providers>
                </windowsAuthentication>

            </authentication>

            <authorization>
                <add accessType="Allow" users="*" />
            </authorization>

            <ipSecurity allowUnlisted="true" />

            <isapiCgiRestriction>
                <add path="%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" allowed="true" groupId="ASP.NET v2.0.50727" description="ASP.NET v2.0.50727" />
                <add path="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" allowed="true" groupId="ASP.NET v2.0.50727" description="ASP.NET v2.0.50727" />
                <add path="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" allowed="true" groupId="ASP.NET v4.0.30319" description="ASP.NET v4.0.30319" />
                <add path="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" allowed="true" groupId="ASP.NET v4.0.30319" description="ASP.NET v4.0.30319" />
            </isapiCgiRestriction>

            < ... />

        </security>

        < ... />

    </system.webServer>
    <location path="" overrideMode="Allow">
        <system.webServer>

        <modules>
          < ... />
        </modules>

        <handlers accessPolicy="Read, Script">
              < ... />
        </handlers>
        </system.webServer>
    </location>

</configuration>
jmp7cifd

jmp7cifd1#

您应该在服务器上启用Application Initialization模块以自动重新启动应用程序。
默认情况下,如果在20分钟内没有向应用程序发送请求,则应用程序将被挂起。

此PowerShell脚本可以帮助您保持应用程序始终处于活动状态

## IIS WebAdmin Module
    Import-Module WebAdministration

    $AppPoolInstance = Get-Item IIS:\AppPools\$AppPool

    Write-Output "Set Site PreLoadEnabled to true"
    Set-ItemProperty IIS:\Sites\$Site -name applicationDefaults.preloadEnabled -value True

    Write-Output "Set Recycling.periodicRestart.time  = 0"
    $AppPoolInstance.Recycling.periodicRestart.time = [TimeSpan]::Parse("0");
    $AppPoolInstance | Set-Item

    Write-Output "Set App Pool start up mode to AlwaysRunning"
    $AppPoolInstance.startMode = "alwaysrunning"

    Write-Output "Disable App Pool Idle Timeout"
    $AppPoolInstance.processModel.idleTimeout = [TimeSpan]::FromMinutes(0)
    $AppPoolInstance | Set-Item

    if ($appPoolStatus -ne "Started") {
        Write-Output "Starting App Pool"
        Start-WebAppPool $AppPool
    } else {
        Write-Output "Restarting App Pool"
        Restart-WebAppPool $AppPool
    }
    • 注意:**您应该将$AppPool$Site名称更改为您的应用程序

link可能对你有帮助

相关问题