In general, its pretty easy to extend the MSBuild process in ASP.NET projects using the [ProjectName].wpp.targets file. I recently attempted to answer a StackOverflow question asking how to automatically deploy on every Visual Studio build using this technique. My first overly-confident answer was to add the same MSBuild properties that would be used from the command line in the .csproj or .wpp.target file:
<PropertyGroup> <DeployOnBuild>true</DeployOnBuild> <PublishProfile>Local</PublishProfile> </PropertyGroup>
I didn’t even try this out before answering because I was sure this would work. Alas no, it didn’t work so I spent the next few days researching this further.
The DeployOnBuild property specifically was ignored when set statically in the project file. Apparently it is a special property that must be set globally on the commandline.
As an alternative, I found that I could call MSBuild again passing the property and it worked.
You’ll notice I created some new properties to make it a bit easier to understand. The MSBuild call references the same project so I also placed a Condition on the call to avoid a circular reference. You can also customize which publish profile is deployed by updating the AutoDeployPublishProfileName MSBuild property.
The working source for this solution is on Github at https://github.com/rschiefer/AutoDeployFromVSBuild
Please leave a comment below if this blog post was helpful or you have additional questions. Happy deployments!