WCF 中的数据契约问题

发布于 2024-07-23 10:11:56 字数 225 浏览 6 评论 0原文

假设我有一个方法并且返回类型是枚举,我的问题是我是否应该将枚举声明为 DataContract?

例如,在示例中,OrderStatus 是枚举数据类型,

OrderStatus Poll(string queryID);

我是否应该将 OrderStatus 枚举类型声明为 DataContract?

提前致谢, 乔治

Suppose I have a method and the return type is enum, my question is should I declare the enum as DataContract or not?

Samples like, in the sample, OrderStatus is an enum data type,

OrderStatus Poll(string queryID);

Should I declare OrderStatus enum type as DataContract?

thanks in advance,
George

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

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

发布评论

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

评论(5

Oo萌小芽oO 2024-07-30 10:11:56

我认为您不需要将 OrderStatus 声明为 DataContract。 根据我的经验,如果枚举被作为契约一部分的另一个类使用,那么它们会自动契约化。

此外,还有一个 [EnumMember] 属性,让您可以选择希望通过合约公开哪些枚举值(如果您出于某种原因不想公开所有枚举值)。

I don't think you need to declare OrderStatus as a DataContract. In my experience, Enums are automatically Contract'ified if they're used by another class that is part of a contract.

Also, there exists an [EnumMember] attribute that lets you choose which enum values you want exposed via your contract, should you not want to expose all of them for some reason.

静谧幽蓝 2024-07-30 10:11:56

唯一需要将 [EnumMember] 等添加到枚举的情况是:

  • 您不希望它们全部暴露(请参阅 Pwninstein 的答案),
  • 出于兼容性原因想要更改线路上的文本(设置< /a>)。

MSDN 有一个后者的例子。

The only time you need to add [EnumMember] etc to the enum is if:

  • you don't want them all exposed (see Pwninstein's answer)
  • you want to change the text on the wire for compatibility reasons (set Value).

MSDN has an example of the latter.

童话 2024-07-30 10:11:56

如果您想使用适当的 xsd 命名空间发布 wsdl,则只能使用枚举的 DataContract 属性来执行此操作。

例如

[Datacontract(Namespace="http://company/xsd/service/2009/07/03"]
public enum Status
{
[EnumMember]
ERROR = 1,
[EnumMember]
GOOD = 2,
}

,否则在 wsdl 中使用默认名称空间。 对于企业 Web 服务,您可能希望对您的 wsdl 模式进行适当的命名空间和版本控制。 另外,使用 EnumMember 属性,您可以选择不发布上述所有枚举值。

If you care to publish your wsdl with proper xsd namespaces you can only do this using the DataContract attribute for the enum.

e.g.

[Datacontract(Namespace="http://company/xsd/service/2009/07/03"]
public enum Status
{
[EnumMember]
ERROR = 1,
[EnumMember]
GOOD = 2,
}

Otherwise the default namespace is used in wsdl. For an enterprise webservice you may wish to have proper namespace and version control your wsdl schemas. Also using EnumMember attribute you can opt out from publishing all your enum values as mentioned above.

紫﹏色ふ单纯 2024-07-30 10:11:56

我以前不需要回答这个问题,但似乎您可以通过创建一个简单的小测试项目并尝试两种方式来自己找到答案。 选择能够为您提供所需结果的方法。 (你的帖子并没有真正表明目标,所以无论如何都很难回答这个问题。)

I haven't needed to answer this before, but it seems like you could find the answer yourself by creating a simple little test project and trying it both ways. Pick the method that gives you the results you need. (Your post doesn't really indicate the goal, so it's hard to answer the question anyway.)

错爱 2024-07-30 10:11:56

当您在对 Pwninstein 答案的评论中索取文件时,我将其发布在这里。

http://msdn.microsoft.com/en-us/library/aa347875.aspx

它说(在简单枚举部分):

您还可以序列化尚未应用 DataContractAttribute 属性的枚举类型。 此类枚举类型的处理方式与前面描述的完全一样,只不过每个成员(未应用 NonSerializedAttribute 属性的成员)都被视为已应用 EnumMemberAttribute 属性。

希望有帮助:D

As you ask for a document in your comment to Pwninstein answer, I'm postig it in here.

http://msdn.microsoft.com/en-us/library/aa347875.aspx

It says (in the Simple Enumerations section):

You can also serialize enumeration types to which the DataContractAttribute attribute has not been applied. Such enumeration types are treated exactly as previously described, except that every member (that does not have the NonSerializedAttribute attribute applied) is treated as if the EnumMemberAttribute attribute has been applied.

Hope it helps :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文