Search code examples
c#visual-studiomsbuildmsdeploy

How do we get the same publish command used in VisualStudio Publish


I'm using MSDeploy.exe to publish my webapp:

msdeploy.exe 
-verb:sync
-source:package=mypackage.zip
-dest:auto,computerName=MyServer
-allowUntrusted

This will sync my package to destination server and delete all the unneeded files during the process. Sometimes resources (eg: MyResource.dll) are locked by IIS worker process and it wouldn't allow the delete operation, thus deployment failed with error:

Access to the path "C:\MyResource.dll" is denied.

However, if I use the Publish feature from within VisualStudio, my website gets published to the server without any problem. The locked files remained in the folder and the error is simply ignored.

I figured VS probably uses this switch:

-enableRule:DoNotDeleteRule

But I'm not very sure about that.

My questions:-

  1. Can we get the publish command used by VS through output window or log files?
  2. Is there a way to stop application pool on a remote server? (this will help release the locked resources) *IISRESET is not suitable because I don't want to bring down all websites just to deploy one webapp.

I've also tried PSEXEC:

psexec.exe -s \\MyServer appcmd.exe stop apppool /apppool.name=MyAppPool

It runs fine in command line, but when I put it in script for build-automation, it returned error:

The handle is invalid.
Couldn't access MyServer.

(banging my head against the wall)


Solution

  • To answer my own questions:

    1) Can we get the publish command used by VS through output window or log files?

    Yes, we can output the command to output window.
    All you need to do is simply add a file named <YourProjectName>.wpp.targets to the root of your project directory. And edit the contents as below:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" DefaultTargets="Build"
             xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <UseMsdeployExe>true</UseMsdeployExe>
      </PropertyGroup>
    </Project>
    

    Now you can Publish using Visual Studio and the MSDeploy command can be seen in the output window.

    Checkout Sayed Ibrahim Hashimi blog here for more info.


    2) Is there a way to stop application pool on a remote server?

    I managed to start/stop/recycle remote AppPool using -preSync -postSync of MSDeploy command.

    MSDeploy.exe 
    -source:package="mypackage.zip"
    -dest:auto,ComputerName=MyServer,IncludeAcls=False
    -verb:sync
    -disableLink:AppPoolExtension
    -disableLink:ContentExtension
    -disableLink:CertificateExtension
    -allowUntrusted
    -preSync:runCommand="C:\Windows\System32\inetsrv\appcmd stop apppool <AppPoolName>"
    -postSync:runCommand="C:\Windows\System32\inetsrv\appcmd start apppool <AppPoolName>"
    

    Hope this helps =)