WCF 回调不会传递派生类
客户端
我的客户端获得了以下回调合同:
[ServiceContract]
interface IEvent
{
[OperationContract(IsOneWay = true)]
void OnEvent(string serverName, string changeType, List<myObject> myObjects);
}
并获得了一个实现 IEvent 的类,如下所示:
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!");
}
}
服务器端
我的服务器获得了以下发布代码:
methodInfo.Invoke(client, new object[] {serverName, changeType, myObjects});
的问题
当我将 methodInfo.Invoke 与 myObjects = 一起使用时出现 new List
一切正常,这意味着当服务器使用 methodinfo.invoke 时 myClass.OnEvent 被调用 并且当我尝试发送 myObjects = new List
时显示 mbox
BUT
它不起作用,意味着 myClass.OnEvent 是 <当服务器使用 methodinfo.invoke 时,不会调用 并且 mbox NOT 显示
服务器和客户端都包含对 myObject DLL 的引用,该 DLL 同时具有 myObject 和 MyDerivedObject MyDerivedObject 当然是从 myObject 派生的
请帮忙
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@eugene 给出的示例也可以应用于服务合约,因此在您的情况下,您可以将 ServiceKnownType 应用于回调合约。
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.
为了使 WCF 能够使用派生类,应该使用每个派生类的
KnownType
属性来修饰基类。不幸的是,这意味着基类应该了解其派生类。在你的情况下,它将是这样的: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: