2012-08-01 80 views
3

這裏是我的服務合同:SOAP消息反序列化問題 - 領域都有空值

[ServiceContract(Namespace = Service.Namespace)] 
[XmlSerializerFormat(Style=OperationFormatStyle.Document, Use=OperationFormatUse.Literal)] 
public interface IService 
{ 
    [OperationContract] 
    UpdateResponse Update(UpdateRequest request); 
} 

實施:

[ServiceBehavior(Namespace = Namespace)] 
public class Service : IService 
{ 
    public const string Namespace = "http://schemas.localhost.net/test"; 

    public UpdateResponse Update(UpdateRequest request) 
    { 
     return new UpdateResponse() { Succeeded = true }; 
    } 
} 

消息合約:

[MessageContract(WrapperNamespace = Service.Namespace)] 
public class UpdateRequest 
{ 
    [MessageBodyMember] 
    public int Id { get; set; } 
    [MessageBodyMember] 
    public string Name { get; set; } 
} 

[MessageContract(WrapperNamespace = Service.Namespace)] 
public class UpdateResponse 
{ 
    [MessageBodyMember] 
    public bool Succeeded { get; set; } 
} 

網絡。配置文件(其中的一部分):

<services> 
    <service behaviorConfiguration="returnFaults" name="ServiceTest.Service"> 
    <endpoint binding="basicHttpBinding" contract="ServiceTest.IService" 
       bindingNamespace="http://schemas.localhost.net/test" /> 
    </service> 
</services> 

這裏是從提琴手發送的SOAP消息(請求):

POST http://localhost:10000/Service.svc HTTP/1.1 
SOAPAction: "http://schemas.localhost.net/test/IService/Update" 
Content-Type: text/xml; charset="utf-8" 
Host: localhost:10000 
Content-Length: 532 

<?xml version="1.0"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> 
<SOAP-ENV:Body xmlns:NS1="http://schemas.localhost.net/test">  
    <NS1:UpdateRequest id="1"> 
     <Name xsi:type="xsd:string">Abcdefg</Name><Id xsi:type="xsd:int">1</Id> 
    </NS1:UpdateRequest> 
    <parameters href="#1"/> 
</SOAP-ENV:Body> 

更新()操作接收UpdateRequest對象,但的字段沒有被設置(名稱爲空, Id爲零)。儘管如此,迴應是好的。

回答

3

Name和Id元素沒有名稱空間。因此,無論是肥皂信封不正確還是MessageContract都不完整。該名/ ID應該是這樣的:

<NS1:Name xsi:type="xsd:string">Abcdefg</NS1:Name><NS1:Id xsi:type="xsd:int">1</NS1:Id> 
+1

不太確定。要繼承,您需要xmlns =「namespace」。在這裏它被定義爲NS1,不是默認的,因此不會被繼承。 – Tisho 2012-08-01 07:11:55

+1

我站在更正,這確實只適用於默認名稱空間。 – CodeCaster 2012-08-01 07:24:01

1

我用的是XmlSerializer的 :) 但我沒有裝飾用下面的消息的成員屬性

[XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified), MessageBodyMember] 

的反序列化現在起作用。 PS:我不能做什麼Tisho說,因爲我不控制Web服務的客戶端。