带有枚举的 Wcf DataContract 类导致“'枚举值'-1'对于类型无效"错误 [英] Wcf DataContract class with enum causes "'Enum value '-1' is invalid for type" error

查看:26
本文介绍了带有枚举的 Wcf DataContract 类导致“'枚举值'-1'对于类型无效"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试通过 wcf 传递对象时遇到以下异常:

I'm getting the following exception trying to pass an object through wcf:

尝试序列化参数 http://tempuri.org/:item 时出错.InnerException 消息是Enum 值"-1"对于Models.SubModels.DamageLocations"类型无效,并且无法序列化.如果类型具有 DataContractAttribute 属性,请确保存在必要的枚举值并使用 EnumMemberAttribute 属性进行标记.请参阅 InnerException 了解更多详情.

There was an error while trying to serialize parameter http://tempuri.org/:item. The InnerException message was 'Enum value '-1' is invalid for type 'Models.SubModels.DamageLocations' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.'. Please see InnerException for more details.

定义如下:

[DataContract]
public class Property
{
    [DataMember]
    public PropertyDamage Damage { get; set; }

    public Property()
    {
        this.Damage = new PropertyDamage();
    }
}

PropertyDamage:

[DataContract]
public enum DamageLocations
{
    [EnumMember]
    Unknown=0,
    [EnumMember]
    Front,
    [EnumMember]
    Rear
}

[DataContract]
public class PropertyDamage
{
    [Display(Name="Location of Damage:")]
    [DataMember(IsRequired=true)]
    public DamageLocations DamageLocation { get; set; }
}

edit - 这也会导致同样的错误:

edit - this also causes the same error:

public enum DamageLocations
{
    Unknown=0,
    Front=1,
    Rear=2
}

edit 2- 在 ctor 中为枚举添加默认值并没有改变错误:

edit 2- Adding a default value for the enum in the ctor did not change the error:

    public PropertyDamage()
    {
        this.DamageLocation = DamageLocations.Unknown; //0
    }

试图研究这个问题,我看到人们收到类似的错误Enum value '0' is invalid",解决方案是在枚举中添加一个 0.

Trying to research the problem, I see people getting a similiar error "Enum value '0' is invalid", and the solution was to add a 0 to the enum.

虽然我已经有 0 项,但错误指出 -1 无效.

I already have a 0 item though, and the error states -1 is invalid.

解决这个问题的正确方法是什么?

What is the proper way to resolve this?

edit3 - 看来 -1 来自帖子数据,这是提交页面的帖子数据.

edit3 - It appears the -1's are coming from the post data, this is the post data from submitting the page.

Damage.DamageLocation=-1

推荐答案

这听起来就像您的数据具有 -1 的值.

It sounds simply like your data has the value -1.

枚举只是花哨的整数.您可以为枚举分配任何值(在基础类型的范围内);例如:

Enums are just fancy integers. You can assign any value to an enum (within the range of the underlying type); for example:

enum Foo { Bar = 1 }
...
Foo foo = (Foo)1035; // perfectly fine

这在 c# 中很好,但在大多数序列化库中不行,尤其是那些想要将其编码为名称的库(XmlSerializer、DataContractSerializer 等).

This is fine in c# but not in most serialization libraries, especially those which want to encode it as names (XmlSerializer, DataContractSerializer, etc).

因此:如果 -1 不是枚举的定义值,请不要在数据中使用该值.如果 -1 有意义,则在枚举中定义它.

So: if -1 is not a defined value of your enums, don't use that value in your data. If -1 has a meaning, define it in the enum.

这篇关于带有枚举的 Wcf DataContract 类导致“'枚举值'-1'对于类型无效"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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