XmlSerializer的有指定的模式不工作模式、工作、XmlSerializer

2023-09-03 07:12:35 作者: 少年不再年少g◎°

我们正试图使用​​Web服务(SOAP),并生成使用svcutil.exe的架构的适配器。我们有一个字段

  

recurrenceCount

不应该被提供,除非它有一个值,因此我们已经加入一个属性

  

recurrenceCountSpecified

作为根据 MSDN 。尽管recurrenceCountSpecified是假的,本场recurrenceCount属性仍是在输出XML中指定。

什么是我们做错了什么?

适配器code:

  [系统。codeDom.Compiler.Generated codeAttribute(System.ServiceModel,3.0.0.0)] [System.ServiceModel.ServiceContractAttribute(命名空间= http://sas.elluminate.com/,ConfigurationName =SASDefaultAdapterV2Port)]
公共接口SASDefaultAdapterV2Port
{
     [System.ServiceModel.OperationContractAttribute(Action="http://sas.elluminate.com/setSession",ReplyAction =*)]
     [System.ServiceModel.FaultContractAttribute(typeof运算(sas.elluminate.com.ErrorResponse),行动=htt​​p://sas.elluminate.com/setSession,NAME =ErrorResponse)
     [System.ServiceModel.XmlSerializerFormatAttribute()]
     sessionResponseCollection setSession(setSessionRequest要求);
}
 

修改后的类是:

  [System.Diagnostics.DebuggerStepThroughAttribute()]
[系统。codeDom.Compiler.Generated codeAttribute(System.ServiceModel,3.0.0.0)]
[System.ServiceModel.MessageContractAttribute(WrapperName =setSession,WrapperNamespace =htt​​p://sas.elluminate.com/",IsWrapped =真)
公共部分类setSessionRequest
{

     [System.Xml.Serialization.XmlIgnoreAttribute()]
     公共BOOL recurrenceCountSpecified;

     [System.ServiceModel.MessageBodyMemberAttribute(命名空间=htt​​p://sas.elluminate.com/,令= 19)]
     公众诠释recurrenceCount;

}
 
Solr安装及入门使用教程

解决方案

您正在尝试使用(xxxSpecified属性),如果您使用的是MessageContract不适用的行为。它仅适用于XmlSerializer的。已正确指定的XmlSerializer应该用于该操作。但是,因为你还指定了MessageContracts被使用,XmlSerializer的只能踢在序列化的一个新的水平 - 序列化时,每个消息成员即

We're trying to consume a web service (Soap) and have generated an adapter for the schema using SvcUtil.exe. We have a field

recurrenceCount

which should not be provided unless it has a value and so we have added a property

recurrenceCountSpecified

as according to MSDN . Even though recurrenceCountSpecified is false, the field recurrenceCount property is still specified in the outgoing xml.

What are we doing wrong ?

Adapter code :

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel","3.0.0.0")]  [System.ServiceModel.ServiceContractAttribute(Namespace="http://sas.elluminate.com/", ConfigurationName ="SASDefaultAdapterV2Port")] 
public interface SASDefaultAdapterV2Port 
{
     [System.ServiceModel.OperationContractAttribute(Action="http://sas.elluminate.com/setSession",ReplyAction = "*")]      
     [System.ServiceModel.FaultContractAttribute(typeof(sas.elluminate.com.ErrorResponse), Action = "http://sas.elluminate.com/setSession", Name="ErrorResponse")] 
     [System.ServiceModel.XmlSerializerFormatAttribute()]
     sessionResponseCollection setSession(setSessionRequest request); 
}

The modified class is :

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel","3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="setSession", WrapperNamespace = "http://sas.elluminate.com/",IsWrapped = true)]
public partial class setSessionRequest
{

     [System.Xml.Serialization.XmlIgnoreAttribute()]       
     public bool recurrenceCountSpecified;

     [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://sas.elluminate.com/", Order = 19)]
     public int recurrenceCount;

}

解决方案

The behavior you're attempting to use (xxxSpecified properties) does not apply if you're using a MessageContract. It applies only to the XmlSerializer. You have correctly specified that XmlSerializer should be used for the operation. However, because you have also specified that MessageContracts are to be used, the XmlSerializer only kicks in at the next level of serialization - i.e. when serializing each message member.