[英]How to wait for all responses of WCF Method ?
我有WCF方法,该方法将所有玩家(客户)的分数都添加到其中以进行排序。
但是不幸的是,当第一个分数发送到服务器时,该功能将其添加到列表中,并将排序后的列表发送给客户端,而不等待其他玩家的所有其他分数。 我尝试使用async&await来将方法的继续延迟约30秒,如下所示:
public List<Player> GetListOfWinners(int SentScore , string _SentPlayerID )
{
List<Player> PlayersList = new List<Player>();
//Add to the List
PlayersList.Add(new Player { PlayerID = _SentPlayerID, Score = SentScore });
DelayMethod();
//Order It
List<Player> SortedList = PlayersList.OrderByDescending(p => p.Score).ToList();
// sent fULL SORTHEDlist to the Clients
return SortedList;
}
private async void DelayMethod()
{
await Task.Delay(30000);
}
但是它没有用,那我该怎么办?
我创建了一个非常基本的服务作为示例,以展示如何实现某些目标。 提供此方法是为了向您介绍诸如lock
和ManualResetEvent
构造。
在我的ISudokuConcurrentService.cs中:
namespace SudokuTest
{
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
[ServiceContract]
public interface ISudokuConcurrentService
{
[OperationContract]
SudokuGame ClientConnect();
[OperationContract]
List<Player> GetListOfWinners(int SentScore, string _SentPlayerID);
}
[DataContract]
public class SudokuGame
{
[DataMember]
public string PlayerID { get; set; }
[DataMember]
public int TimeLimitInSeconds { get; set; }
}
[DataContract]
public class Player
{
[DataMember]
public string PlayerID { get; set; }
[DataMember]
public int Score { get; set; }
}
}
在我的SudokuConcurrentService.svc.cs中:
namespace SudokuTest
{
using System.Collections.Generic;
using System.Linq;
using System.Threading;
public class SudokuConcurrentService : ISudokuConcurrentService
{
static int playerCount = 0;
static List<Player> PlayersList = null;
static object ListLock = new object();
const int TimeLimit = 120;
static ManualResetEvent AllPlayerResponseWait = new ManualResetEvent(false);
public SudokuGame ClientConnect()
{
lock (ListLock)
{
if (PlayersList != null)
{
// Already received a completed game response -- no new players.
return null;
}
}
return new SudokuGame()
{
PlayerID = "Player " + Interlocked.Increment(ref playerCount).ToString(),
TimeLimitInSeconds = TimeLimit
};
}
public List<Player> GetListOfWinners(int SentScore, string _SentPlayerID)
{
lock (ListLock)
{
// Initialize the list.
if (PlayersList == null) PlayersList = new List<Player>();
//Add to the List
PlayersList.Add(new Player { PlayerID = _SentPlayerID, Score = SentScore });
}
// Now decrement through the list of players to await all responses.
if (Interlocked.Decrement(ref playerCount) == 0)
{
// All responses received, unlock the wait.
AllPlayerResponseWait.Set();
}
// Wait on all responses, as necessary, up to 150% the timeout (no new players allowed one responses received,
// so the 150% should allow time for the last player to receive game and send response, but if any players have
// disconnected, we don't want to wait indefinitely).
AllPlayerResponseWait.WaitOne(TimeLimit * 1500);
//Order It
List<Player> SortedList = PlayersList.OrderByDescending(p => p.Score).ToList();
// sent fULL SORTHEDlist to the Clients
return SortedList;
}
}
}
请注意,此示例的主要局限性在于它仅允许在服务有效期内玩单个游戏。 我将继续运行多个游戏作为练习,但是要指出,您需要跟踪每个游戏现在正在做的所有事情。 您可能希望将列表捆绑在一起并等待锁进入对象,然后可以通过某种方式(例如MemoryCache
存储对象的另一个列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.