繁体   English   中英

如何从WCF调用返回List <>

[英]How to return a List<> from a WCF call

我只是开始使用WCF。 我已经创建了WCF服务,并将其托管在控制台应用程序中。

我的自定义类在服务和客户端之间共享。

[Serializable]
public class clsPlinker
{
    [Serializable]
    public class Game : Object
    {
        public Game(string name, string desc, int GameNum)
        {
            _Name = name;
            _Desc = desc;
            _GameNum = GameNum;
        }
    }
}

这是我的界面

[ServiceContract]
public interface IPlinkerWCFservice
{
    [OperationContract]
    List<clsPlinker.Game> GetGameList();
}

这是我的服务

public class PlinkerWCFservice : IPlinkerWCFservice
{
    public List<clsPlinker.Game> GetGameList()
    {
        List<clsPlinker.Game> lstGames = new List<clsPlinker.Game>();
        // do stuff to load the lstGames
        return lstGames;
    }
}

在我的Windows客户端应用中,我称为服务参考

PlinkerWCFservice.PlinkerWCFserviceClient wcfTest = new PlinkerWCFservice.PlinkerWCFserviceClient("NetTcpBinding_IPlinkerWCFservice");
List<clsPlinker.Game> lstGames = wcfTest.GetGameList().ToList();

该代码将无法编译,但出现以下错误:

无法将类型System.Collections.Generic.List<TabletController.PlinkerWCFservice.clsPlinkerGame>隐式转换为System.Collections.Generic.List<PlinkerCommon.clsPlinker.Game>

TabletController是我的WinForms应用程序的名称空间。

在另一种方法中,我可以返回一个clsPlinker.Game,但是尝试返回List却使我很困惑。 我搜索了几个小时,无济于事。 我究竟做错了什么。

像这样改变

您的结果是TabletController.PlinkerWCFservice.clsPlinkerGame类型的,但是您拥有clsPlinker.Game

List<TabletController.PlinkerWCFservice.clsPlinkerGame> lstGames = wcfTest.GetGameList().ToList();

添加服务引用时,请将所有引用程序集中的集合类型从System.Array更改为System.Collections.Generic.List,然后重新使用类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM