Click here to Skip to main content
Click here to Skip to main content
Go to top

WCF, Data Contracts, and Enums

, 24 Aug 2011
Rate this:
Please Sign up or sign in to vote.
Do not attribute enums as Data Contracts if you reuse assemblies.

With Windows Communication Foundation, it is easy to create any kind of service. Of course, my opinion could be biased because I am a .NET (mainly C#) developer, but I really find it hard to believe that someone could disagree with such a statement.

Of course, there is a lot of know-how and trouble for beginners, specially trying to understand that WCF integrates all communication models we know (web, TCP, pipes, message queues) under one uniform paradigm.

But there are some moments where you hit a rock in the path; years ago, I stumbled upon the following scenario:

I had a library “common.dll” that I used from both the client and the service. You might think this is not something you would do too often, and many people would say “a client must not know anything about the service internals”. Well… that’s true, but this is not that case. It is just a situation where a client shares some business logic with the service.

And the problem?

Well, there is no problem with that configuration unless you have an enum in common.dll and you tag it with the DataContract attribute. WCF allows you to do so, but I don’t recommend it. Usually when you have a common set of libraries referenced from both the service and the client, you mark “reuse types in referenced assemblies” on Visual Studio, right? Look below:

Now, when you do that, and you have tagged an enum as a data contract, you will get an error like this:

The solution? Well, do not attribute enums as Data Contracts if you reuse assemblies. Here you can download a sample project with full source code so you can test this behavior. In order to check both situations, just add/remove the “DataContract” attribute from the MyEnum enum.

//remove this to make it work
public enum MyEnum { Value1, Value2, Value3 }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Leonardo Paneque
Team Leader
United States United States
Leonardo loves to code with C# and thinks .NET platforms rocks.
He has a Master degree in Computer Sciences and likes to share code and ideas.
Follow on   Twitter

Comments and Discussions

 
BugIt is a bug in your code [modified] PinmemberJakub Januszkiewicz30-Aug-11 6:52 
GeneralRe: It is a bug in your code PinmemberLeonardo Paneque30-Aug-11 10:01 
GeneralRe: It is a bug in your code PinmemberJakub Januszkiewicz30-Aug-11 11:17 
GeneralRe: It is a bug in your code PinmemberLeonardo Paneque30-Aug-11 11:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 24 Aug 2011
Article Copyright 2011 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid