Tuesday, February 19, 2008

.net, soap, web services and enums....

imagine you have a class like this one


public class BaseClass
{
public BaseClass() {}

protected TheEnum enumcho=TheEnum.First;
public TheEnum Enumcho
{
get { return enumcho; }
}
}


where TheEnum is some enumeration.

now, if you want to use and export this class in a webservice, you would decorate it with [Serializible] and expect everything to work just fine...
so thought i until i discovered that the class generated for the proxy would look like


[System.....]
public partial class BaseClass
{
}


yes, you saw that correctly - its just an empty class, and something like this is enough to drive one crazy...
took me long 2-3 hours to find a solution, and it was hard because as always - the oracle (google) is only giving helpful answers when you're asking the correct questions

.net serialize enum?
c# enum soap?
enum how serialize soap c#?
enum not serialized web service c#?

and a big load of similar ones, to not much help until on one of the searches i came upon this one, where bruce johnson wrote about his problems playing with java/.net web services and especially this one where 'Wim' had problems serializing enum fields. and that probably wouldn't be of much help either had he not mentioned this weird thing: "all fields are serialized, except the enum field...strange thing is, when i rename the member field, it does get serialized" - this was enough to get me started to look for the crazy explanation for the problem (remember?-when all the logic conclusions lead to nothing its time to look for the illogical answers...)

and i finally found it when i added a setter method to the property, like this...


public TheEnum Enumcho
{
get { return enumcho; }
set { int i=0; }
}



to my relief this solved the problem and the property was finally added to the definition of the class in the proxy....breathe...and relax....3 hours waisted...

hope this will be helpful to somebody else too :)