.Net Core 無法發行到IIS網站

前言
 
之前都是使用MsDeploy來發行.net framework專案,部份專案改用.Net Core後,部署到IIS,遇到一些無法發行的問題,例如:dll使用中,無法更新、驗證錯誤等。

執行環境
  • Window 10
  • .Net Core 3.1
  • Visual Studio 2019
  • Azure DevOps

問題排解
命令列,透過發行檔發行,需使用管理權限執行。
dotnet build -c Release /p:DeployOnBuild=true /p:PublishProfile=iis-remote  /p:Password=xxxx

1.dll使用中,無法更新

解決方式: 使用app_offline.htm,讓應用程式先停止。
  • 在發行檔加入如下即可:
    <EnableMSDeployAppOffline>true</EnableMSDeployAppOffline>

  • 微軟文件寫道,專案檔上引用Microsoft.NET.Sdk.Web,在發行時就會自動加入app_offline並在發行後移除。
            但我看專案明明就有引用,但就是試不出來....。-->此方式失敗,記錄備查
ps:在發行檔pubxml也試用,但沒用。

2.發行到遠方主機,出現憑證錯誤。

MSDEPLOY : error Code: ERROR_CERTIFICATE_VALIDATION_FAILED

解決方式:在發行檔加入如下:
    <AllowUntrustedCertificate>true</AllowUntrustedCertificate>

3.在Azure DevOps 使用Pipeline發行時,出現SDK錯誤
##[error]C:\Program Files\dotnet\sdk\3.1.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(234,5):
Error NETSDK1047: Assets file 'D:\a\1\s\Nop.Web\Server\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v3.1/win-x64'.
Ensure that restore has run and that you have included 'netcoreapp3.1'
in the TargetFrameworks for your project. You may also need to include 'win-x64' in your project's RuntimeIdentifiers.

解決方式:在專案檔的最上方加入<RuntimeIdentifier>如下:紅字
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <DisableImplicitComponentsAnalyzers>true</DisableImplicitComponentsAnalyzers>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>


參考文章

這個網誌中的熱門文章

IIS 設定只允許特定IP進入