数据注释属性未在WCF中触发 [英] Data Annotation attributes are not firing in WCF

查看:75
本文介绍了数据注释属性未在WCF中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用版本v4的 System.ComponentModel.DataAnnotations.dll 验证 WCF 服务请求。 0.30319。我将 VS2010 Target Framework v4.0 一起使用。

I am trying to validate the WCF service request using System.ComponentModel.DataAnnotations.dll of Version v4.0.30319. I am using VS2010 with Target Framework v4.0.

以下是我的示例请求。如果我使用 WcfTestclient 调用服务操作,即使我传递了无效值( null / String.Empty / )表示名称

Below are my sample request(s). If i invoke the service operation using WcfTestclient the annotations are not firing even if i pass the invalid values (null/String.Empty/"") for Name.

Request1

[MessageContract]
public class  AddUserRequest
{
    [MessageBodyMember]
    [Required(ErrorMessage = "Id is required.")]
    public int Id { get; set; }

    [MessageBodyMember]
    [Required(ErrorMessage = "Name is required.")]
    [StringLength(100, MinimumLength = 10, ErrorMessage = "Name length should be between 10 and 100")]
    public string Name { get; set; }
}

Request2

[DataContract]
public class User
{
    [DataMember(IsRequired = true)]
    [Required(ErrorMessage = "Id is required.")]
    public int Id { get; set; }

    [DataMember(IsRequired = true)]
    [Required(ErrorMessage = "Name is required.")]
    [StringLength(100, MinimumLength = 10, ErrorMessage = "Name length should be between 10 and 100")]
    public string Name { get; set; }
}

我在这里错过了什么吗?请提出建议。

Is i am missing some thing over here ?. Please suggest.

推荐答案

来自 msdn


System.ComponentModel.DataAnnotations命名空间提供属性
类,这些类用于定义ASP.NET MVC和ASP.NET
数据控件的元数据。

The System.ComponentModel.DataAnnotations namespace provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls.

WCF不会立即处理该命名空间中的属性。
您必须编写自己的逻辑来实现这一目标。

Attributes from that namespace are not handled out-of the box by WCF. You have to write Your own logic to achieve this.

此处描述了对WCF参数的自定义验证。

Here is an article describing custom validation of parameters for WCF.

幸运的是,其他人已经做到了这一点。因此,这是一个CodePlex项目,该项目结合了WCF和数据注释类。

Fortunately others have already done this so here is a CodePlex project that combines WCF and Data Annotation classes. This is probably what You need.

编辑:

DataMember.IsRequired 指示给定成员必须存在于模型中。并不是说它必须有一个值。
这是您的api版本控制。
例如,在您的Service版本1中,您可能具有这样的模型:

DataMember.IsRequired indicates that given member must be present in the model. Not that it must have a value. This is for your api versioning. For example You could have had in Your Service version 1 a model like this:

[DataContract]
public class User
{
    [DataMember]
    public int Id { get; set; }
}

这将序列化为(简化方式):

This would serialize to (in simplified way):

<User>
<Id>19</Id>
</User>

与您的服务集成的任何客户端都会向您发送xml这样的邮件。

And any client that was integrated with Your service would send You xml like this.

但是随后您将版本2中的模型更改为:

But then You changed Your model in Your version 2 to:

[DataContract]
public class User
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public String Name { get; set; }
}

但是您的客户对新版本一无所知,并向您发送旧的xml。该xml将正确反序列化为您的新模型,但 Name 等于null。

But Your client knows nothing about new version and sends You old xml. That xml will deserialize properly to Your new model but with Name equals null.

将此信息通知您的旧客户更改您将 IsRequired = true 添加到您的 Name 属性。这样,WCF将为旧xml返回错误,并且仅接受以下结构:

To inform Your old clients about this change You would add IsRequired=true to Your Name property. That way WCF will return an error for old xml and will accept only this structure:

<User>
<Id>19</Id>
<Name>Some Name</Name>
</User>

请不要说这不会使这样的内容无效:

Please not that this will not make something like this invalid:

<User>
<Id>0</Id>
<Name />
</User>

您的情况是什么。 Id 不可为空,因此默认值为0。
Name 被序列化为没有值-而不是不在那儿。

Which is what is happening in Your case. Id is not nullable so it has a default value of 0. And Name is serialized as having no value - not as "not being there".

这篇关于数据注释属性未在WCF中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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