Web Service makes duplicate, incompatible types of individual types

I had a tough time coming up with a title for this article. It’s hard to explain, exactly what problem I’m referring to is, and choosing key words to help people find this article who are looking for a solution to their problem will be even more difficult. Let me try to explain the problem:

I’ve got a web service. It has many classes and enumerations in it. Many classes contain members of some of the other classes. When I write an application that consumes the web service, some of those types end up as two seperate types. For example, suppose I have an enumeration like this:


public enum MyEnum
{
one,
two,
three
}

Suppose I have two classes like this:


public class MyFirstClass
{
public MyEnum TypeOfThing;
public string something;
}

public class MySecondClass
{
public MyEnum TypeOfOther;
public bool IsThisPlainEnough;
}

Then, in my consuming application, my proxy web class (which is auto generated by Visual Studio when you add a web reference), I’ll get TWO separate MyEnums, one with the name MyEnum and another with MyEnum1. Then, my two classes might come across like this:


public class MyFirstClass
{
public MyEnum TypeOfThing;
public string something;
}

public class MySecondClass
{
public MyEnum1 TypeOfOther;
public bool IsThisPlainEnough;
}

As you can see, it makes MyFirstClass.TypeOfThing incompatible with MySecondClass.TypeOfOther.

I haven’t researched this enough to determine exactly what’s causing this, but I do know something much more important… a solution!

Here’s what you do:

Add an XmlType attribute to the type that gets duplicated, like this:


[System.Xml.Serialization.XmlType(Namespace="MyNameSpace.MyEnum", TypeName="MyEnum")]
public enum MyEnum
{
one,
two,
three
}

Now, when I add a web reference to my class library, it generates ONLY ONE MyEnum and it makes MyFirstClass.TypeOfThing compatible with MySecondClass.TypeOfOther.

That’s it.