0

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?

JustAMartin
  • 13,165
  • 18
  • 99
  • 183
  • 1
    Does it do the same thing if you remove the `protected` modifier from the `set`? – Tim S. Apr 12 '13 at 16:20
  • @Tim: oh you got it. Somehow I assumed that if NHibernate is able to set the protected Version field, then also WCF will be able to do it. Obviously, I was wrong. – JustAMartin Apr 12 '13 at 16:33
  • Cool, I'll make an answer out of the comment. – Tim S. Apr 12 '13 at 16:35
  • @JustAMartin how do you manage solve your problem ? Look here please" http://stackoverflow.com/questions/42294978/lack-of-sent-field-from-sublcass-in-wcf-trying-to-gain-polymorphism?noredirect=1#comment71750175_42294978 I have the very similar problem. Maybe you can your xml, please? –  Feb 17 '17 at 13:44
  • @llvmquestioner As Tim suggested, I just removed `protected` from the `set` and then the WCF serializer picked it up fine. – JustAMartin Feb 17 '17 at 14:33
  • @JustAMartin tell me if you send it by XML ? if yes, can you show it ? –  Feb 17 '17 at 14:53

1 Answers1

2

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; }
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • Thanks. It would be great if WCF serialiser threw some exception and not just silently ignore the protected setter... oh, well, it's Microsoft. – JustAMartin Apr 12 '13 at 16:42
  • @Tim S. Maybe can you help here ? http://stackoverflow.com/questions/42294978/lack-of-sent-field-from-sublcass-in-wcf-trying-to-gain-polymorphism?noredirect=1#comment71750175_42294978 –  Feb 17 '17 at 13:46