Saturday, November 15, 2014

Saturday UTF8 Reading

Reading about UTF8 on a Saturday afternoon?  Yeah I didn't expect that either. But I was having trouble yesterday at work sending html email. I was getting some mojibake. That's when characters are encoded one way and interpreted in another way. It looks like some of the characters turn into garbage.  For example in Microsoft outlook you get a black diamond with a question mark for some special characters.

I'm learning that UTF8 is really a dominant standard mostly because it can be used to encode all Unicode characters as well as support good old ASCII. ASCII is UTF8. UTF8 can do these tricks because it can encode characters with a variable number of bytes. For common and ASCII characters it uses just one.  For special characters it might use two or more bytes. 

Also UTF-8 is being used more and more for all Internet related documents and email clients all support HTML encoded in UTF-8. Read more about it at Wikipedia.  

This is a good read too from email on acid.

I learned that most email clients IGNORE the content type in the HTML but instead use the content type from the email header.  That was certainly tricking me up yesterday. 

The problem was with File.ReadAllText.  The solution was to use the correct encoding.

File.ReadAllText(tempFilePath, System.Text.Encoding.GetEncoding(1252))

See also this Stack Overflow question.

Friday, November 7, 2014

Nhibernate LINQ query with null Where condition

So today I was trying to profile a query and for the life of me I could not see the query in the profiler.  Simple queries where working fine:

Session.Get<User>(123); // I could see the SQL in the profiler.

Session.Query<User>().Where(u => u.thing == someVar) // I was not seeing this in the profiler.

Come to find out, "someVar" was null.  OMG.  Wow.  It's kind of like trying to run this SQL

select u.*
from user
where 1=0

There's pretty much no point in running that query and I guess LINQ to NHibernate thought the same thing!

This happened because someVar was coming from config.  The config file for my unit test was missing the variable, so it was null.  Fun times.

OK, so I fixed that problem and it turned out not to be it.  My queries from the unit test kept returning empty IEnumerables.  So I come to find out that the Fluent Nhibernate Models I am using are special models that are not in the same namespace as the majority of our models.  I had to add the models to the NHibernate Config code and NOW the LINQ queries are working.