Search code examples
iismsbuildvisual-studio-2019publishtarget

Is there any MSBuild target that is executed after publishing a .NET web application?


I need to execute a command after publishing a .NET Web Application in Visual Studio 2019.

I tried following custom target in .csproj and .pubxml, but nothing gets printed after publish happens:

enter image description here


Solution

  • From your description, I think you are using a Net Framework web project. Since Framework web publish does not contain the default target called Publish. But Net Core web publish has it.

    For Net Framework web publish, it is special. There is no such target that runs after the generation of the publish folder. It means that all the targets under the framework web publish are like BeforePublish. It is quite annoying.

    Compared with net core web publish, the old framework web publish does have many defects.

    However, I have a function that can make the target executes after the generation of the publish folder.

    Solution

    1) First, you should install a nuget package called MSBuild.Extension.Pack which is an extra extension for msbuild tasks.

    It has a task called AsyncExec which can executes with the following targets. While it is running, it will continue to execute the next steps of publish. You only need to make the command sleep for a while and wait the publish folder to be generated so that you can modify the conent of the publish folder..

    2) write this under xxx.pubxml file:

    <Target Name="custom step" AfterTargets="GatherAllFilesToPublish">
    
        <AsyncExec Command="powershell.exe sleep 2;powershell.exe -file xxxxx"></AsyncExec>
              
    </Target>
    

    The sleep time is up to the the size of your project and how long it takes to publish the folder.

    And this function works well in my side.