如何传递List< Interface>在WCF?

编程入门 行业动态 更新时间:2024-10-28 14:27:05
本文介绍了如何传递List< Interface>在WCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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&lt; Interface&gt;在WCF?

本文发布于:2023-11-11 11:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lt   amp   List   WCF   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!