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.
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:
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();
}
}
I have the following code in an ASPX page both on IIS 6 and IIS 7:
<% foreach(var country in proposalCountries) { %>
<%= country.Country.CountryID %>
<%= country.Country.CountryID.GetHashCode() %>
<%= country.Country.GetHashCode() %>
<%= proposalCountryServices.Count(c => c.Country == country.Country) %>
<%= proposalCountryServices.Count(c => (c.Country != null && country.Country != null) && c.Country.Equals(country.Country)) %>)
<%= proposalCountryServices.Count(c => Object.Equals(c.Country, country.Country)) %>
<% } %>
Here are my results:
IIS 6:
100 <-- CountryID
100 <-- CountryID Hash Code
100 <-- Country Hash Code
1 <-- Something Found
1 <-- Something Found
1 <-- Something Found
IIS 7:
100 <-- CountryID
100 <-- CountryID Hash Code
100 <-- Country Hash Code
0 <-- Nothing Found
1 <-- Something Found
1 <-- Something Found
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?
Update 1:
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 exact same build 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.
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.
Update 2:
This appears to be a DynamicProxy2 or NHibernate issue. Because I tried the following code:
<%
var c1 = new ICost.Business.Entities.Country {
CountryID = 100
};
var c2 = new ICost.Business.Entities.Country {
CountryID = 100
};
%>
<%= c1.CountryID == c2.CountryID %>
<%= c1.GetHashCode() == c2.GetHashCode() %>
<%= c1.Equals(c2) %>
<%= Object.Equals(c1, c2) %>
<%= c1 == c2 %>
And for both IIS 6 and IIS 7 the result was, true
, true
, true
, true
, false
. See my answer below for what I did to solve this.
Update 3:
This also might have had something to do with it: http://stackoverflow.com/questions/258011/problem-with-windsor-castle-with-iis7