2014-02-12 118 views
3

我XML看起來像:傳遞的IEnumerable <int>作爲參數傳遞給WCF服務

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

而且一個WCF服務合同:

[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); 
} 

服務綁定是基本的HTTP。 但是當我嘗試張貼XML來WCF方法我收到提示: Unable to deserialize XML message with root name "items" and root namespace ""

應該如何WCF方法模樣正確與XML的工作嗎?

+0

嘗試讓你的服務的WSDL。合同模式是否與您的XML匹配? – Aphelion

+0

@MauriceStam出於同樣的原因在問題http://stackoverflow.com/questions/21127021/add-behaviorattribute-to-a-workflowservicehost我無法看到wsdl。我只需要使服務瞭解我提供的xml格式 – Sergio

回答

3

您的服務合同似乎沒有正確設置。

我認爲你需要實現一個「包裝」類,它定義了一個與你的XML相匹配的類型結構。

例如:

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

我只是把一個快速測試應用程序,併成功驗證使用示例XML接口(通過REST的soapUI客戶端)。

問候,

+0

非常感謝!用「XmlSerializerFormat」屬性標記的方法得到它的工作 – Sergio

0

我認爲你需要指定默認XML反序列化根命名空間。如果這不適合您,您可能需要將服務界面改爲接受流。

下面是關於這個問題的更多信息:http://www.codeproject.com/Articles/35982/REST-WCF-and-Streams-Getting-Rid-of-those-Names-Sp

要真正回答你的問題 ,你可以嘗試以下方法:

<?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> 

編輯:這不是你的問題。因此,要真正回答你的問題:

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

另一種方法可以是自定義datacontract(簽名:void DoWork(MyCustomDataContract data);)和自定義反序列化,例如在這裏: How to use Custom Serialization or Deserialization in WCF to force a new instance on every property of a datacontact ?

+0

不能對XML做任何事情,只是服務 – Sergio

+1

嗯至少我設法提供一個更復雜的選擇:P Seymor的答案顯然是很清潔 – Tewr