cce1911
cce1911

Reputation: 353

How to return a List<> from a WCF call

I'm just getting up to speed using WCF. I've created my WCF service and I'm hosting it in a console app.

My custom class is shared between the service and the client.

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

Here is my interface

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

Here is my service

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;
    }
}

In my Windows Client app, I call the service reference

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

The code won't compile giving me the following error:

Cannot implicitly convert type System.Collections.Generic.List<TabletController.PlinkerWCFservice.clsPlinkerGame> to System.Collections.Generic.List<PlinkerCommon.clsPlinker.Game>

TabletController is the namespace of my WinForms app.

In another method, I can return a single clsPlinker.Game, but trying to return a List has me stumped. I've searched for hours, to no avail. What am I doing wrong.

Upvotes: 0

Views: 3921

Answers (2)

wakthar
wakthar

Reputation: 718

When adding service reference, change collection type from System.Array to System.Collections.Generic.List and Reuse types in all referenced assemblies.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222722

Change like this,

Your result is of type TabletController.PlinkerWCFservice.clsPlinkerGame but you are having clsPlinker.Game

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

Upvotes: 0

Related Questions