设置DataMemberAttribute"EmitDefaultValue";从XSD生成代码时使用svcutil [英] setting DataMemberAttribute "EmitDefaultValue" using svcutil when generating code from XSD

查看:129
本文介绍了设置DataMemberAttribute"EmitDefaultValue";从XSD生成代码时使用svcutil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了XSD,并尝试使用svcutil生成C#代码.在XSD中,我有一个类型为int的element(ParticipantIdentifierType).我用minOccur = 1和maxOccur = 1装饰元素. svcutil生成的代码创建了 以下代码:

I have an XSD defined and I tried to generate C# code with svcutil. In the XSD, I have an element(ParticipantIdentifierType) that is type int. I decorate the element with minOccur=1 and maxOccur=1. The code that was generated by svcutil creates the following code:

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, Order=9)]
    public int ParticipantIdentifierType
    {
      get
      {
        return this.ParticipantIdentifierTypeField;
      }
      set
      {
        this.ParticipantIdentifierTypeField = value;
      }
    }

我的目标是使元素(ParticipantIdentifierType)具有强制性.但是,如果客户端在调用服务时未指定该元素,则WCF DataContract Serializer会将值默认为0,因为它为IsRequired = true.我知道通过手动添加/更改 生成的代码将EmitDefaultValue = false添加到DataMemeberAttribute会阻止序列化程序分配默认值并在客户端引发异常.

My goal is to make the element (ParticipantIdentifierType) manadatory. However, if the client doesn't specify the element when calling the service, WCF DataContract Serializer will default the value to 0 because it's IsRequired=true. I know that by manually adding/changing the generated code to add the EmitDefaultValue=false to the DataMemeberAttribute would prevent the serializer from assigning a default value and raise an exception on the client side.

我的问题是,这是一种在XSD或参数中指定svcutil以强制svcutil添加"EmitDefaultValue = false"的方法.到DataMemberAttribute?似乎svcutil生成的代码不会添加此属性,即使该元素是 标记为基本类型(即int,integer,string)的必填项.

My question is that, is that a way to specify in the XSD or paramter to svcutil to force svcutil to add the "EmitDefaultValue=false" to the DataMemberAttribute? It seems like the code generated by svcutil will not add this attribute even if the element is tagged as mandatory for primitive types (i.e. int, integer, string).

谢谢,
Ming

thanks,
Ming

推荐答案

您好Ming

Hi Ming,

序列化程序将EmitDefaultValue信息添加为这样的注释:

The serializer adds the EmitDefaultValue information as an annotation like this:

     < xs:element name ="i"类型="xs:int">
       < xs:annotation>
         < xs:appinfo>
           < DefaultValue EmitDefaultValue =" false" xmlns =" http://schemas.microsoft.com/2003/10/Serialization/"/>
         </xs:appinfo>
       </xs:annotation>
     </xs:element>

      <xs:element name="i" type="xs:int">
        <xs:annotation>
          <xs:appinfo>
            <DefaultValue EmitDefaultValue="false" xmlns="http://schemas.microsoft.com/2003/10/Serialization/" />
          </xs:appinfo>
        </xs:annotation>
      </xs:element>

如果您还使用DCS导出架构,则应该自动为您完成此操作.

This should be done for you automatically, if you're using DCS to export your schemas as well.

希望有帮助,

Youssef


这篇关于设置DataMemberAttribute"EmitDefaultValue";从XSD生成代码时使用svcutil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆