WCF 不会序列化所有属性
我正在使用已作为 VS2010 中的服务引用导入的 SOAP 服务。我使用服务提供的请求对象来调用其中一项服务。我遇到的问题是,并非对象的所有属性都被序列化,或者不是通过线路发送。请求对象如下所示:
var serviceRequest = new UpdateRequest{
StoreId = request.StoreId,
Id = request.Id,
Status = (Status)Enum.Parse(typeof(Status), request.myStatus.ToString()),
parameters = request.Parameters,
validFrom = request.ValidFrom.Value,
validFromSpecified = request.ValidFromSpecified
};
以下是通过线路发送的内容。我用wireshark捕获了
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<updateStore xmlns="http://localhost.service.com/">
<StoreRequest>
<StoreId>1234</StoreId>
<validFrom>2011-11-29T00:00:00</validFrom>
<parameters>
<param1>true</param1>
</parameters>
</StoreRequest>
</updateStore>
</s:Body>
</s:Envelope>
其中两个参数,Id和Status还没有发送到服务,我就是不明白为什么。正在设置值,并且 WSDL 生成的属性是公共的,并且与正在序列化的属性具有相同的序列化属性。
任何帮助将不胜感激。
编辑 - - 使用服务参考生成的代码进行了更新
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myservice.com/")]
public partial class StoreUpdateRequest : object,
System.ComponentModel.INotifyPropertyChanged {
private long StoreIdField;
private long IdField;
private bool IdFieldSpecified;
private Status StatusField;
private bool StatusFieldSpecified;
private long storeIdField;
private bool storeIdFieldSpecified;
private System.DateTime validFromField;
private bool validFromFieldSpecified;
private System.DateTime validToField;
private bool validToFieldSpecified;
private technicalParameters parametersField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public long StoreId {
get {
return this.StoreIdField;
}
set {
this.StoreIdField = value;
this.RaisePropertyChanged("StoreId");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public long Id {
get {
return this.IdField;
}
set {
this.IdField = value;
this.RaisePropertyChanged("Id");
}
}
I'm consuming a SOAP service that has been imported as a Service Reference in VS2010. I call one of the services with a request object that is provided by the service. The problem I'm having is that not all properties of the object are serialized, or rather not sent over the wire. The request object looks like this:
var serviceRequest = new UpdateRequest{
StoreId = request.StoreId,
Id = request.Id,
Status = (Status)Enum.Parse(typeof(Status), request.myStatus.ToString()),
parameters = request.Parameters,
validFrom = request.ValidFrom.Value,
validFromSpecified = request.ValidFromSpecified
};
And here is what is sent over the wire. I've captured it with wireshark
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<updateStore xmlns="http://localhost.service.com/">
<StoreRequest>
<StoreId>1234</StoreId>
<validFrom>2011-11-29T00:00:00</validFrom>
<parameters>
<param1>true</param1>
</parameters>
</StoreRequest>
</updateStore>
</s:Body>
</s:Envelope>
Two of the parameters, Id and Status have not been sent to the service, and I just can't figure out why. The values are being set and the WSDL generated properties are public and have the same serialization attributes as the properties that are being serialized.
Any help would be appreciated.
Edit----
Updated with service reference generated code
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myservice.com/")]
public partial class StoreUpdateRequest : object,
System.ComponentModel.INotifyPropertyChanged {
private long StoreIdField;
private long IdField;
private bool IdFieldSpecified;
private Status StatusField;
private bool StatusFieldSpecified;
private long storeIdField;
private bool storeIdFieldSpecified;
private System.DateTime validFromField;
private bool validFromFieldSpecified;
private System.DateTime validToField;
private bool validToFieldSpecified;
private technicalParameters parametersField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public long StoreId {
get {
return this.StoreIdField;
}
set {
this.StoreIdField = value;
this.RaisePropertyChanged("StoreId");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public long Id {
get {
return this.IdField;
}
set {
this.IdField = value;
this.RaisePropertyChanged("Id");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。错误是 SOAP 服务已更新,并且某些属性现在是可选的。
当 SOAP 消息中的字段/属性 XX 被标记为可选时,WCF 会创建相应的额外 XXIsSpecified 属性,该属性必须设置为 true XX 值已设置。否则,WCF 将不会序列化或发送该属性。
有多种方法可以查看属性是否设置为可选。
在生成的 Reference.cs 文件中,每个可选选项都会有一个相应的 IsSpecified 属性,如下所示:
private System.DateTime validFromField;
private bool validFromFieldSpecified;
您可以使用 soapUI 查看和测试 wsdl
The problem is resolved. The error was that the SOAP service had been updated and some properties now where optional.
When a field/property say XX is marked as optional in a SOAP message WCF creates an corresponding extra XXIsSpecified property that must be set to true when the XX value is set. Otherwise WCF will not serialize nor send that property.
There are different ways to see if a property is set as optional.
In the generated Reference.cs file each optional will have a corresponding IsSpecified property, like so:
private System.DateTime validFromField;
private bool validFromFieldSpecified;
You can use soapUI to see and test the wsdl
<xs:element minOccurs="0" name="validFrom" type="xs:dateTime"/>