Question

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

Was it helpful?

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);
}

OTHER TIPS

To WCF be able using derived classes, the base class should be decorated with a KnownType attribute for each of derived classes. Unfortunately, this means that base class should know about its derived classes. In your case it will be like:

[KnownType(typeof(MyDerivedObject))]
[DataContract]
class myobject
{
     // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top