WCF 中的数据契约问题
假设我有一个方法并且返回类型是枚举,我的问题是我是否应该将枚举声明为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您不需要将 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.唯一需要将
[EnumMember
] 等添加到枚举的情况是:值
< /a>)。MSDN 有一个后者的例子。
The only time you need to add
[EnumMember
] etc to the enum is if:Value
).MSDN has an example of the latter.
如果您想使用适当的 xsd 命名空间发布 wsdl,则只能使用枚举的 DataContract 属性来执行此操作。
例如
,否则在 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.
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.
我以前不需要回答这个问题,但似乎您可以通过创建一个简单的小测试项目并尝试两种方式来自己找到答案。 选择能够为您提供所需结果的方法。 (你的帖子并没有真正表明目标,所以无论如何都很难回答这个问题。)
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.)
当您在对 Pwninstein 答案的评论中索取文件时,我将其发布在这里。
http://msdn.microsoft.com/en-us/library/aa347875.aspx
它说(在简单枚举部分):
希望有帮助: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):
Hope it helps :D