自定义ASP.NET发布

问题描述 投票:1回答:2

我有一个asp.net项目,需要在每次发布后运行一些代码。

我试图通过以下方式实现这一目标:

  1. 使用vs项目文件
  2. 使用外部构建工具Cake(c#make) 1.)因此找到了与此问题相关的一些主题: After Publish event in Visual Studio Why does MSBuild ignore my BeforePublish target? Running Target after files are published to FileSystem

所以我建议将以下代码添加到项目文件中:

  <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>

但只有Before-&AfterBuild才能执行。所有其他目标完全被忽略。也许是因为这些其他主题使用VS 2010和2012.在VS 2017中有没有一种工作方式可以做到这一点?

2.)当我在任务中使用以下代码时,我得到的输出与我用VS编译时的输出相同。

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

因此,这也不是一个可行的解决方案。

我感谢任何帮助让这种方式以某种方式工作。

c# asp.net automation msbuild visual-studio-2017
2个回答
0
投票

这取决于你想要做什么,但如果你想让你的代码在发布后运行,你需要将你的任务放在Microsoft.Web.Publishing.targets之一的目标之后(搜索文件以查看完整的目标列表) 。通常你需要AfterTargets="MSDeployPublish"


0
投票

1.)因此找到了与此问题相关的一些主题。所以我建议将以下代码添加到我的项目文件中。实际上,在我在vs2017中测试之后,从详细的输出日志中,我在发布时可以找到的内容如下图所示:

enter image description here enter image description here

1.在VS2017中,当在IDE中发布项目时,似乎没有有效的发布目标来重新定义或AfterTargets。因此,我担心你无法通过在VS中发布来实现这一目标。

2.相反,我认为你可以考虑使用msbuild工具。将下面的测试目标脚本添加到您的发布配置文件中(对我来说: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>

打开vs2017的开发人员命令提示符,然后键入命令:

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

它将在webpublish进程后运行测试目标。

2.)当我在任务中使用以下代码时,我得到的输出与我用VS编译时的输出相同。不确定是否使用此代码。但你可以使用测试目标来做你想要的。将MSBuild Task加入测试目标可以为你的#2实现这个目标。

正如您所提到的,您有一些代码,首先将这些代码编译为.exe,并将Command Task添加到测试目标中可能有所帮助。另外:根据你上面的信息,似乎你使用的是文件系统和asp.net,所以我使用asp.net web app来测试(不是.net核心)。预计会有反馈。

© www.soinside.com 2019 - 2024. All rights reserved.