Dean
Dean

Reputation: 4594

Publish in debug mode but compiler removes code inside #if debug

I'm only publishing in debug mode to test on my local machine and I was hoping the '#if debug' mode would work so that I could test features that I don't want in production yet.

When I publish in debug mode the web.config still has

<system.web>
    <compilation debug="true" targetFramework="4.0">
</system.web>

but when I use reflector on the project's dll the code that was like this

#if debug
    PlaceHolder1.Visible = true;
#endif

is non-existent. I figure the compiler has removed it.

NOTE: I'm NOT talking about a build, I'm talking about publishing. Doing a debug build works as I expect it to with the code above still present

Is this expected behavior? Is there a way to get the compiler to include those bits of code when I'm publishing in debug mode? Am I going about this all wrong?

Update: In response to @dash's comment my Package/Publish Web settings are:

Package/Publish Web settings

Upvotes: 2

Views: 5094

Answers (3)

haiiaaa
haiiaaa

Reputation: 183

In Visual Studio:

1. Right click on your project choose properties.
2. Go to the "Build" tab
3. Select Release in the "Configuration" combobox.
4. Check the "Define DEBUG constant"-checkbox.

Make sure you don't forget to remove it before deploying to production.

Edit: Since you are using lowercase debug you can instead of step 4 enter debug (lowercase) in "Conditional compilation symbols" in step 4: Conditional compilation symbols

Upvotes: 2

Nick Butler
Nick Butler

Reputation: 24383

C# preprocessor symbols are case-sensitive. Try:

#if DEBUG

Upvotes: 5

Tim S.
Tim S.

Reputation: 56536

Yes, that is expected behavior. The conditional compilation directives (e.g. #if debug) are considered at compile-time only. The C# code is compiled when you build it. What is the compilation debug option you pointed out then? I'm not sure, but I think it changes how aspx pages are compiled or how the web site runs the built code, which happens just before the page is viewed instead of when the code is built.

Upvotes: 2

Related Questions