C# 与 WCF 服务共享枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/187505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sharing Enum with WCF Service
提问by Keith G
I have few different applications among which I'd like to share a C# enum. I can't quite figure out how to share an enum declaration between a regular application and a WCF service.
我有几个不同的应用程序,我想在其中共享一个 C# 枚举。我不太清楚如何在常规应用程序和 WCF 服务之间共享枚举声明。
Here's the situation. I have 2 lightweight C# destop apps and a WCF webservice that all need to share enum values.
这是情况。我有 2 个轻量级 C# 停止应用程序和一个 WCF web 服务,它们都需要共享枚举值。
Client 1 has
客户端 1 有
Method1( MyEnum e, string sUserId );
Client 2 has
客户端 2 有
Method2( MyEnum e, string sUserId );
Webservice has
网络服务有
ServiceMethod1( MyEnum e, string sUserId, string sSomeData);
My initial though was to create a library called Common.dll to encapsulate the enum and then just reference that library in all of the projects where the enum is needed. However, WCF makes things difficult because you need to markup the enum for it to be an integral part of the service. Like this:
我最初是创建一个名为 Common.dll 的库来封装枚举,然后在需要枚举的所有项目中引用该库。但是,WCF 使事情变得困难,因为您需要标记枚举以使其成为服务的组成部分。像这样:
[ServiceContract]
[ServiceKnownType(typeof(MyEnum))]
public interface IMyService
{
[OperationContract]
ServiceMethod1( MyEnum e, string sUserId, string sSomeData);
}
[DataContract]
public enum MyEnum{ [EnumMember] red, [EnumMember] green, [EnumMember] blue };
So .... Is there a way to share an enum among a WCF service and other applictions?
所以.... 有没有办法在 WCF 服务和其他应用程序之间共享枚举?
采纳答案by Szymon Rozga
Using the Common library should be fine. Enumerations are serializable and the DataContract attributes are not needed.
使用 Common 库应该没问题。枚举是可序列化的,不需要 DataContract 属性。
See: http://msdn.microsoft.com/en-us/library/ms731923.aspx
请参阅:http: //msdn.microsoft.com/en-us/library/ms731923.aspx
Enumeration types. Enumerations, including flag enumerations, are serializable. Optionally, enumeration types can be marked with the DataContractAttribute attribute, in which case every member that participates in serialization must be marked with the EnumMemberAttribute attribute
枚举类型。枚举,包括标志枚举,是可序列化的。可选地,枚举类型可以用 DataContractAttribute 属性标记,在这种情况下,参与序列化的每个成员都必须用 EnumMemberAttribute 属性标记
EDIT: Even so, there should be no issue with having the enum marked as a DataContract and having client libraries using it.
编辑:即便如此,将枚举标记为 DataContract 并让客户端库使用它应该没有问题。
回答by Joachim Kerschbaumer
you could assign int values to your Enum members and just use int's for transfer and when necessary cast them back into your Enum type
您可以将 int 值分配给您的 Enum 成员,只需使用 int 进行传输,并在必要时将它们转换回您的 Enum 类型
回答by Keith G
I must have had some issues with an outdated service reference or something. I went back and created a common library containing the enum, and everything works fine. I simply added a using reference to the service interface's file.
我一定在过时的服务参考或其他方面遇到了一些问题。我回去创建了一个包含枚举的公共库,一切正常。我只是添加了一个对服务接口文件的 using 引用。
using Common;
[ServiceContract]
[ServiceKnownType(typeof(MyEnum))]
public interface IMyService
{
[OperationContract]
ServiceMethod1( MyEnum e, string sUserId, string sSomeData);
}
and I dropped the following:
我删除了以下内容:
[DataContract]
public enum MyEnum{ [EnumMember] red, [EnumMember] green, [EnumMember] blue };
I guess since the enum is referenced via ServiceKnownType, it didn't need to be marked up in the external library with [DataContract] or [Enumerator]
我猜因为枚举是通过 ServiceKnownType 引用的,所以不需要在外部库中使用 [DataContract] 或 [Enumerator] 进行标记
回答by Pilsator
I had a quite weird problem and thought it might be interesting to you. I also had problems that I got a connection stop when I used enums in my data contract. It took me quite a while to find out what the real problem was: I used enums with int values assigned. They started by 1 instead of 0. Obviously WCF requires that there is an enum value equal to 0 for serialization. If you don't state any values within your enumeration, an automatic int value mapping will be done for you starting by 0, so everything's fine. But when you copy paste some other enum definition where the 0 value is not assigned you won't get that to your client through WCF - strange but true!
我有一个很奇怪的问题,我认为它可能对你很有趣。当我在数据合同中使用枚举时,我也遇到了连接停止的问题。我花了很长时间才发现真正的问题是什么:我使用了带有 int 值的枚举。它们从 1 而不是 0 开始。显然 WCF 要求有一个等于 0 的枚举值用于序列化。如果您没有在枚举中说明任何值,将从 0 开始为您完成自动 int 值映射,所以一切都很好。但是,当您复制粘贴其他未分配 0 值的枚举定义时,您将无法通过 WCF 将其发送给您的客户端 - 奇怪但真实!