在WCF数据服务中使用枚举 [英] Using enums in WCF Data Services

查看:139
本文介绍了在WCF数据服务中使用枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用POCO数据模型手动构建WCF数据服务,我无法弄清楚如何正确地公开枚举值。假设一个简单的模型,如:

  public class Order 
{
public int ID {get; set;}
public string说明{get; set;}
public OrderStatus Status {get; set;}
}

public enum OrderStatus
{
New,
InProcess,
完成
}

如何在 OrderStatus 属性通过OData WCF数据服务?



如果不执行任何操作,数据服务将生成运行时错误(枚举是无效的属性)。我看到的唯一答案是至少解决错误是将枚举属性标记为忽略,例如:

  [System.Data.Services.IgnoreProperties(Status)] 
public class Order ...

这有效,但它迫使你从服务层省略有价值的信息。
在WCF数据服务中还有其他可用于使用枚举值的选项?



编辑:请注意,这是 WCF数据服务(又名Astoria)。这不是原始的WCF服务,在这种情况下答案更清楚。

解决方案

目前WCF数据服务不支持枚举(OData协议也不支持它们)。
典型的解决方法是使用字符串和常量值,或整数和常量值。


I'm trying to manually build a WCF Data Service using a POCO data model and I cannot figure out how to properly expose enum values. Assuming a simple model like:

public class Order
{
   public int ID {get; set;}
   public string Description {get; set;}
   public OrderStatus Status {get; set;}
}

public enum OrderStatus
{
   New,
   InProcess,
   Complete
}

How do you expose the valuable information in the OrderStatus property via the OData WCF Data Service?

If you do nothing, the Data Service generates a runtime error (enum is invalid property). The only answer I've seen that at least resolves the error is to mark the enum property as Ignored, such as:

[System.Data.Services.IgnoreProperties("Status")]
public class Order ...

This works, but it forces you to "omit" valuable information from the service layer. Are there other options for working with enum values in WCF Data Services?

EDIT: Please notice this is WCF Data Services (aka Astoria). This is not raw WCF Services, in which case the answers are more clear.

解决方案

Enums are currently not supported in WCF Data Services (the OData protocol doesn't support them either). The typical workaround is to use string and constant values, or integer and constant values.

这篇关于在WCF数据服务中使用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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