如何等待WCF方法的所有响应
本文关键字:方法 响应 WCF 何等待 等待 | 更新日期: 2023-09-27 18:08:33
我有一个WCF方法,它可以获取所有玩家(客户端)的分数,并将它们添加到一个列表中进行排序。
但不幸的是,当第一个分数发送到服务器时,该函数将其添加到列表并将排序列表发送到客户端,而不是等待其他玩家的所有其他分数。我尝试使用async &等待,以延迟方法的继续,大约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
等结构的方法而提供的。
In my 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