JustAMartin
JustAMartin

Reputation: 13733

WCF loses inherited property values

I have a WCF service method:

public EntityBase GetEntityById(string entityName, object entityId)

I have two base classes:

public abstract class EntityBase
{
    public virtual object Id { get; set; }
}

public abstract class VersionableEntityBase : EntityBase
{
    public virtual int Version { get; protected set; }
}

All of my entities which inherit from EntityBase are added as KnownTypes to the service at startup, also including VersionableEntityBase entity.

Now when I create an object

public class MyEntity : EntityBase
{
}

and call the service with GetEntityById, the inherited Id is received just fine in the client.

But if I create the following:

public class MyVersionableEntity : VersionableEntityBase 
{
}

and return the same entity from the GetEntityById() method, my Version field becomes empty when received in client. Somehow WCF does not see that MyVersionableEntity inherits from the intermediate VersionableEntityBase, so it skips the Version field.

Unfortunately, I cannot change the GetEntityById method to return VersionableEntityBase because not every entity will need the Versioning capabilities.

How do I tell WCF serialiser that the entity returned from GetEntityById method is also of type VersionableEntityBase and not just EntityBase?

Upvotes: 0

Views: 580

Answers (1)

Tim S.
Tim S.

Reputation: 56536

Remove the protected modifier from Version.set. The WCF serializer isn't able to access it.

public abstract class VersionableEntityBase : EntityBase
{
    public virtual int Version { get; set; }
}

Upvotes: 2

Related Questions