验证 WCF 数据协定集合中的元素

发布于 2024-11-09 18:09:33 字数 529 浏览 1 评论 0原文

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

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

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

Appointment DataContract 可能如下所示:

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

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

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

We have a WCF service that uses Microsoft.Practices.EnterpriseLibrary.Validation and receives an object like so (simplified):

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

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

The Appointment DataContract could look like:

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

Now the problem is that the validation of the Name property seems to work, but the Description isn't validated. So you can't pass a request with an empty Name, but you can pass a request with a Name and a list of Appointments with empty Descriptions.

Is it normal that WCF doesn't validate the elements of a collection in a DataContract?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入怼 2024-11-16 18:09:33

好吧,我们通过添加 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));
        }
    }
}

Well, we solved it by adding 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));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文