I want to expose enum attributes to WCF client application, but I can only see enum values.
Here is the enum:
public enum TemplateType
{
[EnumDes
There is a workaround if the intention is to expose a display text for enum members, define your enum in this way in the contracts:
public enum EPaymentCycle
{
[EnumMember(Value = "Month by Month")]
Monthly,
[EnumMember(Value = "Week by Week")]
Weekly,
[EnumMember(Value = "Hour by Hour")]
Hours
}
The SvcUtils serialization produces an interesting result:
public enum EPaymentCycle : int
{
[System.Runtime.Serialization.EnumMemberAttribute(Value="Month by Month")]
MonthByMonth= 0,
[System.Runtime.Serialization.EnumMemberAttribute(Value="Week by Week")]
WeekbyWeek= 1,
[System.Runtime.Serialization.EnumMemberAttribute(Value="Hour by Hour")]
HourbyHour = 2
}
You can read the EnumMemberAttribute Value by reflection and there you got it. Also the xsd metadata file produced by svcutil serialization is as expected:
<xs:simpleType name="EPaymentCycle">
<xs:restriction base="xs:string">
<xs:enumeration value="Month by Month" />
<xs:enumeration value="Week by Week" />
<xs:enumeration value="Hour by Hour" />
</xs:restriction>
I'm not fully versed in the specs, but I doubt this kind of metadata has an equivalent representation in WSDL. Thus, this will not be visible on the client side if you generate the types in your proxy.
However, if you put all your DataContracts in a separate assembly that you reference in the client, you can reuse those types on the client side. In that case, the attributes would be visible. "Reuse types in referenced assemblies" needs to be checked for your Service Reference, but this is on by default.
Here's a short blog post about it. I'm sure there are others...
You can expose enums from a service but the attributes on an enum are not serialized when they are sent over the wire. This means that consumers of this enum will only see the enum itself and none of your attributes.
What you need to do is dress up your enum with a DataContract
attribute and the values with the EnumMember attribute so that your information will be serialized, but this will only allow you to specify the underlying value of each enum value, not a description.
Example enum for the values of a traffic light...
[DataContract]
public enum TrafficLightType
{
[EnumMember]
Red,
[EnumMember]
Green,
[EnumMember]
Amber
}