Conditional Compilation in Visual Studio(C#) - c#

I am attempting to make it so that we can have our application to behave differently based on the presence of a preprocessor directive. I've read that you can create build configurations to define different preprocessor directives based on which build you do. Well, I am not seeing anything in Visual Studio to do this.. I know how to do it from the command line, but not how to do it within the automated environment of VS 2008.
Can someone tell me how to create a new build configuration which has preprocessor directives set in it?
Also, not sure if it has anything to do with it, but our project is an ASP.Net website

I do not even see a way to control the
DEBUG symbol, and it is an ASP.Net
website
Ok, there's the problem. The ASP.net web site option gives you a lot less control over builds, etc as it doesn't even use a project file.
If it is at all feasible for you to switch, switch over to an ASP.net web application. A web app will behave much more like a typical C# project (you have a project file, and the contents of the csproj file control what is in the app instead of just the directory structure, etc.
After you have converted, you should see the options that you are expecting.
Here's a link with directions for converting: How To Convert ASP.NET Website to ASP.NET Web Application

If you right-click the project and select "Properties", then select the Build tab, you can enter custom compilation symbols in "Conditional compilation symbols".
For example, if your code looks like this:
#if DEBUG
// do something
#else
// do something else
#end
You can set "DEBUG" as a Conditional compilation symbol.
You can set different values for different build configurations, by changing the "Configuration" drop-down.

Related

c# preprocessor directives based on web publish profile

I have a test and production web server. My project has a piece of code that I would like in place on the test server, but not in place on the production server.
#if (DEBUG) doesn't work, because I prefer both to be published in RELEASE mode.
Is there any way to set up a preprocessor directive based on which web Publish profile is being used?
The easiest way to accomplish this is to create separate build configurations for each of the environments you need to publish to. You can clone your new build configurations from Release. Then, in the project settings, enter the name of your build configuration in the Conditional compilation symbols box.
So, for example, we have a Stage build configuration which is identical to Release except it defines the STAGE compiler constant. Then, in the code, you can use #if (STAGE) checks.
If you are like me wondering where MS moved everything, on VS2022, here it is:

DNN7 module cannot debug due to break point not hit

When i using the Default DNN7 create modules (DotNetNuke Compiled Module), i want try to debug on Page_load
CustomModuleController objCustomModules = new CustomModuleController();
and make and breakpoint on the line, or anyline on Page_load, it given the error.
The breakpoint will not currently be hit. No symbols have been loaded for this document.
I would recommend that you check out the templates at http://christoctemplate.codeplex.com the templates in the visual studio starter kit frankly don't work.
That being said, if you want to make sure that the current template will work, check the following
What is the build location of the DLL (right click on the project, choose properties, check the build tab)
Do you have debugging enabled in the web.config file?
Are you building in DEBUG or RELEASE mode?
What if you want to develop dynamic modules (non-compiled), in separate project, other than the website proj?
The Christoc Templates are all compiled, right?

asp.net webconfig Debug=true and Solution Configuration as Debug [duplicate]

Setting <compilation debug="true/false"> in Web.config seems to do things that you can also set in Visual Studio in Project properties, Build tab. Are they connected somehow? Does one of them takes precedence when compiling?
They are not connected. The compilation tag is ASP.NET only, while the project option one is for Windows Forms, console, WPF and so on.
ASP.NET compilation is so special, so you have to dive further to learn about every piece of it,
http://msdn.microsoft.com/en-us/library/ms178466.aspx
Set compilation debug="true" to insert debugging symbols into the compiled page. Because this affects performance, set this value to true only during development.

Monotouch Condtitional Compilation for Distribution

I'm aware you can include directives to compile and run different code based on whether you're in debug or release mode. Can you do the same sort of thing when you build the project for distribution?
I ask because I've just submitted and had approved an app update which points to a web service on our test server rather than live!
Yes, you sure can.
In MonoDevelop under Project Options->Compiler->Define Symbols, choose the appropriate build configuration (AppStore), and you can make a new variable such as PRODUCTION.
You can then use:
#if PRODUCTION
#endif
Throughout your code.

Why doesn't switching to release in VS set the debug parameter to false in web.config

In VS2008 (and earlier if I'm not mistaking) I have dropdown that lets me choose between Debug and Release mode. All well.
When I switch VS to Release, the debug attribute in my web.config files isn't set to false at all though.
Is this the way it is supposed to be? I am bound to forget to set it to the correct value on deploying.. What measures should I take to make sure this is working like it should?
This is one solution to this problem:
http://blog.aggregatedintelligence.com/2009/04/aspnet-never-again-fear-publishing-your.html
Well your web.config would probably be different for debug and release (connection string, passwords, etc...) but if it's not, look at a postbuild event which would copy over a different file.
Also check this blog post from Scott Guthrie.
Changing release mode will not change web.config, however when you build your web app, it will build the dll for only C# files in release mode where else your web.config's debug on/off is used by IIS to build debug/release version of ASPX markup files.
The build flavour just affects how the code is compiled, it does not affect your configuration files. So yes, to answer your question, this is how it is supposd to be.
The element is a good solution if you have access to the machine.config of your server, which hosts only production applications.
I usually modify the web.config file when generating the deployed files as part of the automated build process. For example web deployment projects can perform web.config section replacement. There are a number of reasons I don't like web deployment projects and I tend to do it with a simple VBS file that modifies the file using MSXML.
The answer you selected from Bobby is not correct. Visual Studio builds the files for you in release while you are in VStudio.
IIS compiles the code at startup with that setting when you deploy. Not the bin directory, but the App_Code and the code behind files.
You should precompile your app before deployment which will compile your code behinds and App_Code dir into dlls in the bin directory.
The deployment tools automatically switch that setting if you set the deployment tool to Release
I use web deployment projects. http://weblogs.asp.net/scottgu/archive/2008/01/28/vs-2008-web-deployment-project-support-released.aspx

Categories

Resources