title: Why am I having a weird issue comparing object instances on IIS 7?
body: <p>I have a really weird issue that I can't figure out with comparing objects on IIS 7.  We are in the process of deploying our old IIS 6 based ASP.NET application on IIS 7, however we have this equality comparison issue that we can't seem to figure out. </p>
  
  <p>Let me start out by saying that I have the same assemblies and code running both on IIS 6 and IIS 7, however the comparison of the objects is differing with the same code both on IIS 6 and IIS 7.  Here is an example of what my object looks like:</p>
  
  <pre><code>class Country : EntityBase {
      public int CountryID { get; set; }
      public string Name { get; set; }
  
      public override bool Equals(object obj) {
          if (obj == null || !(obj is Country))
              return false;
  
          Country c = (Country)obj;
          return CountryID == c.CountryID;
      }
  
      public override int GetHashCode() {
          return CountryID.GetHashCode();
      }
  }
  </code></pre>
  
  <p>I have the following code in an ASPX page both on IIS 6 and IIS 7:</p>
  
  <pre><code>&lt;% foreach(var country in proposalCountries) { %&gt;
  &lt;%= country.Country.CountryID %&gt;
  &lt;%= country.Country.CountryID.GetHashCode() %&gt;
  &lt;%= country.Country.GetHashCode() %&gt;
  
  &lt;%= proposalCountryServices.Count(c =&gt; c.Country == country.Country) %&gt;
  &lt;%= proposalCountryServices.Count(c =&gt; (c.Country != null &amp;&amp; country.Country != null) &amp;&amp; c.Country.Equals(country.Country)) %&gt;)
  &lt;%= proposalCountryServices.Count(c =&gt; Object.Equals(c.Country, country.Country)) %&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>Here are my results:</p>
  
  <h3>IIS 6:</h3>
  
  <pre><code>100 &lt;-- CountryID
  100 &lt;-- CountryID Hash Code
  100 &lt;-- Country Hash Code
  
  1 &lt;-- Something Found
  1 &lt;-- Something Found
  1 &lt;-- Something Found
  </code></pre>
  
  <h3>IIS 7:</h3>
  
  <pre><code>100 &lt;-- CountryID
  100 &lt;-- CountryID Hash Code
  100 &lt;-- Country Hash Code
  
  0 &lt;-- Nothing Found
  1 &lt;-- Something Found
  1 &lt;-- Something Found
  </code></pre>
  
  <p>Is there a difference between .NET 3.5 SP1 on Windows 2003 vs Windows 2008?  I am really at a loss of what the problem could be.  Has anybody experienced a similar issue?</p>
  
  <h3>Update 1:</h3>
  
  <p>To answer Jon's question.  The two collections are loaded using NHibernate. But I feel I should reiterate that both IIS 6 and IIS 7 are using the <strong>exact same build</strong> of the application, so unless NHibernate or DynamicProxy2 is changing how things are loaded based on Windows 2003 or Windows 2007, which I haven't been able to find anything about on Google, I don't know what to make of it.  </p>
  
  <p>This is also a system wide issue of whenever I am comparing two of my entity objects.  So it could have something to do with the DynamicProxy2 wrapper, but both objects are Country objects and given the overrides I have created everything should work the same in IIS 6 and IIS 7.</p>
  
  <h3>Update 2:</h3>
  
  <p>This appears to be a DynamicProxy2 or NHibernate issue.  Because I tried the following code:</p>
  
  <pre><code>&lt;%
      var c1 = new ICost.Business.Entities.Country {
          CountryID = 100
      };
      var c2 = new ICost.Business.Entities.Country {
          CountryID = 100
      };
  %&gt;
  &lt;%= c1.CountryID == c2.CountryID %&gt;
  &lt;%= c1.GetHashCode() == c2.GetHashCode() %&gt;
  &lt;%= c1.Equals(c2) %&gt;
  &lt;%= Object.Equals(c1, c2) %&gt;
  &lt;%= c1 == c2 %&gt;
  </code></pre>
  
  <p>And for both IIS 6 and IIS 7 the result was, <code>true</code>, <code>true</code>, <code>true</code>, <code>true</code>, <code>false</code>.  See my answer below for what I did to solve this.</p>
  
  <h3>Update 3:</h3>
  
  <p>This also might have had something to do with it:
  <a href="https://stackoverflow.com/questions/258011/problem-with-windsor-castle-with-iis7">Looks like you forgot to register the http module with Windsor Castle with IIS7</a></p>