以编程方式发布 Web 应用程序

发布于 2024-11-29 14:13:20 字数 866 浏览 1 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌化 2024-12-06 14:13:20

执行此操作的另一种方法是模拟单击解决方案资源管理器中的右键单击“发布”上下文菜单项,如以下类似问题所示:
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).

赠意 2024-12-06 14:13:20

使用 msbuild.exe 可执行文件(这就是您构建的方式),将 sln 文件作为项目参数传递,并定位“Publish”目标。

msbuild SlnFolders.sln /t:Publish

您可以在命令行中声明一些属性,也可以使用自己的 .targets 文件静态声明它们;在 .csproj 中引用此 .targets 文件。

<Import Project="MyPublishCustomizations.targets" />

.targets 文件的内容可能如下所示:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="Publish">
    </Target>
    <!-- or .. -->
    <Target Name="CustomPublish" BeforeTargets="Publish">
    </Target>
</Project>

.. 然后可以在其中包含 XML ..

<PropertyGroup>
    <MyCustomPath>\\our_server\our_share</MyCustomPath>
</PropertyGroup>
<Copy SourceFiles="$(OutDir)\*" DestinationFiles="$(MyCustomPath)" />

然后,您可以定义任何其他 MSBuild 任务,甚至是 shell 命令<执行..>

关于 MSBuild 的更一般信息:https://msdn.microsoft.com/en-us /library/dd393573.aspx

此外,如果您坚持用 C# 编写发布逻辑,您可以考虑上述路径并定义自定义 MSBuild 任务,即

<ExecuteMyCustomTaskDefinedInCSharp ParameterA="Hello" ParameterB="World" />

了解如何此处

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.

msbuild SlnFolders.sln /t:Publish

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.

<Import Project="MyPublishCustomizations.targets" />

The contents of the .targets file might look like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="Publish">
    </Target>
    <!-- or .. -->
    <Target Name="CustomPublish" BeforeTargets="Publish">
    </Target>
</Project>

.. the <Target ..> XML could then have in it ..

<PropertyGroup>
    <MyCustomPath>\\our_server\our_share</MyCustomPath>
</PropertyGroup>
<Copy SourceFiles="$(OutDir)\*" DestinationFiles="$(MyCustomPath)" />

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.

<ExecuteMyCustomTaskDefinedInCSharp ParameterA="Hello" ParameterB="World" />

Learn how here.

烟花肆意 2024-12-06 14:13:20

基本上,它归结为以某种方式调用 msbuild,如下所示:

msbuild.exe "MyProject.csproj" /p:PublishProfile="MyPublishProfile.pubxml" /p:DeployOnBuild=true /p:VisualStudioVersion="14.0"

我为此编写了一个 PowerShell cmdlet,我可以从 NuGet 包管理器控制台调用它。

Basically, it boils down to calling msbuild somehow like this:

msbuild.exe "MyProject.csproj" /p:PublishProfile="MyPublishProfile.pubxml" /p:DeployOnBuild=true /p:VisualStudioVersion="14.0"

I have written a PowerShell cmdlet for this, which I can call from the NuGet package manager console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文