问题描述
我有一个WCF服务,我试图在我的一个操作合同中返回一个List(IWatchable是我建立的自定义接口)。当我在客户端上测试服务时,该方法返回 object []
,而不是 List< IWatchable>
即可。是否可以返回IWatchable列表,因为IWatchable是WCF的接口?
I have a WCF service where I am trying to return a List (where IWatchable is a custom interface I have built) in one of my operation contracts. When I test the service on the client the method returns an object[]
instead of List<IWatchable>
. Is it possible to return a List of IWatchable, since IWatchable is an interface with WCF?
方法:
public List<IWatchable> GetWorkload( Guid nodeId, int maximum )
IWatchable:
IWatchable:
public interface IWatchable
{
string ActionName { get; set; }
Guid ActionReference { get; set; }
}
希望更多信息有用......
Hopefully a bit more info will be helpful...
我有一个派生界面:
public interface IAMRAWatchable: IWatchable
来自的三个具体实现IAMRAWatchable
:
public class InstrumentationWatch: IAMRAWatchable
public class OutputWatch: IAMRAWatchable
etc...
在我的WCF方法中,返回 列表< IWatchable>
我想向客户端发送一个 InstrumentationWatch
和一个 OutputWatch
...这可能还是我要解决这个问题错误的方式?
In my WCF method that returns List<IWatchable>
I want to send an InstrumentationWatch
and an OutputWatch
to the client... Is this possible or am I going about this the wrong way?
已解决
感谢John,我找到了解决方案。 KnownType
因为我使用 列表< IWatchable>
- 所以我把我的列表包装到一个新类中并添加了属性。我需要重新考虑我的代码,但对于其他感兴趣的人来说是这个类:
Thanks to John I found my solution. KnownType
wasn't working since I was using List<IWatchable>
- So I wrapped my list into a new class and added the attributes to it. I'll need to re-factor my code but for others who are interested here is the class:
[DataContract]
[KnownType( typeof( InstrumentationWatch ) )]
[KnownType( typeof( OutputWatch ) )]
public class WorkInfo
{
[DataMember]
public List<IWatchable> WorkQueue { get; set; }
}
和我的WCF方法:
public WorkInfo GetWorkload( Guid nodeId, int maximum )
推荐答案
接口永远不能被序列化。它只是对行为的描述。
An interface can never be serialized. It is only a description of behavior.
您可以序列化实现该接口的对象,但您必须告诉WCF它们的类型是什么。请参阅。
You can serialize objects which implement the interface, but you must tell WCF what their types are. See Data Contract Known Types.
这篇关于如何传递List< Interface>在WCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!