开发者

Programmatically publish web application

开发者 https://www.devze.com 2023-03-28 03:38 出处:网络
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.

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开发者_C百科.
========== 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?


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消