You’ve all had it happen. You just put the finishing touches on your project and you hit the Publish button. The files are automatically copied to the file server and users start get the updates right away. You double-check the publish and then you realize that you published the DEBUG version with all your program stops, disabled error checking, and wide open access to all program features!
Custom MSBuild targets to the rescue! Just add the following to your project file and it will prevent a publish when the DEBUG constant is defined. Place it at the bottom of your project file right before the </Project> tag. (For an example of editing your project file, see my post on Obfuscating a ClickOnce Publish.)
<!-- The following makes sure we don't try to publish a configuration that defines the DEBUG constant -->
<Target Name="BeforePublish">
<Error Condition="'$(DefineDebug)'=='true'"
Text="You attempted to publish a configuration that defines the DEBUG constant!" />
</Target>
By the way, the DEBUG constant is the constant that you use in your code as a compiler constant, for example #If DEBUG Then. It can be found in the ‘Compile’ tab under ‘My Project’ by clicking ‘Advanced Compile Options…’:
Edit: It looks like some people have had issues with the way I got things working. I was using a VB.Net forms application in VS2005 (and I think I tested in 2008 as well). The key is to find which constant works best for your situation. Drew Mcpherson says that DebugSymbols worked for him rather than DefineDebug.
If you want even more control, another way of choosing a compiler constant is to define your own. You’ll notice that below the “Define DEBUG Constant” box there is a textbox for defining custom constants. If, for example, you have three configurations, such as Debug, Release, and Publish, you could define a constant on the Publish configuration to prevent any other from allowing a publish.



I tried this and Visual Studio 2010 happily published it anyway!
I thought it was too good to be true
http://img842.imageshack.us/img842/9189/debugpublishoops.png
This solution seems simple, doomed to work. So I tried it with Visual Studio 2008 on a C# project. Unfortunately, it did not work and — sorry, I could not figure out why it didn’t.
The example in this article is ALMOST correct. The correct code to add to your project file is as follows (note the name of the variable is "DebugSymbols", not "DefineDebug" as stated in the article):
<!– The following makes sure we don’t try to publish a configuration that defines the DEBUG constant –>
<Target Name="BeforePublish">
<Error Condition="’$(DebugSymbols)’ == ‘true’" Text="You attempted to publish a configuration that defines the DEBUG constant!" />
</Target>
[...] http://www.nathanpjones.com/wp/2010/05/preventing-clickonce-publishing-a-debug-configuration/ This entry was posted in .Net, ClickOnce, Deployment. Bookmark the permalink. ← Hello world! LikeBe the first to like this post. [...]