以编程方式发布 Web 应用程序
我正在尝试编写一个 Visual Studio 扩展,它允许我在一个解决方案中发布多个 Web 应用程序,类似于在所有项目上使用一键发布功能。
DTE2 service = (DTE2)this.GetService(typeof(DTE));
Projects projects = service.Solution.Projects;
SolutionBuild2 build = (SolutionBuild2)service.Solution.SolutionBuild;
foreach (Project project in projects)
{
build.PublishProject("Release", project.UniqueName, true);
}
当我尝试运行此代码时,输出窗口中的唯一结果是:
Error: Object reference not set to an instance of an object.
========== Publish: 0 succeeded, 0 failed, 0 skipped ==========
...这并没有告诉我太多信息。有没有办法找出问题所在?
我还看到有一个界面 IVsPublishableProjectCfg,但似乎没有任何如何使用它的示例。
是否有另一种方式以编程方式将 Web 应用程序发布到某个目录,类似于一键发布功能的工作原理?
I'm trying to write a visual studio extension that allows me to publish multiple web applications in a solution, similar to using the one-click publish feature on all the projects.
DTE2 service = (DTE2)this.GetService(typeof(DTE));
Projects projects = service.Solution.Projects;
SolutionBuild2 build = (SolutionBuild2)service.Solution.SolutionBuild;
foreach (Project project in projects)
{
build.PublishProject("Release", project.UniqueName, true);
}
When I try to run this code, the only result in the output window is this:
Error: Object reference not set to an instance of an object.
========== Publish: 0 succeeded, 0 failed, 0 skipped ==========
... which doesn't tell me much. Is there a way to find out what's going wrong?
I also see there an interface IVsPublishableProjectCfg, but there doesn't seem to be any examples of how to use it.
Is there another way to programmatically publish web applications to a certain directory, similar to how the one-click publish feature works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行此操作的另一种方法是模拟单击解决方案资源管理器中的右键单击“发布”上下文菜单项,如以下类似问题所示:
DTE.ExecuteCommand 并等待
请注意,由于这是一个异步命令,因此您需要构建在逻辑上利用发布后事件来迭代您的项目集合(其中的一些详细信息显示在所包含的答案之一中)。
An alternate way to do this is to simulate clicking of the right-click "Publish" context menu item in Solution explorer, as in this similar question:
DTE.ExecuteCommand and wait
Note that, as this is an asynchronous command, you will need to build in logic to tap into the post-publish events to iterate over your collection of projects (some detail on this is shown in one of the included answers).
使用 msbuild.exe 可执行文件(这就是您构建的方式),将 sln 文件作为项目参数传递,并定位“Publish”目标。
您可以在命令行中声明一些属性,也可以使用自己的 .targets 文件静态声明它们;在 .csproj 中引用此 .targets 文件。
.targets 文件的内容可能如下所示:
.. 然后可以在其中包含
XML ..然后,您可以定义任何其他 MSBuild 任务,甚至是 shell 命令
<执行..>
。关于 MSBuild 的更一般信息:https://msdn.microsoft.com/en-us /library/dd393573.aspx
此外,如果您坚持用 C# 编写发布逻辑,您可以考虑上述路径并定义自定义 MSBuild 任务,即
了解如何此处。
Use the msbuild.exe executable (which is how you're building anyway), pass your sln file as the project argument, and target the "Publish" target.
You can declare some properties in the command line or you can declare them statically using your own .targets file; reference this .targets file in your .csproj.
The contents of the .targets file might look like this:
.. the
<Target ..>
XML could then have in it ..You can then define any other MSBuild tasks, even shell commands with
<Exec ..>
.More generally on MSBuild here: https://msdn.microsoft.com/en-us/library/dd393573.aspx
Also if you insist on writing your publish logic in C#, you might consider the path described above and define a custom MSBuild task, i.e.
Learn how here.
基本上,它归结为以某种方式调用 msbuild,如下所示:
我为此编写了一个 PowerShell cmdlet,我可以从 NuGet 包管理器控制台调用它。
Basically, it boils down to calling msbuild somehow like this:
I have written a PowerShell cmdlet for this, which I can call from the NuGet package manager console.