Deperplexing WCF errors pt. 3 - Interfaces and KnownTypes

Tags: .NET, WCF, WinFX, WinFX January CTP

Another error message that stumped me today (after I had removed the IsOneWay parameter and actually got to see it) was the following exception:

System.ServiceModel.CommunicationException
The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error

This little gem was caused when calling a method on a service with an interface as a parameter, something like this:

[OperationContract]
void DoSomething (IMessage message)


When deserializing the IMessage, WCF had no way of knowing what type to deserialize it as, so it through the charming message above.
I don't know how this situation came to be, since the proxy generated by svcutil seems to create the message as DoSomething(object message) and not IMessage, but the principle should be the same.

The immediate solution that fixed this was adding the [KnownType(typeof(myMessage))] attribute to the method. This allows the deserialization engine to understand the message and do something constructive with it rather than crashing.

Naturally, I am less than pleased with this solution. The whole purpose of using interfaces is that I won't have to know, when coding, what objects will be passed to my service. I just want to expose the interface.

One way to keep this flexibility can be found in the very last paragraph of the long and detailed Data Contract Known Types article on MSDN - it seems that the list of Known Types can be defined globally in the system using the section in the config file. Details about that are also sparse, and I'm not at all sure if it's possible in the January CTP - the section names seem to have changed from to between January and February, and there are no samples to explain the proper structure for the January version. I hope this gets clearer with the next beta.



3 Comments

Comments have been disabled for this content.