WCF – Serialization – DataContract runtime error – Type ‘myType’ cannot be serialized

06 Nov

My scenario:

(View it on stackoverflow)

Trying to pass “complex” class type that contains primitive types with interfaces and lists of interfaces/classes.

I guess the problematic member is:

public List<IMyInterface> IntrfList

Runtime Error:

An error occurred while receiving the HTTP response to http:/localhost/xxxxxx/xxxxxService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

The descriptive error from MS Service Trace Viewer (SvcTraceViewer):

Type ‘myType’ cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

[BTW: SvcTraceViewer is very usefull tool for WCF, learn more here]

I did as the explanation suggests but didn’t help.

The “shared” class:

    public class ServerState
        private Queue<IJob> _mWaitingQueue;
        public Queue<IJob> mWaitingQueue
                lock (_LockObjWaiting)
                    return _mWaitingQueue;
            private set
                _mWaitingQueue = value;
        private object _LockObjWaiting = new object();

        private List<IJob> _mInPrograssList = new List<IJob>();

        public List<IJob> mInPrograssList
                    return _mInPrograssList;
            private set
                _mInPrograssList = value;

IJob is an interface. The structure of IJob is like this:

IJob (Interface)
  - JobBase (Abstract class)
    - JobA (Driven from JobBase)
    - JobB (Driven from JobBase)


After fighting and cutting all possible parts of that “shared” class, I got to the problematic area.
The problem was that I used interface as a member (or list of..), which associated with Driven classes.
And that’s it! The serializer having problem figuring out how to serialize that member, so here is the solution:

When using interface as a member (or list of interface) in a class that is about to be serialized and shared via WCF,
the “Shared” class must specify the “possible” types of the interface by adding to it:


And each of those types must be marked with:


[Note: Since JobBase is abstract, no reason to mark KnownType of it…]

That’s it.



Posted by on November 6, 2012 in Uncategorized


3 responses to “WCF – Serialization – DataContract runtime error – Type ‘myType’ cannot be serialized

  1. Anonymous

    April 27, 2016 at 7:07 pm

    Not working

    • Anonymous

      April 27, 2016 at 7:07 pm

      Correction, it is working, but only for abstract classes, not for interfaces.

      • livshitz

        April 27, 2016 at 7:12 pm

        If you had the same problem as I did, the WCF serializer does not know how to resolve the interface, that’s why you need mark the interface (or base class) with “KnownType” to leave it a clue about the possible types.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: