【问题标题】:How to determine if asp.net core has been installed on a windows server如何确定asp.net核心是否已安装在windows服务器上
【发布时间】:2016-07-25 12:28:03
【问题描述】:

我正在设置各种 Windows 服务器来托管 asp.net 核心应用程序,我需要能够确定它们是否安装了 asp.net 托管包。

https://docs.asp.net/en/latest/publishing/iis.html#install-the-net-core-windows-server-hosting-bundle 说:

"在服务器上安装 .NET Core Windows Server Hosting 捆绑包。 该捆绑包将安装 .NET Core 运行时、.NET Core 库和 ASP.NET 核心模块。该模块创建之间的反向代理 IIS 和 Kestrel 服务器。”

我正在设置部署,我需要确保我的服务器已配置,以便我可以运行 asp.net 核心应用程序。

基本上,我正在寻找注册表项或其他方式来告诉我是否应该运行安装程序设置。 (类似于我们判断是否安装了旧版本的框架的方式,例如 https://support.microsoft.com/en-us/kb/318785 适用于早期版本)

【问题讨论】:

    标签: asp.net-core .net-core


    【解决方案1】:

    您可以在HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Updates\.NET Core 路径下搜索Microsoft .NET Core 1.1.1 - Windows Server Hosting 注册表项,如下图所示。

    您也可以使用 PowerShell 来确定密钥是否存在。

    $DotNETCoreUpdatesPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\.NET Core"
    $DotNetCoreItems = Get-Item -ErrorAction Stop -Path $DotNETCoreUpdatesPath
    $NotInstalled = $True
    $DotNetCoreItems.GetSubKeyNames() | Where { $_ -Match "Microsoft .NET Core.*Windows Server Hosting" } | ForEach-Object {
        $NotInstalled = $False
        Write-Host "The host has installed $_"
    }
    If ($NotInstalled) {
        Write-Host "Can not find ASP.NET Core installed on the host"
    }
    

    您可以从How to determine ASP.NET Core installation on a Windows Server by PowerShell下载示例。

    【讨论】:

    • 如果没有安装,“Get-Item -ErrorAction Stop -Path $DotNETCoreUpdatesPath”会出错,脚本永远不会完成。脚本需要被 try catch 包围才能工作。如果您愿意,我可以编辑答案以包括在内。
    • 或者,我们可以使用 Test-Path 来检查 $DotNETCoreUpdatesPath 是否存在: $NotInstalled = $True $DotNETCoreUpdatesPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\.NET Core" if (测试路径 $DotNETCoreUpdatesPath) { $DotNetCoreItems = Get-Item -ErrorAction Stop -Path $DotNETCoreUpdatesPath $NotInstalled = $True $DotNetCoreItems.GetSubKeyNames() |其中 { $_ -Match "Microsoft .NET Core.*Windows Server Hosting" } | ForEach-Object { $NotInstalled = $False Write-Host "主机已安装 $_" } (...)
    • 对于最新版本的框架不适合我
    【解决方案2】:

    您可以使用powershell检查托管模块是否注册到IIS

    在本地 powershell 会话中

    Import-module WebAdministration
    $vm_dotnet_core_hosting_module = Get-WebGlobalModule | where-object { $_.name.ToLower() -eq "aspnetcoremodule" }
    if (!$vm_dotnet_core_hosting_module)
    {
        throw ".Net core hosting module is not installed"
    }
    

    如果你想在远程会话中替换前 2 行

    Invoke-Command -Session $Session {Import-module WebAdministration}
    $vm_dotnet_core_hosting_module = Invoke-Command -Session $Session {Get-WebGlobalModule | where-object { $_.name.ToLower() -eq "aspnetcoremodule" }}
    

    【讨论】:

    • 我喜欢这个答案,但在启动 64 位和 32 位 powershell 时遇到了困难
    • 如何执行这个?
    • 这只有在您不关心安装哪个版本时才可以...
    • 此命令即使在安装后也不显示模块。对我来说,这是因为该模块实际上被称为AspNetCoreModuleV2
    【解决方案3】:

    如果你安装了 .NET Core CLI,你可以试试:

    dotnet --list-runtimes
    

    将打印如下内容:

    Microsoft.NETCore.App 3.0.0 [c:\program files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [c:\program files\dotnet\shared\Microsoft.NETCore.App]

    参考:Microsoft doc

    【讨论】:

    • 机器上有运行时并不意味着已经安装了IIS模块
    【解决方案4】:

    如果允许您引入约束,一种选择是只允许“自包含应用程序”,因为它们不需要任何额外的安装。这也使诸如“安装了什么版本”之类的问题消失了。

    如果您需要支持“便携式应用程序”,您只需执行以下命令即可检查 dotnet.exe 是否可用:

    where dotnet

    然后您可以检查版本:

    dotnet --version

    这也可以让您在担心时检查 .NET Core 的版本。

    【讨论】:

      【解决方案5】:

      请注意,这些命令需要以管理员身份在 powershell 中运行。

      使用IISAdministration powershell module可以列出所有模块如下:

      Import-Module IISAdministration
      
      (Get-IISConfigSection -SectionPath "system.webServer/globalModules" `
      | Get-IISConfigCollection).GetCollection() `
      | select @{n="name";e={$_.GetAttributeValue("name")}}, `
               @{n="image";e={$_.GetAttributeValue("image")}}, `
               @{n="preCondition";e={$_.GetAttributeValue("preCondition")}}
      

      所以一个测试可以是例如

      $coreModule =
      (Get-IISConfigSection -SectionPath "system.webServer/globalModules" `
      | Get-IISConfigCollection).GetCollection() `
      | where {$_.GetAttributeValue("name") -eq "AspNetCoreModuleV2" }
      
      if (-not $coreModule)
      {
          throw 'AspNetCoreModuleV2 is not installed'
      }
      

      在撰写本文时请注意,AspNetCoreModuleAspNetcoreModuleV2 均单独列出。未来可能会出现其他版本。

      【讨论】:

        【解决方案6】:

        您也可以双击 DotNetCore.1.0.1-WindowsHosting.exe
        如果已安装 .NET Core Windows Server Hosting 捆绑包,则打开窗口将显示:

        1. 修改设置标签
        2. 修复和卸载按钮

        修复和卸载按钮以及修改设置标签。

        【讨论】:

        • 很好 - 无需使用注册表或 powershell。
        【解决方案7】:

        你可以看看

        HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall{ab4f6e29-67a1-47d9-b2ab-43348a9bbae4}

        并确保存在“Microsoft .NET Core 1.0.0 - Windows Server Hosting”

        【讨论】:

        • 这只会检测到使用了特定的安装程序...可能在下一次更新可用时不再起作用。
        • 没错,我认为它与检测遗留 .net 版本的工作方式相同:您在注册表中四处寻找框架特定版本的特定键。
        猜你喜欢
        • 2011-11-14
        • 2014-05-24
        • 2011-01-21
        • 1970-01-01
        • 2020-04-18
        • 1970-01-01
        • 1970-01-01
        • 2016-11-13
        • 1970-01-01
        相关资源
        最近更新 更多