Wcf – svcutil omits the ServiceKnownType attribute from the generated interface proxy

svcutil.exewcf

I encountered a problem when I tried to implement the ServiceKnownType example from MSDN.
In the example they provide the following classes:

[DataContract()]
public class Widget
{
    [DataMember]
    public string Id;
    [DataMember]
    public string Catalog;
}

[DataContract()]
public class Machine : Widget
{
    [DataMember]
    public string Maker;
}

And the following interface:

[ServiceKnownType(typeof(Widget))]
[ServiceKnownType(typeof(Machine))]
[ServiceContract()]
public interface ICatalog
{
    [OperationContract]
    Hashtable GetItems();
}

The problem is that when the proxy class is generated (using “Add Service Reference” / svcutil.exe), the “ServiceKnownType” attributes are omitted from the ICatalog proxy interface, resulting in getting an exception
“The formatter threw an exception while trying to deserialize the message: …
Add the type corresponding to 'Widget' to the list of known types”
To solve this problem I have to manually add the service known attributes to the generated proxy interface, which is a very bad solution, since the code regenerates when I update the reference.
The interesting thing in all this situation, is that if the GetItems operation would return object instead of Hashtable, or getting an object as a parameter , then the problem would be solved, i.e.

[OperationContract]
object GetItems();

or

[OperationContract]
Hashtable GetItems(object obj);

results in presence of the “ServiceKnownType” attribute on ICatalog proxy interface.
Does anyone knows how to solve this problem?

Thanks

Best Solution

Seems to be a related problem.

Did you try returning generic Dictionary of Widgets and putting [KnownType(typeof(Machine))] on Widget class?

Related Question