Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance validation error: '2' is not a valid value for QueryType. (web service)

I have a web service that I am passing an enum

public enum QueryType {
    Inquiry = 1
    Maintainence = 2
}

When I pass an object that has a Parameter of QueryType on it, I get the error back from the web service saying:

'2' is not a valid value for QueryType

when you can clearly see from the declaration of the enum that it is.

I cannot change the values of the enum because legacy applications use the values, but I would rather not have to insert a "default" value just to push the index of the enum to make it work with my web service. It acts like the web service is using the index of the values rather than the values themselves.

Does anybody have a suggestion of what I can do to make it work, is there something I can change in my WSDL?

like image 243
Anthony Shaw Avatar asked Sep 15 '09 20:09

Anthony Shaw


1 Answers

I'm assuming you are using asmx web services for this answer.

Your guess is right -- the XML Serializer uses the enumeration names in the WSDL and not the value.

If you look at your WSDL it will look something like this:

<s:simpleType name="QueryType">
  <s:restriction base="s:string">
    <s:enumeration value="Inquiry" /> 
    <s:enumeration value="Maintainence" /> 
  </s:restriction>
</s:simpleType>


So, when you invoke the service it is expecting a string that is the name of the enumeration member. When you use a .NET proxy, this conversion is usually handled for you. If a value is passed to the service that cannot be deserialized into the enum value you will get the message that you are seeing.

To get around this, you can ensure you are sending it the expected value or, if that doesn't work for you, you can tell the XML Serializer what values you want to use. You can do this using the XmlEnum attribute:

public enum QueryType 
{
    [XmlEnum("1")]
    Inquiry = 1,
    [XmlEnum("2")]
    Maintainence = 2
}


This will generate the following schema fragment (from the WSDL):

<s:simpleType name="QueryType">
  <s:restriction base="s:string">
    <s:enumeration value="1" /> 
    <s:enumeration value="2" /> 
  </s:restriction>
</s:simpleType>


Then if you are passing the value "2" into the service then it should be deserialized properly but you lose the meaning of the enumeration values.

like image 54
Randy supports Monica Avatar answered Oct 06 '22 03:10

Randy supports Monica