Sharing Enum with WCF

I have several different applications, among which I would like to share a C # listing. I cannot figure out how to share the enum declaration between a regular application and a WCF service.

Here is the situation. I have 2 lightweight C # removal apps and a WCF web service that all need to share enum values.

Client 1 has

Method1( MyEnum e, string sUserId ); 

Client 2 has

 Method2( MyEnum e, string sUserId ); 

Webservice has

 ServiceMethod1( MyEnum e, string sUserId, string sSomeData); 

My first was to create the Common.dll library to encapsulate an enumeration, and then just link to this library in all projects where enumeration is required. However, WCF complicates the job because you need to mark up the listing so that it becomes 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 }; 

So ... Is there a way to share the enumeration between the WCF service and other applications?

+47
enums c # wcf
Oct 09 '08 at 14:25
source share
4 answers

Using a shared library should be ok. Enumerations are serializable and DataContract attributes are not needed.

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

Types of transfers. Enumerations, including flag enumerations, serialization. Optionally, enumeration types can be marked with a DataContractAttribute, in which case each member that participates in serialization must be marked with the EnumMemberAttribute attribute

EDIT: Despite this, there should be no problem with having an enumeration marked as DataContract and using its client libraries.

+45
09 Oct '08 at 15:34
source share

I must have had problems with an outdated service link or something like that. I came back and created a shared library containing an enumeration, and everything works fine. I just added a link to use the service interface file.

 using Common; [ServiceContract] [ServiceKnownType(typeof(MyEnum))] public interface IMyService { [OperationContract] ServiceMethod1( MyEnum e, string sUserId, string sSomeData); } 

and I omitted the following:

 [DataContract] public enum MyEnum{ [EnumMember] red, [EnumMember] green, [EnumMember] blue }; 

I assume that since the enumeration is referenced through ServiceKnownType, it did not need to be marked in the external library using [DataContract] or [Enumerator]

+33
Oct 09 '08 at 20:16
source share

I had a rather strange problem and thought it might be interesting for you. I also had problems with having a connection stop when I used enums in the contract with my data. It took me quite a while to figure out what the real problem is: I used enumerations with assigned int values. They start with 1 instead of 0. Obviously, WCF requires that the serialization has an enumeration value of 0. If you do not specify any values ​​in your enumeration, it will automatically match the value values ​​for you, starting from 0, so that all perfectly. But when you copy paste some other enumeration definition in which the value 0 is not assigned, you will not get it for your client through WCF - strange but true!

+32
Jul 30 '10 at 12:21
source share

you can assign int values ​​to your Enum members and just use int to pass in and if necessary return them back to Enum type

0
Oct 09 '08 at 15:37
source share



All Articles