.NET Framework Bookmark and Share   
 index > Windows Communication Foundation > MessageContract generates unexpected field in XSD.
 

MessageContract generates unexpected field in XSD.

Hi All,

I've created a WCF service which expects SOAP messages which are generated by the MessageContracts. However this seems to generate code in the XSD that I do not understand why it's there.

I created a MessageContract with the following element in it:

        [System.Xml.Serialization.XmlElementAttribute("OrderNr", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Status", typeof(StatusType))]
public object Item { get; set; }

I expect this to generate the following element in the XSD:

<xs:choice minOccurs="1" maxOccurs="1">
<xs:element minOccurs="0" maxOccurs="1" name="OrderNr" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="Status" type="StatusType"/>
</xs:choice>


However it creates the following:


<xs:choice minOccurs="1" maxOccurs="1">
<xs:element minOccurs="0" maxOccurs="1" name="OrderNr" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="Status" type="StatusType"/>
<xs:element minOccurs="0" maxOccurs="1" name="Item"/>
</xs:choice>


This is not that big of a problem when I send SOAP messages to this webservice, however when I create a webservice client through VS I get the message:

InvalidOperationException was unhandled: There was an error reflecting 'Item'.
Deeper in this expection is the following message: "You need to add XmlChoiceIdentifierAttribute to the 'Item' member."

All though it is not possible to add this annotation to this object, because the element has a wrapper element. The XmlChoiceIdentifier is not allowed when it is wrapped.

The problem seems to be caused by the extra element in the XSD. This has no type and can not be serialized by the generated client. This way we can not call the webservice trough C#.

Yours sincerely,
Mike Jesmiatka
  • Edited byMike Jesmiatka Tuesday, September 15, 2009 8:02 AMMarkup war annoying
  • Edited byMike Jesmiatka Tuesday, September 15, 2009 8:03 AMmarkup annoying
  •  
Mike Jesmiatka

Hi,

Try add : [System.Xml.Serialization.XmlChoiceIdentifierAttribute] on the item you try to serialize.

Otherwise look at this thread:
http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/1f9ddf41-97f4-41d0-bebd-90c6e99dcf21

Thanks
Binze


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bin-ze Zhao
Hi,

Let me quote myself:

InvalidOperationException was unhandled: There was an error reflecting 'Item'. Deeper in this expection is the following message: "You need to add XmlChoiceIdentifierAttribute to the 'Item' member."

All though it is not possible to add this annotation to this object, because the element has a wrapper element. The XmlChoiceIdentifier is not allowed when it is wrapped.

Let me show you the whole class that I've wrote to clarify the problem.

[MessageContract(WrapperName="VerwerkOrderResponse", WrapperNamespace = "")]
[Serializable]
[XmlRoot(Namespace = "", IsNullable = false)]
public class VerwerkOrderResponseContract
{
[System.Xml.Serialization.XmlElementAttribute("OrderNr", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Status", typeof(StatusType))]
public object Item { get; set; }
}
Mike Jesmiatka
Hi,

I tried your messageContract code, In wsdl file I couldn't see any xsd node. After I tried:
[MessageContract(WrapperName = "VerwerkOrderResponse", WrapperNamespace = "")]
public class VerwerkOrderResponseContract
{
[System.Xml.Serialization.XmlElementAttribute("OrderNr", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Status", typeof(string))]
[MessageHeader]
public object Item { get; set; }
}

Then I get the xsd node contain:
<xs:element name="Item" nillable="true" type="xs:anyType" />

I amnot sure why you want to make your message contract like this, can you explain this here so we can have a clear sense of it.

Normally MassagContract should have messageHeader or messageBody defined and manipulating the data deined within them between service and client.

Thanks
Binze
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bin-ze Zhao
Even I tried, the WSDL doesn't show the XSD that Mike has given above.
Mike- where did you get that XSD from (using svcutil tool?). I think I am missing something. I created a contract using your code above and the WSDL- XSD0, 1 and 2 don't have that XSD. What it has is anytype for- VerwerkOrderResponse.

-Phani
Phani_tpk
Hi,

I did have the [MessageBodyMember] in my class as well. I copied the wrong version of the file from subversion.

I've tested the input that you have supplied and it indeed creates an xs:anyType when the [ServiceContract] does not have the [XmlSerializerFormat]. However our [ServiceContract] also has the [XmlSerializerFormat], which will be resulting in the example I've provided in my first post. My apologies for not providing this in my first post.

If you change your example from:

[System.Xml.Serialization.XmlElementAttribute("OrderNr", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Status", typeof(string))]
[MessageHeader]
public object Item { get; set; }

To:

[System.Xml.Serialization.XmlElementAttribute("OrderNr", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Status", typeof(Guid))]
[MessageBodyMember]
public object Item { get; set; }

and supply [XmlSerializerFormat] to your [ServiceContract], then you've reproduces the problem we have. Note that in your example the use of the return type object is unnecessary, because it always is a string type. In our case we return either a string or a StatusType.

We are trying to reproduce the the <xs:choice> element in our XSD, because our customer wants to receive an OrderNr or a Status returned depending on the result of the operation.

Yours sincerely,
Mike Jesmiatka



Mike Jesmiatka

You can use google to search for other answers

Custom Search

More Threads

• ClientBase<T> Magic
• Generated proxy classes of custom object
• Remove the 'Recent Items List" from the application menu of the Ribbon Control
• Object[] parameter become null
• Why can't I get data back from my WCF service hosted in Sharepoint
• WCF and Validation
• WCF support for Windows Authentication
• duplex contract question
• WCF + SSL Procedure
• Help in session and callback event behaviour ???