50
votes

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.

Here's the situation. I have 2 lightweight C# destop apps and a WCF webservice 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 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 };  

So .... Is there a way to share an enum among a WCF service and other applictions?

4
It's recommended to avoid enums in webservices because they create subtle backwards compatible problems. See stackoverflow.com/a/788281/52277Michael Freidgeim
Doesn't apply in this case; the backward compatibility issues referred to are due to returning an enum to a client (which very clearly can break them) versus taking an enum as input.Keith Ripley

4 Answers

46
votes

Using the Common library should be fine. Enumerations are serializable and the DataContract attributes are not needed.

See: 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

EDIT: Even so, there should be no issue with having the enum marked as a DataContract and having client libraries using it.

35
votes

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 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]

35
votes

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!

0
votes

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