Monday, May 28, 2007

Xml serialization of enums - System.InvalidOperationException

A few days ago I was writing a web service when all of a sudden a test failed on me with:
System.InvalidOperationException: Instance validation error: '3' is not a valid value for SomeEnumInMyApp
which turned out to be quite a google-unfriendly exception. The exception is thrown when trying to serialize an enum with a value that isn't explicitly in the declaration of the enum. A couple of people dealt with this, but I don't really want to manually edit the WSDL, and I don't care right now about breaking a future version of the web service (not likely to happen in my case)
In case nothing of this makes any sense to you, let's see some code:
Given this enum declaration:
public enum Epopo {
 Uno = 1,
 Dos = 2
}
"Epopo?" Yes, Epopo. Just because.
This test passes:
[Test]
public void SerializeEnumToXml1() {
 Epopo p = Epopo.Uno;
 using (MemoryStream ms = new MemoryStream()) {
  XmlSerializer xml = new XmlSerializer(typeof(Epopo));
  xml.Serialize(ms, p); 
 }
}
But this one fails with the mentioned exception:
[Test]
public void SerializeEnumToXml2() {
 Epopo p = Epopo.Uno | Epopo.Dos;
 using (MemoryStream ms = new MemoryStream()) {
  XmlSerializer xml = new XmlSerializer(typeof(Epopo));
  xml.Serialize(ms, p); 
 }
}
Hmm. What if we try with a LosFormatter instead of XmlSerializer?
[Test]
public void SerializeEnumToLosFormatter() {
 Epopo p = Epopo.Uno | Epopo.Dos;
 LosFormatter los = new LosFormatter();
 using (MemoryStream ms = new MemoryStream()) {
  los.Serialize(ms, p); 
 }
}
Test passes. Damn you, XmlSerializer. What about XmlEnum?
public enum XmlEpopo {
 [XmlEnum("Uno")]
 Uno = 1,
 [XmlEnum("Dos")]
 Dos = 2
}

[Test] 
public void SerializeEnumWithXmlEnumToXml() { 
 XmlEpopo p = XmlEpopo.Uno | XmlEpopo.Dos; 
 using (MemoryStream ms = new MemoryStream()) { 
  XmlSerializer xml = new XmlSerializer(typeof(XmlEpopo));
  xml.Serialize(ms, p); 
 }
}
Doesn't work either... Well, after that I despaired a bit and started trying everything: XmlElement, PowerCollections.Set, Iesi.Collections.ListSet, Dictionary<Epopo, object>, but nothing worked. Ultimately, I used a List<Epopo>, like this:
[Test]
public void SerializeListOfEnum() { 
 List<Epopo> l = new List<Epopo>();
 l.Add(Epopo.Uno); 
 l.Add(Epopo.Dos); 
 using (MemoryStream ms = new MemoryStream()) { 
  XmlSerializer xml = new XmlSerializer(typeof(List<Epopo>));
  xml.Serialize(ms, l); 
 } 
}
It sucks, I know, but at least it works. If any knows a better solution, feel free to post a comment!
By the way, it seems that Microsoft is having this same problem as well...
Full code for these tests is here.

Sunday, May 20, 2007

Introduction 1.1

A good way to know a programmer is to see what tools he uses. If I read "NUnit" on a résumé, I can safely assume he/she at least heard of unit testing (that doesn't mean he's doing it right, though). If I see "MbUnit", it's likely that he did use NUnit at first, but needed more or wasn't satisfied (that was my case) So, here are some of the tools/frameworks I use (in no particular order) Another good way is to see what he's reading. I must confess I'm not much of a book reader (technical books are very expensive here) but I read a lot of blogs (part of my blogroll is in the column on the right). I think the best ideas, patterns, practices, like TDD, DDD, Continuous Integration, etc, are out there in the net anyway. I try not to stay with .NET blogs only, I also read some Java, Javascript and even Lisp blogs (although Lisp is a pending subject)

Wednesday, May 16, 2007

Introduction

Hi, my name is Mauricio Scheffer, and this is yet-another-programming-blog. I live in Argentina and I'm currently working as a developer for a fairly popular US website (around 150000 unique visitors per month) using ASP.NET (still 1.1, we don't have time to migrate yet :-( ) Since I'm pretty much the only developer here (my boss knows her .NET and SQL but she's too busy administering the site) I spend most of the day squashing bugs (hence the blog title). But I'm not complaining, I'm getting a new teammate next week and I'm constantly interviewing new people. Plus, there's zero bureaucracy, so I'm free to implement things the way I want, as long as it doesn't take forever. Well, that should be enough for a first post. :-)