【问题标题】:Validation of elements in a collection of a WCF data contract验证 WCF 数据协定集合中的元素
【发布时间】:2011-05-26 14:28:33
【问题描述】:

我们有一个 WCF 服务,它使用 Microsoft.Practices.EnterpriseLibrary.Validation 并接收这样的对象(简化):

[DataMember]
[NotNullValidator]
public string Name { get; set; }

[DataMember]
public IList<Appointment> Appointments { get; set; }

约会数据合同可能如下所示:

[DataMember]
[NotNullValidator]
public string Description { get; set; }

现在的问题是 Name 属性的验证似乎有效,但 Description 没有得到验证。因此,您不能传递具有空名称的请求,但您可以传递具有名称的请求和具有空描述的约会列表。

WCF 不验证 DataContract 中集合的元素是否正常?

【问题讨论】:

    标签: wcf validation collections datacontract


    【解决方案1】:

    好吧,我们通过添加 SelfValidation 解决了这个问题:

    [HasSelfValidation]
    public class Client
    {
        [DataMember]
        [NotNullValidator]
        public string Name { get; set; }
    
        [DataMember]
        public IList<Appointment> Appointments { get; set; }
    
        [SelfValidation]
        {
            foreach (var appointment in Appointments)
            {
                results.AddAllResults(Validation.Validate(appointment));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2016-11-25
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多