Meca
Meca

Reputation: 148

How do I restart an IIS Website during publishing of a .NET Core website in Visual Studio?

I am using Web Deploy in Visual Studio to publish my .Net Core website. I often get an error because some of the files on the server are locked. The error shows up as Publish has encountered an error. I can get around the problem by stopping and starting the website in IIS on the server.

On my ASP.NET MVC websites, I could publish without issue because Web Deploy would insert a site maintenance file into the root of the publish and that would cause IIS to shut the site down. When the publish was complete, the site maintenance file would be removed and the website would restart.

How do I restart an IIS Website during publishing of a .NET Core website in Visual Studio?

Upvotes: 0

Views: 1257

Answers (1)

Igor Goyda
Igor Goyda

Reputation: 2017

You can deploy always in a new folder and just switch root folder of the running site. I do this by creating an archive and moving this archive to a production server and, after all, run a deploy script (powershell). This is not a straight decision of your problem, just kind of workaround which can give you an idea.

Example of powershell script for deploying:

Param(
    [string]$project,
    [string]$archive,
    [string]$config
)

#####################################################################
# Functions
#

function Unzip
{
    Param([string]$fname, [string]$output)
    if ($PSVersionTable.PSVersion.Major -ge 5)
    {
        Expand-Archive $fname $output -Force
    }
    else
    {
        & .\7za.exe x $fname -o"$output" -bb0 -bd -y
    }
}

#####################################################################

$current=(Get-Date).ToString("yyyy.MM.dd")
$base_path="D:"
$backup="$base_path\$project\bak-$current"

echo "Deploying project $project"

echo "Copy new binaries to: $base_path\$project\$current"
Unzip $archive $base_path\$project\$current

echo "Copying new config to: $base_path\$project\$current"
Copy-Item "$config" -Destination "$base_path\$project\$current\" -Force

echo "Creating log folder"
New-Item "$base_path\$project\$current\logs" -type directory -force

echo "Switch IIS physical path for website: $website"
Import-Module WebAdministration
Set-ItemProperty "IIS:\Sites\$website" -Name PhysicalPath -Value "$base_path\$project\$current"

Upvotes: 1

Related Questions