使用WCF进行数据注释

时间:2016-02-05 13:14:23

标签: c# .net wcf xsd wsdl

我正在为客户端创建WSDl,他们将发送请求,我们将执行response.Request包含电话号码或位置或两者,即至少需要一个.WHat是我可以在此强制执行限制的最佳方式。我听说我们可以使用我在MVC中使用的dataanotations。我在WCF中尝试过,但是当我在WSDL中通过右键单击检查 - >在浏览器中查看时,我看不到像minoccurs = 1或我们在XSD。所以我的问题是我是否需要创建单独的XSD并验证请求或者我是否需要添加dataanotations.Can任何正文告诉我一些简单的步骤我可以遵循。这就是我尝试过的

[DataMember]
        [Required(ErrorMessage = "Name Required")]
        public string Phonenumber
        {
            get
            {
                return this.phonenumber;
            }
            set
            {
                this.phonenumber = value;
            }
        }
        /// <remarks/>
        /// 
        [DataMember]
                [DataMember]
        [Required(ErrorMessage = "Name Required")]
        public string Location
        {
            get
            {
                return this.location;
            }
            set
            {
                this.location = value;
            }
        }

在wsdl我可以看到像

<xs:element name="Phonenumber" type="xs:string" nillable="true" minOccurs="0"/>
<xs:element name="Location" type="xs:string" nillable="true" minOccurs="0"/>

1 个答案:

答案 0 :(得分:0)

WCF不理解[Required]属性(或数据注释命名空间中的其他属性)。如果需要数据成员,可以在属性中设置IsRequired属性:

    [DataMember(IsRequired = true)]
    public string Phonenumber
    {
        get
        {
            return this.phonenumber;
        }
        set
        {
            this.phonenumber = value;
        }
    }
    /// <remarks/>
    /// 
    [DataMember(IsRequired = true)]
    public string Location
    {
        get
        {
            return this.location;
        }
        set
        {
            this.location = value;
        }
    }