01 February 2007
Interesting String.GetHashCode() issue between ASP.NET 1.1 and 2.0

Today a fellow co-worker, David Penton, ran into an interesting issue about a background ASP.NET thread using ASP.NET 2.0, instead of ASP.NET 1.1 when the individual website was set to run under 1.1.

First a little background.  The internal staging server is Windows 2003 R2.  In the past Windows 2003 would throw an exception at the web application level when two or more websites were sharing the same application pool, and they were set to different versions of ASP.NET.  The R2 release seems to have resolved this issue, hence our IT Administrator running most sites under the common Default Application Pool.

We have a client that requires an ASP.NET 1.1 build of the website.  So the developer was working within VS2k3.  The background processes in question of the web application is to process the searching algorithm we use for an out-of-the-box CommunityServer install (nicknamed the SearchBarrel).  We have an Enterprise Search addon available that uses Lucene.NET, but for this client they are using the search barrel.  The SearchBarrel breaks up a post into individual words, then issues a ToLower() and then GetHashCode() on the string for each word.  We store this Int32 hash in the database as number matching is faster to index than string matching.

The String.GetHashCode() method is different between .NET 1.1 and .NET 2.0.  So when you are upgrading an application from 1.1 to 2.0, and you are storing the HashCode for strings somewhere, you'll have to generate new HashCodes in .NET 2.0.

The issue the developer ran into was very odd.  The post was using a mix of English and Chinese, so we are dealing with an extended character set as well.

(The word we are trying to hash)
ps3对蓝光技术的采用也是令大家称道的原因之一 

When a single word mixed English and binary characters without spaces (i.e. Chinese Simplified as above), the background SearchBarrel CSJob (the thread) would generate an ASP.NET 2.0 HashCode for the above word! 

(.NET 2.0 HashCode of the word above)
-309760669

(.NET 1.1 HashCode of the word above)
1104497610
 

Yes, the website was set to ASP.NET 1.1.  Yes, the binaries were built under .NET 1.1.  But yet, we were getting an .NET 2.0 hashcode.  The only thing that came to my mind was that it was using the default application pool, which was shared with many other staging websites - mostly ASP.NET 2.0 sites I'm guessing (since most of our clients have moved to ASP.NET 2.0).

It gets even odder.  If the developer was to edit the post, change that one word of mixed English and Chinese characters to insert a space between the two different languages, clearing the search barrel and letting the background thread re-hash the post - it would then use ASP.NET 1.1 to generate the HashCode!

Very very odd.  And we could re-produce it consistently by adding and removing that space and forcing the searchbarrel to rebuild.

The only thing we could guess is that .NET 1.1 choked on the English + Chinese character mix when encoding the hashcode and somehow reverted back to ASP.NET 2.0 to generate the GetHashCode() method.

The fix?  Move the website to its own dedicated Application Pool.

Reader's Comments

Today a fellow co-worker, David Penton , ran into an interesting issue about a background ASP.NET thread

# 01 February 07 3:56 PM

Interesting... I knew the GetHashCode() changed with .NET 2.0, though never seen a .NET 1.1 app use the .NET 2.0 method.  Very odd!

# 01 February 07 3:59 PM

That is indeed very odd...good sleuthing work ;-).

# 01 February 07 5:23 PM

Today a fellow co-worker, David Penton , ran into an interesting issue about a background ASP.NET thread

# 07 February 07 12:16 AM

I have same problem. What is the answer?

I convert the users' password characters to their HashCode in .NET 1.1

But when I upgrade my project to .NET 2.0 the users cannot login.

And I can't produce the original value of the password.

This is very strange. Why the HashCode different between .NET 1.1 and .NET 2.0

# 16 March 07 5:32 AM

Hey Mustafa:

Well, in your case there isn't a direct fix.  Because .NET 1.1 and .NET 2.0 have different HashCode values, you're going to have to migrate your data.

The steps would be something like:

* Using an .NET 1.1 application, get the old password and store them in a table (maybe convert to ClearText in the same table during this temp period?)

* Create another application, but compiled under .NET 2.0.  This application will take your temp clear-text passwords, convert them to HashCodes for .NET 2.0, and store them back into your original Users table.

The bug mentioned above in my post relates to running multiple 1.1 and 2.0 applications in the same Application Pool on W2K3 R2 servers.  Sometimes the HashCode defaults to 1.1, other times it's at 2.0 depending on the "space", standard characters, and in extended character sets.

Hope that helps!

# 21 March 07 5:53 PM
Mustafa said:

I thought, can be found a function that, convert .NET 1.1 HasCode to .NET 2.0 HashCode. But I can't found.

Thank you for your suggestions.

# 21 March 07 7:15 PM
Puja said:

How to get back the original value whose gethashcode value is stored in databse?

# 30 August 08 11:00 AM

@Puja: If you are wanting the original ASP.NET 1.1 values, you will have to use an application compiled to run with .NET Framework 1.1.  An easy way would be to write a quick asp.net webform page, and run it locally on your Win XP IIS.  Just change the IIS settings for the virtual directory (or website) to run in ASP.NET 1.1xxxx.  Once you have your valies, change the application back to ASP.NET 2.0xxxx.

# 30 August 08 11:11 AM
Leave a Comment
Comment Policy: HTML is not allowed. Links and line breaks are converted automatically.
(required) 
(optional)
(required) 

 


  
Enter the anti-spam code you see above (required)

 

Comment Notifications
Subscribe to this post's comments using RSS

If you would like to receive an email when updates are made to this post, please register here