Search code examples
c#.netwcfwcf-callbacks

WCF callback won't pass derived class


CLIENT SIDE

my client got the following callback contract:

[ServiceContract]
interface IEvent
{
    [OperationContract(IsOneWay = true)]
    void OnEvent(string serverName, string changeType, List<myObject> myObjects);

}

and got a class implementing IEvent as following:

class myClass : IEvent{
 public void OnEvent(string serverName, string changeType, List<myObject> myObjects)
    {
        MessageBox.Show(@"Event Called: " + changeType + @" occured at: " + serverName     + @" got " + myObjects.Count + " myObjects!");
    }
}

SERVER SIDE

my server got the following publishing code:

methodInfo.Invoke(client, new object[] {serverName, changeType, myObjects});

The problem

When I use methodInfo.Invoke with myObjects = new List<myobject>(){} all works fine meaning myClass.OnEvent is called when server uses methodinfo.invoke and mbox is displayed

BUT

when I try to send myObjects = new List<myobject>(){new MyDerivedObject()} it doesn't work meaning myClass.OnEvent is NOT called when server uses methodinfo.invoke and mbox is NOT displayed

both server and client contain references to myObject DLL that has both myObject and MyDerivedObject MyDerivedObject is of course derived from myObject

Help please


Solution

  • The example that @eugene gave can also be applied to a service contract, so in your case you could apply the ServiceKnownType to the callback contract.

    [ServiceContract]
    [ServiceKnownType(typeof(MyDerivedObject))]
    interface IEvent
    {
        [OperationContract(IsOneWay = true)]
        void OnEvent(string serverName, string changeType, List<myObject> myObjects);
    }