与WCF服务共享枚举 [英] Sharing Enum with WCF Service
问题描述
我有其中我想分享一个C#枚举几个不同的应用程序。我不能完全弄清楚如何一个普通应用程序和WCF服务之间共享一个枚举声明。
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.
这里的情况。我有2个轻量级的C#应用程序destop和WCF web服务,所有需要共享的枚举值。
Here's the situation. I have 2 lightweight C# destop apps and a WCF webservice that all need to share enum values.
客户端1有
Method1( MyEnum e, string sUserId );
客户端2具有
Client 2 has
Method2( MyEnum e, string sUserId );
Webservice的有
Webservice has
ServiceMethod1( MyEnum e, string sUserId, string sSomeData);
我最初虽然是创建一个名为Common.dll封装枚举库,然后只是引用图书馆在所有那些需要枚举的项目。但是,WCF使事情变得困难,因为你需要的标记枚举为它是服务的一个组成部分。像这样的:
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:
[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 };
于是....是否有共享的WCF服务和其他applictions中枚举的方法吗?
So .... Is there a way to share an enum among a WCF service and other applictions?
推荐答案
使用公共图书馆应该罚款。枚举是序列化的,并且不需要DataContract属性。
Using the Common library should be fine. Enumerations are serializable and the DataContract attributes are not needed.
请参阅:
的http:/ /msdn.microsoft.com/en-us/library/ms731923.aspx
枚举类型。枚举,其中包括标志枚举,是
序列化。或者,枚举类型可以打上了
DataContractAttribute属性,在这种情况下,每一个
会员系列化参与必须标有
EnumMemberAttribute属性。
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
编辑:即使这样,也应该具有标记为DataContract枚举并利用它具有客户端库没有问题。
Even so, there should be no issue with having the enum marked as a DataContract and having client libraries using it.
这篇关于与WCF服务共享枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!