Are there any differences between System.Web.HttpContext.Current.IsDebuggingEnabled and System.Diagnostics.Debugger.IsAttached?
If so, what are the exact differences besides the fact that one is only for web applications while the other works in all kind of projects?
HttpContext.IsDebuggingEnabled is about the compilation setting in the web.config. Debugger.IsAttached defines if there is actually an active debugger listening to the information coming from the web server.
See the explanation at DotnetPerls regarding HttpContext.IsDebuggingEnabled:
Debug mode is not the default. ... When you do not set debug="true" in Web.config, the site is compiled in Release mode.
Regarding your question why the first 'one is only for web applications': web applications have the ability to compile at run-time, while all other .NET products are pre-compiled. Because of this, you can define in the web.config if the build is done in Debug or Release mode. This is a ASP.NET only option, so the property is only available there.
As answer to your second question, why the first option is only for ASP.NET: There is also a way for a Windows application to check it's build status: by checking the DebuggableAttribute as explained in How to check if an assembly was built using Debug or Release configuration?.
IsDebuggingEnabled refers to "debug mode" which does not necessarily mean a debugger is actually attached. You can set ASP.NET websites into debug mode by setting <compilation debug="true"> in your web.config file.
When a website is in debug mode, JIT optimizations are not applied to code contained within your view files (.aspx, .cshtml, etc) as well as any runtime-compiled code-behind files or the App_Code directory. There are also other effects.
Related
I know this has been asked before but I just cannot figure this out. I believe I have covered everything that has been brought up already but I'll cover those.
I am getting this message when I try to step into a service that is a project that is currently in my solution:
I have 3 projects in my solution:
SuburbanCustPortal <-- my website
SuburbanHub <-- my serice
WebsiteLogging <-- my logging project (no significance here)
I read that I should check the following items:
Make sure that debug is on. I have this in both of my project's web.config:
Make sure they are both using the same .net version. They are both on the .net framework 4.0.
Make sure Enable Just Your Code is unchecked:
The service is pointed to a local url:
I can pull up the service in my browser without error:
This is my settings for iis:
I have restarted visual studio, the computer and removed the service and added it back.
I cannot, for the life of me, figure this out. If I have missed anything I am willing to give it a shot.
It is very important that I get this resolved so I can get this project out this weekend so any help would be greatly appreciated.
IN RESPONSE TO Sanket Shah
I do not have the option w3wp.exe:
SOMETHING I FORGOT TO MENTION
I have set debug=true in both of my projects:
<compilation targetFramework="4.0" debug="true">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
Also, I wanted to add that I have been able to step into my service before but since coming back to the project recently, I am not able too. I have a break point on the line that calls the service and when I try to F11 in to it, I get the above message.
IN RESPONSE TO Pawel
I have tried setting symbols the follow ways and neither allowed me to step in:
I have even tried to use the Microsoft symbols:
IN RESPONSE TO Pawel #2
ADDITION INFO
I just noticed this, I'm not sure if it is related:
ADDITIONAL INFO ABOUT DEBUG MODE
I have all projects set in debug mode:
Your solution/project/settings file(s) might be / seems like it is corrupted. If you create a new solution from scratch, adding new projects which mimic your current structure, you can try and see if debugging works properly.
If that works, gradually move the code over while keeping checking that debug keeps working.
I don't know why this doesn't work.
I have a work-around for you, which you verified in the comments: add System.Diagnostics.Debugger.Break() somewhere in your service after it starts. This should pop up some sort of exception window, with a list of Visual Studio instances open. Pick the one with your solution and click OK, and it should attach correctly to that location.
Annoying? Yes. In my experience with Visual Studio, it can be very finicky sometimes about debugging into Windows services. This is the only reliable way to do it that I know about.
In addition to other answers, you can do two things:
Close your solution, delete [solution-name].suo file, reopen solution. See if your problem still persists.
Open a command prompt as admin, cd %windir%\Microsoft.net\Framework\v4.0.30319, execute this aspnet_regiis.exe -i.
In solution, have you tried to set all assemblies (client and server) to start in debug mode rather than trying to attach it after launch ? (right click on solution in the top of Solution explorer, startup, multiple startup project, choose "start" for every assembly which is an entry point : server, client, etc).
Have you tried to run only the "server" part in debug mode (the one which is not debuggable) and call it for example with soap ui ?
You should try to see if you can run it directly and debug it without the "step in" from another process, or if it also fails to load in debugger even if started directly.
FYI, if you use IIS Express it's normal you don't see "w3wp.exe", which is for IIS. You may have a iisexpress.exe process, or in some cases aspnet_wp.exe.
Don't forget to check "show processes for all users" in Debug->Attach to process window if you choose to hook on existing process rather than doing that I was saying first.
Go to Properties of the solution select Multi statup project and select all projects with start, then debug the solution.
I have a strange issue
#if (!DEBUG)
checkLicense();
#endif
This is working correctly in both Release and Debug configurations. But when I try to publish using release configuration this condition does not execute. It looks like publish is using the debug dll.
What have I missed?
There is a setting to control whether the DEBUG constant is defined for each project based on the different deployment modes. See this answer to verify that the constant is being defined for 'release mode' by making sure the Define DEBUG constant checkbox is checked.
If the box isn't checked, then your debug code is being removed by the preprocessor before compiling your site and no code will execute, even if you include the ELSE as the other answer suggested.
If that doesn't work, then another possibility is be that the machine you're running your release code on could have its machine.config with the deployment element:
<deployment retail="true" />
This element overrides the web.config setting for your application and sets the debug flag to false for all .NET applications on the machine.
So if possible, check that. Although I think the first option I gave you is much more likely.
first of all you have to make sure that you step into this "if" and use try\catch.
#if (!DEBUG)
MessageBox.Show("I'm in");
try{
checkLicense();}
catch{MessageBox.Show("ERROR IN checkLicense");}
#endif
and than take a version out and run it. If you are in the "if", you will know it and if you have an exception, you will know it also.
You can try also
#if DEBUG
....
#else
.....
Alternative to my other answer because I just noticed the mvc tag, whereas before I assumed you were using WebForms.
Is the code snippet you provided by any chance in a View?
Because if that's the case, then the DEBUG setting for your project is not going to be respected when the View is generated at runtime - it will only respect a debug flag in your web.config file.
See this answer for more information.
I have an asp.net web app, and I have added some related class projects to my solution file. When I run the web app, I want to break and step through the code in the class (when a class is referenced).
I don't get an error messages. The code in the class project just does not kick in.
I have searched and read this post Debugging a Class Library but no luck.
How do I get that to work?
Can you try to stop the ASP.NET process? I usually have this problem when the asp.net service is still running, and I compile (by asp.net service I mean the icon that appears near computer clock). Try to close that, recompile, then run.
Are you sure you have <compilation debug="true"/> set in your web.config file? Are the classes in the same assembly as the rest of the application?
edit: The only other thing I can suggest is stop IIS and/or all instances of the vs development server, clean the project, rebuild, and give it another shot. Also be sure there is only one web.config and you aren't running in release against Web.Release.config or something.
This sometimes happens. Check Debug>Modules to see if there is your dll loaded. It seems that VS debugger doesn't have .pdb file available. Clear Temporary ASP.NET Files in your .NET folder, Clean/Rebuild and try debug again.
Also do not forget to run VS as Administrator. Try to Attach to process instead of F5.
You can call in your code this function, and the debugger will pop-up
System.Diagnostics.Debugger.Launch();
alternative you can call the Debug.Fail("Stop me to see what next");
Visual Studio 2010 kills (there is no other word) data in one of the arguments of the function in the unsafe block.
What could cause this error? The following message shows by the debugger.
Cannot obtain value of local or argument as it is not available at this instruction pointer, possibly because it has been optimized away.
Go to Project Properties and under Build Make sure that the "Optimize Code" checkbox is unchecked.
Also, set the "Debug Info" dropdown to "Full" in the Advanced Options (Under Build tab).
Also
In VS 2015 Community Edition
go to
Debug->Options or Tools->Options
and check
Debugging->General->Suppress JIT optimization on module load (Managed only)
If you compile with optimizations enabled, then many variables will be removed; for example:
SomeType value = GetValue();
DoSomething(value);
here the local variable value would typically get removed, keeping the value on the stack instead - a bit like as if you had written:
DoSomething(GetValue());
Also, if a return value isn't used at all, then it will be dropped via "pop" (rather than stored in a local via "stloc", and again; the local will not exist).
Because of this, in such a build the debugger can't get the current value of value because it doesn't exist - it only exists for the brief instant between GetValue() and DoSomething(...).
So; if you want to debug... don't use a release build! or at least, disable optimizations while you debug.
In visual Studio 2017
goto Debug->Option then check Debugging->general->
and check this option
I just ran into this and I was running under Release build configuration instead of Debug build configuration. Once I switched back to Debug my variable showed in the watch again.
For web applications there is another issue which is important and it is selecting correct configuration during application publish process.
You may build your app in debug mode, but it might happen you publish it in release mode which omptimzes code by default but IDE may mislead you since it shows debug mode while published code is in release mode.
You can see details in below snapshot:
I have faced the same issue and the solution for me is change Solution Configuration from Release to Debug. Hope it helps
When I was faced with the same problem I just had to clean my solution before rebuilding. That took care of it for me.
Regarding the problem with "Optimize code" property being UNCHECKED yet the code still compiling as optimized: What finally helped me after trying everything was checking the "Enable unmanaged code debugging" checkbox on the same settings page (Project properties - Debug). It doesn't directly relate to the code optimization, but with this enabled, VS no longer optimizes my library and I can debug.
In my case, I was working on a web api project and although the project was set correctly to full debug, I was still seeing this error every time I attached to the IIS process I was trying to debug. Then I realized the publish profile was set to use the Release configuration. So one more place to check is your publish profile if you're using the 'Publish' feature of your dotnet web api project.
I found that I had the same problem when I was running a project and debugging by attaching to an IIS process. I also was running in Debug mode with optimizations turned off. While I thought the code compiled fine, when I detached and tried to compile, one of the references was not found. This was due to another developer here that made modifications and changed the location of the reference. The reference did not show up with the alert symbol, so I thought everything was fine until I did the compilation. Once fixing the reference and running again it worked.
As an additional answer for those experiencing this issue when debugging an Azure websites' web app:
When deploying from GitHub, for example, the code is compiled in Azure server optimized by default.
I tell the server to compile in a debuggable way by setting SCM_BUILD_ARGS to /p:Configuration=Debug
but there are more options. See this:
http://azure.microsoft.com/blog/2014/05/08/introduction-to-remote-debugging-on-azure-web-sites-part-3-multi-instance-environment-and-git/
In Visual Studio 2017 or 2015:
Go to the Solution right click on solution then select Properties-> select all the Configuration-> Debug then click OK.
After that Rebuild and Run,this solution worked for me.
Had the same issue before with a WPF application and all the solutions here did NOT solve the issue. The problem was that the Module was already optimized so the previous solutions DO NOT WORKS (or are not enough to solve the issue):
"Optimize Code" checkbox un-Checked
"Suppress JIT optimization on module load" checked
Solution configuration on DEBUG
The module is still loaded Optimized. See following screenshot:
To SOLVE this issue you have to delete the optimized module. To find the optimized module path you can use a tool like Process Hacker.
Double click your program in the "Process panel" then in the new window open the tab ".NET Assemblies". Then in the column "Native image path" you find all Optimized modules paths. Locate the one you want to de-optimize and delete the folder (see screenshot below):
(I blurred my company name for obvious reasons)
Restart your application (with check box in step 1 correctly checked) and it should works.
Note: The file may be locked as it was opened by another process, try closing Visual Studio. If the file is still locked you can use a program like Lock Hunter
Check to see if you have a Debuggable attribute in your AssemblyInfo file. If there is, remove it and rebuild your solution to see if the local variables become available.
My debuggable attribute was set to: DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints which according to this MSDN article tells the JIT compiler to use optimizations. I removed this line from my AssemblyInfo.cs file and the local variables were available.
In Visual Studio 2012:
Go to the project properties -> Debug -> Uncheck "Enable the Visual Studio hosting process"
I had the same issue. Tried all the above and found I also had to delete everything inside {PROJECT_ROOT}\bin\Release\netcoreapp2.2 and {PROJECT_ROOT}\obj\Release\netcoreapp2.2 for my project. Its definitely releated to publishing because although I use Deployment tools / bitbucket on my Azure Web App, I did try the Build >> Publish >> Publish to Azure because I wanted to inspect which files were actually deployed.
I'm currently trying to find out why my InjectableAttributes never get to the filter part.
Therefor I linked in the source project directly so I could easily put breakpoints etc.
When I build however I get the following message:
The following module was built either with optimizations enabled or without debug information:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\[a bunch of randomly named directories]\assembly\[more random names]\MvcTurbine.Web.DLL]
To debug this module, change its project build configuration to Debug mode. To suppress this message, disable the 'Warn if no user code on launch' debugger option.
I checked the project's properties. It's configuration is set to "Active(Debug)" and the "Optimize code" check box is unchecked.
UPDATE
I checked again if I didn't by accident include the file rather then the project, but the references seem correct.
Just to be sure I also removed all existing bins so that all libraries are definitely rebuild somewhere, but to no avail.
UPDATE
In the advanced Build window the Debug output is set to Full and all options are identical to projects that are building debug info.
The config manager shows that the project is being built and has the exact same settings as all the other options.
UPDATE
In the Debug->Windows-Modules window the symbol status for the turbine library is PDB file does not match image I removed all corresponding pdb files on my entire system to make sure it gets re-generated, but to no avail.
To make sure I also searched for the dll on my entire system and removed them. Nothing.
What can be preventing VS from creating debug information?
I've just experienced the same problem this morning, using VS2010. I solved it by deleting all the build artefacts, e.g. executables, libraries, PDBs, etc. This was in \bin\Debug, \bin\Release, \obj\Debug, and \obj\Release (including sub-folders). Then I recompiled the debug configuration, and was back in business.
Check that debug info is being generated for the project. You can do this by opening the Build tab on the Project Properties page and clicking the "Advanced..." button. Check the "Debug Info" setting. See this MSDN article for an explanation of what each option means.
If that doesn't solve it, check that the MvcTurbine project is actually being built (Build -> Configuration Manager).
It sounds like you've referenced the MvcTurbine.Web.DLL file rather than the MvcTurbine project. Try removing the reference and re-adding it as a project reference.
Update Are you sure the DLL isn't coming from the GAC? Do Debug->Windows->Modules when the app is running and check the Path column.