简体   繁体   中英

C# WCF - Using entity from Service in client app

I am new in WCF and have simple issue. In my service library i have class :

[DataContract]
public class Player
{
    [DataMember]
    public string UserName { get; set; }
    [DataMember]
    public int WinNum { get; set; }
    [DataMember]
    public int LoseNum { get; set; }
    [DataMember]
    public int PlayedTime { get; set; }
    .
    .
    .
    [OperationContract]
    public override string ToString()
    {
        return UserName;
    }

Service class look like this:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class PiskvorkyService :IPiskvorkyService
{
    [DataMember]
    public List<Player> Players { set; get; }

    public PiskvorkyService()
    {
        Players = new List<Player>();
    }
       .
       .
       .
    public List<Player> GetPlayers()
    {
        return Players;
    }

From client app (WPF) i can add some new Players into list on server, it work well. But now i need to get Player list from service in client app and list them wherever I need. I tried something like this in client app:

    var Players = ps.GetPlayers(); //ps is instance of PiskvorkyServiceClient();
    foreach (var player in Players)
    {
        MessageBox.Show(player.ToString());
    }

But for each Player in list i just get:

播放器错误的字符串表示形式

Could someone help me please?

Thanks

This has nothing to do with WCF , it simply that you are using ToString on an object which haven't overridden it, which means you'll get the default implementation, which gives you the class name.

Do either:

public class Player
{
    ...
    public override string ToString()
    {
        return this.UserName; // or whatever you wish to print
    }
    ...
}

Or:

MessageBox.Show( player.UserName );

You have overridden the ToString -method on the service side

[DataContract]
public class Player
{
    .
    .
    .
    [OperationContract]
    public override string ToString()
    {
        return UserName;
    }

But methods are not serialized and therefor not transfered to the client side. This means you receive a player object with only its data (all fields and/or properties decorated with [DataMember] ).

This is the default behaviour.

So your best choice would be, as @Chris already mentioned, to call player.UserName on the client side.

Some other approach you can follow is to extend your model via extension methods on the client side. So you can do something like the following:

public static class PlayerExtensions
{
    public static string Print(this Player player)
    {
        return player.Username;
    }
}

Then you can use your player-objects like this on the client side:

var player = new Player { UserName = "Nick" };
string plyerName = player.Print();

Call player.UserName on the client side. var Players = ps.GetPlayers(); //ps is instance of PiskvorkyServiceClient(); foreach (var player in Players) { MessageBox.Show( player.UserName ; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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