tags:

views:

341

answers:

3

Hello there,

I want to expose enum attributes to WCF client application, but I can only see enum values.
Here is the enum:

    public enum TemplateType
{
    [EnumDescription("Property Particulars")]
    [EnumValue("PropertyParticulars")]        
    PropertyParticulars = 1,

    [EnumDescription("Short Format Lists")]
    [EnumValue("ShortFormatLists")]        
    ShortFormatLists,

    [EnumDescription("Client Letters")]
    [EnumValue("ClientLetters")]
    ClientLetters,

    [EnumDescription("Labels")]
    [EnumValue("Labels")]
    Labels
}

Please guide how can I expose Description and Value attributes?

Thank you!

A: 

Please check this article

C#: Enhance Enums using Extension Methods

http://pietschsoft.com/post/2008/07/C-Enhance-Enums-using-Extension-Methods.aspx

Doug D
How is this relevant to WCF?
Thorarin
interesting and great - but it won't travel across a WCF channel.....
marc_s
A: 

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.

Andrew Hare
Thanks for your reply, Is there any work around to expose the attributes?
inutan
+1  A: 

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

Thorarin
+1 exactly - these attributes are .NET specific - those will **not** travel across from server to client over a WCF messaging channel.
marc_s