How to detect the compilation mode (debug/release/release-xml) of cross platform assemblies (XForms.Android / XForms.iOS/ XForms.UWP) by programatically?
I have tried with refer below links.
https://www.hanselman.com/blog/HowToProgrammaticallyDetectIfAnAssemblyIsCompiledInDebugOrReleaseMode.aspx
This was working for all the assemblies except XForms.Android / XForms.iOS/ XForms.UWP assemblies.
Could you please provide me a solution for this?
You can do the following to execute code for debug or release:
#if DEBUG
// execute DEBUG code
#else
// execute release code
#endif
Note that you need to make sure that the "Define DEBUG constant" is checked. Not sure how it is for Xamarin studio, but in Visual Studio you can find it in the project properties in the "build" tab:
You can find some further information about #if in this SO question.
Related
I have a c# .net core project that is using preprocessor directives ( #if DEBUG) to enable unsafe SSL Connections during debugging. Now when I release-build the whole project and open the generated dll with dotPeek I still find the code surrounded by the '#if DEBUG' in there. I would have thought that this code is removed during the build. I also checked that in the BuildSettings from VS the "define DEBUG" is unchecked for the Release-Build-Configuration. Is my idea of how this works incorrect ? Where in the .dll would I find the definition of 'DEBUG' at all - at the top of the file or somewhere else ?
#if DEBUG
DisableSecureSSLConnections();
#endif
As #canton7 mentioned in the comment section. The actual problem was the .pdb file was available for the disassembler. This caused dotPeek to show the actual source
I have developed an UWP app that uses a lot of NuGet packages (MvvmLight, SQLite, ...) and other resources (Syncfusion controls).
I encounter a bug with the Store app, which is already published for tests, that is not present when I build the app in "Debug" mode.
So, I've tried to debug in "Release" mode, with the checked options "Compile with .NET Native tool chain" and "Optimize code". The build ends successfully, but I encounter an exception with a Syncfusion control (SfDataGrid) on the main page of the app. I would like use breakpoints to understand what happens, but they are deactivated as I build the app in "Release" mode.
If I build the app in "Debug" mode, with the same options checked ("Compile with .NET Native tool chain" and "Optimize code"), I don't encounter the same bug with the Syncfusion control, and the defined breakpoints are well keeped.
So I don't see how I could fix my problem:
if I create a new solution and built it in "Release" mode, the breakpoints are well keeped, and I can debug the code
if I build Syncfusion samples in "Release" mode, it's the same thing: the breakpoints are well keeped, and I can debug the code
I have compared the "build" parameters of the app and the other ones: they are the same
I have also looked at the "Just-in-Time" page, in the Visual Studio "options". I've got the following error: "Another debugger has registered itself as the Just-In-Time debugger. To repair, enable Just-In-Time debugging or run Visual Studio repair.". Ive tried to "repair" Visual Studio, but it's always the same thing...
Here is the "Options" settings:
And the result in solution, where breakpoints are disabled:
Would you have any explanation? How could I do to debug my app in "Release" mode?
[Edit 1]: add some details after further investigations
My app is based on a "template" like Template10, called Nentang. The structure of the project is the same, and they share a big part of references or NuGets packages.
But if I compare the build result of the "blank" Nentang and my solution, there are some differences that I don't understand:
as explained, on my app, the breakpoints and debug don't work in "Release" mode, and I can see that almost all modules don't have any "Symbol File":
=> only "ntdll.dll" and "KernelBase.dll" are linked to thier pdb file in a local directory: "C:\Users\myname\AppData\Local\Temp\SymbolCache"
on the "blank" Nentang app, the breakpoints and debug work well in "Release" mode, and I can see that almost all modules have a "Symbol File":
=> allmost half of the modules are linked to the same file in the "project" directory: "C:\Projects\Samples...\Nentang.UWP\bin\x64\Release\AppX\Nentang.UWP.pdb"
=> another quarter of the modules are linked to the same file of a "system" directory: "C:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs\Microsoft.NET.Native.Framework.1.3\1.3\x64\ret\Native\SharedLibrary.pdb"
=> the other modules are not linked to a Symbol File: it's the case of "ntdll.dll" and "KernelBase.dll"
How could I restore the "Symbol files" of my project?
I have also remarked a "strange" parameter in the properties of my solution, that is not present is the Nentang properties:
There is this parameter: "f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs"
What does it mean? Could it explain my problem?
Debugging optimized code is always a challenge - even more so with .NETNative. Here are a few things you could try:
Disable Just My Code
Suppress JIT Optimizations: This will not help for modules built with .NETNative toolchain. If the exception occurs in Release builds without .NETNative, then check the debugger option called "Suppress JIT optimization on module load (Managed Only)". As the name implies, this will cause the CLR to JIT compile code unoptimized, which will allow you to set breakpoints and inspect locals.
Look at the Output window for clues as to what went wrong. The exception message will be there and the preceding messages may help diagnose the cause.
Debug your application with Native debug engine. You can do this by checking the Native checkbox under the Debug tab of the project properties.
Last resort is to debug the assembly.
I've been doing the following in my C# code:
#if DEBUG
//Debugger only code
#endif
But for some reason when I do the same in my C# console application (that is built as a Windows service in VS 2010) it doesn't seem to work and for both Release and Debug configurations it looks like DEBUG is not defined.
Do I need to set something for that preprocessor constant to work?
When using the 'Debug' configuration, you have to go to project's Properties -> Build
and tick the 'Define DEBUG constant' box.
Either that, or define the conditional compilation symbol: DEBUG. As you can do with any other custom compilation symbol :)
I am new to C# and Visual Studio, and this may be a silly question but I do have to find an answer. My question is is it possible to differentiate debug and release builds in such a way the certain lines of code(in my case MessageBox.Show(...)) are compiled if I build under Debug mode, but are left out if I build under Release mode?
Wrap the lines of code in #if DEBUG statements:
#if DEBUG
MessageBox.Show("Message");
#endif
If you want to do different things in release then you can have:
#if DEBUG
MessageBox.Show("Message");
#else
// Write to log file
#endif
Source
you can use the compiler instruction "#if DEBUG" followed by "#endif" so your code will be compiled only on debug mode.
find more details here:
http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
In addition to #if DEBUG, you can also use the Assert static method on the Debug class in System.Diagnostics to check things and show a message box if the value is wrong. e.g:
Debug.Assert(IsValid);
Will show a message in the debug build if IsValid is false.
You can specify different conditional compilation symbols in the project's properties (an article here; MSDN documentation for Visual Studio). Then, using conditional compilation it's easy to include/exclude certain lines. Like this:
#if DEBUG
MessageBox.Show(...);
#end
Yes, you can use
#if DEBUG
MessageBox.Show....
#endif
If you check build version, you find you have variables defined in project: DEBUG is only defined in debug release.
If you want you can also switch the behaviour using
#if DEBUG
MessageBox.Show....
#else
// do something else here in release mode
#endif
I have several of these in my code which have been working fine so far:
#If DEBUG Then
... some code here
#End If
Now, i am noticing that, lately, the code inside the " #If DEBUG Then ... #End If"
gets execute also in "Release Mode".
This is strange and did not happen before. What could have happened so that
the #If DEBUG are now being ignored (they are ignored both in debug in the IDE or the final executable) ?
I have applied Clean, Rebuild, etc.: no luck. Thank you for any hints and help.
-Pam
Firstly, make sure you understand the difference between how you're running the code and how you're building it. Too many people equate "launching in a debugger" with "the debug version" and "launching not in a debugger" with "the release version". They're completely orthogonal - you can launch a release build in a debugger (typically with less information available) and you can launch a debug build not in a debugger. Apologies if you were already aware of this.
Now, assuming you really have changed the project configuration you're building to Release, you need to check the project properties for that specific configuration. I don't know what it looks like in VB, but in C# in the project properties, in the build tab, there will be a list of defined symbols - that is what affects whether #if DEBUG code is built or not. Perhaps someone has copied over the project configuration from Debug into Release?
EDIT: One way to check this at build time is:
#if DEBUG
#error This shouldn't happen
#endif
In a release build, that should build without error. In debug, it won't.
EDIT: Another option is that your overall solution configuration is now referring to the wrong project configuration types. I can't remember the exact menu name, but if you look around Project for Configuration Manager, you should be able to bring up a grid mapping "Project" and "Solution Configuration" to the project configuration to build.
C# Project ( Visual Studio )
go to: Project Properties -> Build(tab)
Select Configuration: Release
Uncheck "Define DEBUG constant"
Now select Configuration: Debug
Check "Define DEBUG constant"
In your code, you can now type the following ( DEBUG with uppercase )
#IF DEBUG
// Debugging code goes here
#ENDIF
Under Project Properties / Compile / Advanced Compile Options there is
a checkbox called "Define Debug Constant" that sets this.
Check out: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24658238.html
EDIT: Try this initializing with this:
#If CONFIG = "Debug" Then
#CONST DEBUG = true
#if CONFIG = "Release" Then
#CONST DEBUG = false
Did you, by any chance, tick the "Define DEBUG constant" for the Release configuration, while you were in the Project Properties / Build?
Also make sure you are not building the project-level Debug configuration within the solution-level Release configuration (see the Configuration Manager).
Also remember #if DEBUG must be in uppercase. e.g. #if debug won't work.
Had a similar problem where "DEBUG" was never true. Tried by doing an uncheck and check of the "Define DEBUG constant" checkbox and rebuilding everytime but that did not work.
My solution was to define "DEBUG" manually in the "Conditional compilation symbols" textbox for the Debug configuration. When rebuilding, Visual Studio 2019 automatically removed the DEBUG symbol from the textbox (because this indeed should not be there) and from then on it worked again. When i switched from Debug to Release the correct lines got greyed out.
This seems to be a possible bug in VS 2019 (16.4.5)?
undefine DEBUG and that will not execute that portion.
If you are using ASP.NET make sure about this line in Web.Config file:
<compilation debug="false" targetFramework="4.5">
So if debug="true" your project runs in DEBUG mode.
If you are using VB.NETFramework v4.5 then use like
If Debugger.IsAttached Then
'... some code here
End If