Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

svcutil omits the ServiceKnownType attribute from the generated interface proxy

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

like image 274
Andy Avatar asked Nov 15 '22 16:11

Andy


1 Answers

I spent hours today on what, as best I can tell, is the exact same issue. The solution for me was to use the AddGenericResolver method from IDesign's ServiceModelEx library.

NOTE: .NET 4.0 required as it uses DataContractResolver

You can find it on IDesign Downloads page.

All I had to do in my case was add the following line of code:

Client.AddGenericResolver( typeof ( K2Source ) );

I hope this helps someone else out there save a few hours!

You can find more information in the book "Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus" by Juval Lowy

like image 54
Hastarin Avatar answered Dec 15 '22 00:12

Hastarin