0

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

Nahum
  • 6,959
  • 12
  • 48
  • 69
  • Does the client have access to and load the library containing `MyDerivedObject`? – Hand-E-Food Jul 25 '11 at 06:29
  • @Hand-E-Food yes there is. both server and client contain references to myObject DLL that has both myObject and MyDerivedObject – Nahum Jul 25 '11 at 06:34

2 Answers2

3

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);
}
Bjorn Coltof
  • 499
  • 2
  • 6
0

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
{
     // ...
}
Eugene
  • 2,858
  • 1
  • 26
  • 25
  • what can I do in case myObject is in a closed DLL I cannot touch? – Nahum Jul 25 '11 at 06:47
  • You can try to create a wrapper object that holds the `myObject`, and define your list containing this wrapper. Additionally, you should add `ServiceKnownType` attribute to `OnEvent` method for each type: ` [ServiceKnownType(typeof(myObject))] [ServiceKnownType(typeof(MyDerivedObject))] [OperationContract] void OnEvent() ` – Eugene Jul 25 '11 at 07:09