Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using Visual Studio 2015 Community edition. I just tried to debug a piece of code but I'm having a hard time with the debugger. The code I used is in the end of the post.

The code is inside a Loaded event of a WPF window which is wired correctly and executed. Symptoms I'm facing:

  • Breakpoint on line
    C#
    System.Xml.Linq.XElement element1 = System.Xml.Linq.XElement.Parse(xml1);

    is not hit at all
  • Breakpoint on line
    C#
    System.Xml.Linq.XElement element2 = System.Xml.Linq.XElement.Parse(xml2);

    is hit correctly
  • Using QuickWatch on element1 I get
    error CS0103: The name 'element1' does not exist in the current context
  • Using QuickWatch on element2 works correctly
  • Using QuickWatch on xml1 gives
    error CS0103: The name 'xml1' does not exist in the current context
  • Using QuickWatch on xml2 works correctly
  • The loop in the end is iterated three times but the System.Diag... line is not executed at all

When I copy the exact same code to Visual Studio 2013 it works correctly. So it feels a bit like xml1 and code referring to it would have been removed during the compilation in Visual Studio 2015.

Any hints what I could try?



The code I used:
C#
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string xml1 = @"<hostgroup id=""2080"">
    <instance_id>1</instance_id>
    <hostgroup_name>Avanade-Hypervisors</hostgroup_name>
    <members>
      <host id=""2061"">
        <host_name>ZACN-PSPR-HVR13</host_name>
      </host>
      <host id=""2062"">
        <host_name>ZACN-PSPR-HVR14</host_name>
      </host>
      <host id=""2063"">
        <host_name>ZACN-PSPR-HVR15</host_name>
      </host>
    </members>
</hostgroup>";

            string xml2 = @"<hoststatuslist>
 <hoststatus id=""49294"">
    <host_id>2061</host_id>
    <name>ZACN-VSPR-WAP02</name>
    <display_name>ZACN-PSPR-HVR13</display_name>
    <alias>management_servers</alias>
    <status_update_time>2015-09-19 08:00:31</status_update_time>
    <last_state_change>2015-09-18 19:25:06</last_state_change>
    <last_hard_state_change>2015-09-16 18:18:04</last_hard_state_change>
    <last_time_up>2015-09-19 08:00:31</last_time_up>
    <last_time_down>2015-09-18 19:25:06</last_time_down>
  </hoststatus>
<hoststatus id=""49294"">
    <host_id>2062</host_id>
    <name>ZACN-VSPR-WAP02</name>
    <display_name>ZACN-PSPR-HVR14</display_name>
    <alias>linux_servers</alias>
    <status_update_time>2015-09-19 06:00:31</status_update_time>
    <last_state_change>2015-09-18 19:25:06</last_state_change>
    <last_hard_state_change>2015-09-16 18:18:04</last_hard_state_change>
    <last_time_up>2015-09-19 08:00:31</last_time_up>
    <last_time_down>2015-09-18 19:25:06</last_time_down>
  </hoststatus>
<hoststatus id=""49294"">
    <host_id>2063</host_id>
    <name>ZACN-VSPR-WAP02</name>
    <display_name>ZACN-PSPR-HVR15</display_name>
    <alias>localhost</alias>
    <status_update_time>2015-09-19 09:00:31</status_update_time>
    <last_state_change>2015-09-18 19:25:06</last_state_change>
    <last_hard_state_change>2015-09-16 18:18:04</last_hard_state_change>
    <last_time_up>2015-09-19 08:00:31</last_time_up>
    <last_time_down>2015-09-18 19:25:06</last_time_down>
  </hoststatus>
</hoststatuslist>";

            System.Xml.Linq.XElement element1 = System.Xml.Linq.XElement.Parse(xml1);

            System.Xml.Linq.XElement element2 = System.Xml.Linq.XElement.Parse(xml2);

            var querya = from item1 in element1.Descendants("host")
                         join item2 in element2.Descendants("hoststatus")
                         on item1.Attribute("id").Value equals item2.Elements("host_id").First().Value
                         select new
                         {
                             Host = item2.Elements("display_name").First().Value,
                             UpTime = item2.Elements("last_time_up").First().Value,
                             Alias = item2.Elements("alias").First().Value
                         };

            foreach(var a in querya)
            {
                System.Diagnostics.Debug.WriteLine(a.Host);
            }
        }
Posted
Comments
OriginalGriff 19-Sep-15 12:06pm    
"Any hints what I could try?"
Turn it off and back on again? :laugh:

Seriously, I'd try a clean and full rebuild, just to check that it hasn't thrown a wobbly and not built the latest version. (I've had that a couple of times with VS in various versions - frustrating). And I'd turn it off and back on again as well...

[edit]
Have you tried looking at the IL to see what it has generated?
[/edit]
Wendelius 19-Sep-15 12:10pm    
I turned it off and now no breakpoints are hit. Did I brake it? :laugh:

But seriously: Sorry, I forgot to mention that I tried both clean and restart. However I didn't investigate the IL, good point. I'll have a look, hold on :)
Wendelius 19-Sep-15 12:37pm    
Two things I discovered. First of all it's (partly) my mistake. While the project was set to Debug the configuration for the solution was Release. When changing the solution config to Debug, everything worked. I just still don't quite understand the behaviour though...

Going through the IL I discovered that I'm not able to reliably interpret it so I'm not certain if it contains any oddities...
Wendelius 19-Sep-15 13:33pm    
Care to put the suggestion as a solution so I could comment and accept it.
[no name] 19-Sep-15 14:10pm    
In such Situation I usually enter a MessagBox(...) to see whether VS has a Problem.

1 solution

"Any hints what I could try?"
Turn it off and back on again? :laugh:

Seriously, I'd try a clean and full rebuild, just to check that it hasn't thrown a wobbly and not built the latest version. (I've had that a couple of times with VS in various versions - frustrating). And I'd turn it off and back on again as well...

[edit]
Have you tried looking at the IL to see what it has generated?
[/edit]

(Posted as solution as per request: "Care to put the suggestion as a solution so I could comment and accept it.")
 
Share this answer
 
Comments
Wendelius 19-Sep-15 14:18pm    
When investigating the IL I noticed that the code is optimized. The solution configuration in the toolbar and the project configuration window showed Debug but when opening the Solution configuration window the configuration was Release. Changing it to Debug solved the problem.

Not sure if this is related to not hitting the first breakpoint but the output in C# in ILSpy showed quite a big difference between the definitions in Debug and Release code:

Debug:
------
string text = "<hostgroup id="\"2080\"">\r\n...";
string text2 = "<hoststatuslist>\r\n...";
XElement xElement = XElement.Parse(text);
XElement xElement2 = XElement.Parse(text2);
...

Release
--------
string arg_0B_0 = "<hostgroup id="\"2080\"">\r\n...";
string text = "<hoststatuslist>\r\n...";
XContainer arg_21_0 = XElement.Parse(arg_0B_0);
XElement xElement = XElement.Parse(text);
...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900