Can you tell me how to handle 'BundleConfig.cs' file's below line when we are working on debug mode ?
Because I need to ignore below line on debug mode.How can I do that ? Any help would be highly appreciated.
BundleTable.EnableOptimizations = true;
The easiest way is to use the #if Preprocessor directive
#if DEBUG
BundleTable.EnableOptimizations = false;
#else
BundleTable.EnableOptimizations = true;
#endif
If your app is running in debug mode, Visual Studio defines DEBUG for you. On the other hand, if your app is running in release, DEBUG will be undefined.
In order to check if it's a release version, you check for DEBUG to not be defined
#if !DEBUG
BundleTable.EnableOptimizations = true;
#endif
PS: For obvious reasons, there is no RELEASE flag.
Related
I have an azure function written in C#. I want to enable a Http triggered Azure Function only if it is in a Development environment.
I tested this feature with using a pre-processor directive such as #if and i am able to show/hide an Azure function in the output window.
private const string HttpTestFn = "RunHttpTest";
#if DEBUG
[FunctionName(HttpTestFn)]
#endif
However, i want to extend this to work based on the Environment such as
#if Environment.IsDevelopment()
[FunctionName(HttpTestFn)]
#endif
How do i achieve this?
TIA!
This won't work
#if Environment.IsDevelopment()
You would create a specific build configuration for the dev environment, when deploying to that environment, you use the build configuration to build, package and deploy to that environment
So you keep the
#if DEBUG
[FunctionName(HttpTestFn)]
#endif
I am not sure what do you use to deploy your app to different environments, but will assume you are using some sort of a pipeline to build and deploy the app, so when building, you would use a different build parameters for development environment than for other.
For release you would use
dotnet build --configuration Release
but for your development environment you would use
dotnet build --configuration DEBUG
This way the version being deployed to dev won't have that function.
Another note, I would put the #if #endif at the start and end of the function file rather than just around the functionname attribute.
I have got this in the left side of the script:
#if DEBUG
Console.WriteLine("...in debug modus.");
Thread.Sleep(3000);
#endif
But it doesn't only run when in Debug modus but it runs all the time. I don't see other formatting then this on the Internet so why does this code run all the time?
I have code that tests which folder a project is running from to determine if it is the testing version or production version. I know there is a way to do this with the difference between debug version and released (which I want to do in the future but I don't know how yet). So for now, this is a workaround to get me what I need. This code works correctly when I run from Visual Studio but not when my scheduled task runs the compiled version.
string projectPath = System.IO.Directory.GetCurrentDirectory();
var TestVersion = true;
if (projectPath == #"H:\Automation\RefreshData\RefreshData\bin\Debug" || projectPath == #"\\atlelz1fs03.atalanta.local\USERS\Automation\RefreshData\RefreshData\bin\Debug" || projectPath == #"H:\Automation\RefreshData" || projectPath == #"\\atlelz1fs03.atalanta.local\USERS\Automation\RefreshData")
{
TestVersion = false;
}
Which folder should I be looking at for the compiled version? or is there a better way for me to determine this?
Use precompiler statements:
#if DEBUG
var TestVersion = true;
#endif
#if !DEBUG
var TestVersion = false;
#endif
then when in debug mode run with debug selected in visual studio (as normal) when you release this change it to Release:
this will set TestVersion to false in your released code only. (BTW I have lots of configuration options, just ignore these, you will likely have Debug and Release only)
This works even better if you use continuous integration to compile your code for you, you can then configure this to do this, so you don't forget.
I get this message when I try to attach the debugger to a staging publish in Visual Studio:
As you can see here, it is set to debug mode:
I disabled "Just My Code":
I also tried ticking this box:
I still get the error message. I have tried cleaning and rebuilding.
Is it possible in C# to do run specific lines codes in debug setting and other in say release settings.
if #debug
//run some lines of code
else
// run different lines of code
You can do something like:
#if DEBUG
// Debug Code
#else
// Release Code
#endif
I use that in WCF services to run it as a console app in debug, but as a Windows Service in release
HTH,
Rupert.
Read this blog post If You’re Using “#if DEBUG”, You’re Doing it Wrong, the author suggests using System.Diagnostics.ConditionalAttribute:
[Conditional("DEBUG")]
private static void DebugMethod()
{
// Debugging code
}