Home > View Post

Using base types in WCF

I was recently asked if it's possible to expose a base type (maybe an abstract class or interface) as a parameter in a WCF operation and allow different types to be specified when invoked. Unsurprisingly, the WCF guys thought of this and included some attributes to help.

Let's imagine we have an interface IEntity and two derived types MyEntity1 and MyEntity2. We need to tell WCF about the two derived types if we'd like them added to the Service' metadata and we can do this using the ServiceKnownType attribute:

void Save(IEntity entity);

Sadly, the proxy that's generated when you expose an interface exposes a parameter of type 'object' and not IEntity - which doesn't exactly create the best looking API. However, if we use an actual class then the proxy exhibits the appropriate parameter type. Better still, we can apply the KnownType attribute to the superclass and WCF will spot this wherever that type is exposed as a parameter.

public abstract class BaseEntity

Sadly you can't apply the KnownType attribute to interfaces but you can still use the ServiceKnownType attribute on either the OperationContract method or the ServiceContract interface/class. Just not on the type itself.

Naturally, I wondered if it was possible to create a new type, on the client, that inherited from the proxy generated BaseEntity and send that across the wire. I was particularly curious to see how it would have been serialised if this was indeed possible. Unsurprisingly, in my experiments at least, this threw a nasty CommunicationException: "There was an error while trying to serialize parameter http://tempuri.org/:baseEntity". Hohum.

Tags: WCF

Josh Post By Josh Twist
11:23 AM
18 Mar 2009

» Next Post: Development Foundation Assessment at Devweek
« Previous Post: And now for something completely crazy: Binding without WPF

Comments are closed for this post.

Posted by Jeff @ 24 Mar 2009 4:25 PM
I've tried passing an object of a client-side type derived from a service-side base type and was a little disappointed to see that it didn't work. I asked around a little on a few forums but unfortunately there doesn't seem to be a KnownType concept for the client.

© 2005 - 2022 Josh Twist - All Rights Reserved.