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?
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 am trying to remotely debug a project with ASP.NET Core 2.1 that uses IIS as a server using the remote debugger tool, since I was able to establish the connection to the remote server and debug some things using breakpoints, the problem that is currently happening to me is that I can debug the file code as controllers,services, but I cannot debug the Program.cs file code using breakpoints.
Any ideas?
you could check the below things if you are not getting any error but breakpoint didn’t hit:
1)if you are running your code on more than one machine so the first check you are debugging the correct place code.
2)make sure your codding is running and you set the System.Diagnostics.Debugger.Break or __debugbreak( for c++) in your code where you want to set the breakpoint. (do not forget to build your project after adding this code)
3)If you are debugging optimized code, make sure the function where your breakpoint is set isn’t being inlined into another function. The Debugger.Break test described in the previous check can work to test this issue as well.
Add a call to Launch at the beginning of the OnStart()method.
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
}
How to: Debug the OnStart Method
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.
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.
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
}