3

I got xml looking like:

<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item>1</item>
    <item>2</item>
    <item>3</item>
</items>

And a wcf service contract:

[ServiceContract(Namespace = "", Name = "MyService", SessionMode = SessionMode.NotAllowed)]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    void DoWork(IEnumerable<int> items);
}

Service binding is basic http. But when i try to post that xml to wcf method i'm getting error: Unable to deserialize XML message with root name "items" and root namespace ""

How should wcf method look like to properly work with that xml?

Seymour
  • 7,043
  • 12
  • 44
  • 51
Sergio
  • 6,900
  • 5
  • 31
  • 55
  • Try to get the WSDL of your service. Does the contract schema match with your XML? – Myrtle Feb 12 '14 at 13:43
  • @MauriceStam for same reason as in question http://stackoverflow.com/questions/21127021/add-behaviorattribute-to-a-workflowservicehost i'm unable to see wsdl. I just need to make service understand xml format i provided – Sergio Feb 12 '14 at 13:55

2 Answers2

3

Your service contract does not seem to be setup correctly.

I think you need to implement a "wrapper" class, which defines a type structure that matches your XML.

For example:

[XmlRoot("items")]
public class MyItems
{
    [XmlElement("item")]
    public List<int> Items { get; set; }
}

I just put together a quick test app and successfully verified the interface using your sample XML (via a soapUI REST client).

Regards,

Seymour
  • 7,043
  • 12
  • 44
  • 51
0

I think you need to specify a root namespace for default xml deserialization. If this is not an option for you, you might need to change your service interface to accept a stream instead.

Here's more information on the subject: http://www.codeproject.com/Articles/35982/REST-WCF-and-Streams-Getting-Rid-of-those-Names-Sp

To actually answer your question, you could try the following:

<?xml version="1.0" encoding="UTF-8"?>
<items xmlns="http://schemas.datacontract.org/2004/07/" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</items>

Edit: That was not your question. So to actually answer your question:

[OperationContract]
[WebInvoke(BodyStyle =
    WebMessageBodyStyle.Bare, Method = "POST", ResponseFormat = WebMessageFormat.Xml,)] 
    void DoWork(Stream data);

Another alternative could be a custom datacontract (signature: void DoWork(MyCustomDataContract data);) and custom deserialization, example here: How to use Custom Serialization or Deserialization in WCF to force a new instance on every property of a datacontact ?

Community
  • 1
  • 1
Tewr
  • 3,713
  • 1
  • 29
  • 43