.net core 解决由文件锁定导致版本迭代失败的问题

如果应用正在运行,部署文件夹中的文件会被锁定。 在部署期间,无法覆盖已锁定的文件,微软官方文档给出三种方案解决该问题

参考使用IIS在windows上进行托管

方法(一)在服务器上的 IIS 管理器中手动停止应用池。

方法(二)利用app_offline.htm 文件

存在 app_offline.htm 文件时,ASP.NET Core 模块会通过发送回 app_offline.htm 文件的内容来响应请求。 删除 app_offline.htm 文件后,下一个请求将启动应用。 如果应用在 shutdownTimeLimit(可在 web.config 文件设置,默认10s 中定义的秒数之后仍在运行,ASP.NET Core 模块将终止正在运行的进程。

我习惯用svn做版本发布迭代管理,根据上述内容可将版本发布做成一个批处理进行执行

::设置app_offline.htm路径 和项目路径
set app_offline_path=F:\publish\app_offline.htm
::设置项目发布路径
set publish_path=F:\publish\DeveloperCore171
::设置TortoiseProc.exe
set svn_path=D:\Program Files\TortoiseSVN\bin\TortoiseProc.exe

copy /y %app_offline_path%  %publish_path%\
echo "复制完成"
echo "svn更新"
"%svn_path%" /command:update /path:"%publish_path%" /closeonend:1  
echo "删除文件"
del %publish_path%\app_offline.htm

方法(三)使用 PowerShell 来停止和重新启动应用池(需要使用 PowerShell 5 或更高版本)

这个微软给出了脚本demo如下:

$webAppPoolName = 'APP_POOL_NAME'

# Stop the AppPool
if((Get-WebAppPoolState $webAppPoolName).Value -ne 'Stopped') {
    Stop-WebAppPool -Name $webAppPoolName
    while((Get-WebAppPoolState $webAppPoolName).Value -ne 'Stopped') {
        Start-Sleep -s 1
    }
    Write-Host `-AppPool Stopped
}

# Provide script commands here to deploy the app

# Restart the AppPool
if((Get-WebAppPoolState $webAppPoolName).Value -ne 'Started') {
    Start-WebAppPool -Name $webAppPoolName
    while((Get-WebAppPoolState $webAppPoolName).Value -ne 'Started') {
        Start-Sleep -s 1
    }
    Write-Host `-AppPool Started
}

在此,针对该脚本执行可能遇到的坑做一下总结:

1.首次执行该脚本时,由于执行策略默认禁止执行需要先执行策略更改 Set-ExecutionPolicy RemoteSigned 

PS C:\> Set-ExecutionPolicy RemoteSigned 
#执行策略更改
#执行策略可以防止您执行不信任的脚本。更改执行策略可能会使您面临 about_Execution_Policies
#帮助主题中所述的安全风险。是否要更改执行策略?
#[Y] 是(Y)  [N] 否(N)  [S] 挂起(S)  [?] 帮助 (默认值为“Y”):

2.双击执行.ps1脚本是,不是以管理员身份开启的powershell,需要在脚本开始执行时追加判断,如果不是管理员开启,强制以管理员开启

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

3.完整脚本如下

#首次执行PS C:\> Set-ExecutionPolicy RemoteSigned 
#执行策略更改
#执行策略可以防止您执行不信任的脚本。更改执行策略可能会使您面临 about_Execution_Policies
#帮助主题中所述的安全风险。是否要更改执行策略?
#[Y] 是(Y)  [N] 否(N)  [S] 挂起(S)  [?] 帮助 (默认值为“Y”):

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

#设置默认应用池名称
$webAppPoolName = 'DefaultAppPool'
#设置项目发布路径
$publishpath='F:\publish\DeveloperCore171'

# Stop the AppPool
if((Get-WebAppPoolState $webAppPoolName).Value -ne 'Stopped') {
    Stop-WebAppPool -Name $webAppPoolName
    while((Get-WebAppPoolState $webAppPoolName).Value -ne 'Stopped') {
        Start-Sleep -s 1
    }
    Write-Host `-AppPool Stopped
}

# Provide script commands here to deploy the app
TortoiseProc.exe /command:update /path:$publishpath /closeonend:1  

# Restart the AppPool
if((Get-WebAppPoolState $webAppPoolName).Value -ne 'Started') {
    Start-WebAppPool -Name $webAppPoolName
    while((Get-WebAppPoolState $webAppPoolName).Value -ne 'Started') {
        Start-Sleep -s 1
    }
    Write-Host `-AppPool Started
}

完整脚本demo下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值