4

I have an asp.net project and need to run some code after each publish.

I tried to acomplish this using to ways:

  1. Useing the vs project file
  2. Useing external build tool Cake (c# make)

    1.) So found some topics related to this issue:

So like suggested i added following code to my project file:

  <Target Name="BeforePublish">
    <Message Text="BeforePublish"></Message>
    <Warning Text="BeforePublish"></Warning>
  </Target>
  <Target Name="AfterPublish">
    <Message Text="AfterPublish"></Message>
    <Warning Text="AfterPublish"></Warning>
  </Target>
  <Target Name="MSDeployPublish" >
    <Message Text="MSDeployPublish"/>
    <Warning Text="MSDeployPublish"/>
  </Target>
  <Target Name="PipelinePreDeployCopyAllFilesToOneFolder" >
    <Message Text="PipelinePreDeployCopyAllFilesToOneFolder" />
    <Warning Text="PipelinePreDeployCopyAllFilesToOneFolder" />
  </Target>
  <Target Name="BeforeBuild">
    <Message Text="BeforeBuild"></Message>
    <Warning Text="BeforeBuild"></Warning>
  </Target>
  <Target Name="AfterBuild">
    <Message Text="AfterBuild"></Message>
    <Warning Text="AfterBuild"></Warning>
  </Target>

But only the Before- & AfterBuild get executed. All the other targets just get ignored completly. Maybe it's because these other topics use VS 2010 and 2012. Is there a working way to do this in VS 2017?

2.) When i use following code in a task I don't get same output as when i would have compiled it with VS.

   MSBuild(projectFile, new MSBuildSettings()
   .WithProperty("OutDir", publishDir)
   .WithProperty("DeployTarget", "WebPublish")
   .WithProperty("DeployOnBuild", "true")
   .WithProperty("WebPublishMethod", "FileSystem")
   .WithProperty("Configuration", "Release")
   );

Therefore this isn't a viable solution atm as well.

I am thankful for any help getting this to work one way or another.

Ryanless
  • 134
  • 1
  • 13
  • Hi friend, does my answer work for this issue?If there has latest news, feel free to share here. – LoLance Mar 14 '19 at 05:07

2 Answers2

1

It depends on what you want to do after but if you want your code to run after publish you need to put your task after one of the targets from Microsoft.Web.Publishing.targets (search for the file to see full list of targets). Usually you need AfterTargets="MSDeployPublish"

Victor Fialkin
  • 165
  • 4
  • 7
0

1.) So found some topics related to this issue. So like suggested i added following code to my project file. Actually,after my test in vs2017, from the output log with detailed, what I can find when publishing is like pics below:

enter image description here enter image description here

1.It seems in VS2017, when publishing the project in IDE, there has no valid publish target to redefine or AfterTargets. So I'm afraid you can't achieve this goal by publishing in VS.

2.Instead, I think you can consider using msbuild tool. Add test target script below into your Publish profile (For me:FolderProfile.pubxml):

<Project...>
...
  <Target Name="Test" AfterTargets="WebFileSystemPublish">
    <Message Text="I'm a test target after webpublish"/>
    <Move SourceFiles="C:\Ase\favicon.ico" DestinationFolder="C:\tomorrow"/>
  </Target>
</Project>

Open your developer command prompt for vs2017, and type command like:

msbuild C:\xxx\WebApplication55\WebApplication55.sln /p:DeployOnBuild=true /p:PublishProfile=C:\xxx\repos\WebApplication55\WebApplication55\Properties\PublishProfiles\FolderProfile.pubxml 

It will run the test target after webpublish process.

2.) When i use following code in a task I don't get same output as when i would have compiled it with VS. Not sure you use this code for what. But you can use test target to do what you want.Add a MSBuild Task into test target can achieve this goal for your #2.

And as you mentioned you have some code, compile those code to a .exe first, and add a Command Task into test target could help. In addition: According to your info above, it seems you use file system and asp.net, so I use asp.net web app to test(not .net core). A feedback would be expected.

Community
  • 1
  • 1
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • So, after searching more I found the target "CopyAllFilesToSingleFolderForPackage" which works though it is suboptimal. Yes, I do use FileSystem Publish, sadly puting target into the publishprofile didn't solve the problem. for now I will just use the slightly crappy version with CopyAllFiles. – Ryanless Mar 14 '19 at 16:38
  • Hi, friend, I've tried the CopyAllFilesToSingleFolderForPackage, it can work for new publish in VS IDE. But for publishprofile, do you mean if you use the msbuild by command-line, it doesn't work or only the target "Test" doesn't work? – LoLance Mar 15 '19 at 01:42