C# 将用户添加到队列的WPF客户端服务器未到达方法
我正在使用processerqueues的客户机-服务器实现。 但是我遇到了一个问题,当必须将用户添加到队列时,要添加的方法无法访问 这是ManagerServiceClient类C# 将用户添加到队列的WPF客户端服务器未到达方法,c#,client-server,queue,C#,Client Server,Queue,我正在使用processerqueues的客户机-服务器实现。 但是我遇到了一个问题,当必须将用户添加到队列时,要添加的方法无法访问 这是ManagerServiceClient类 public class ManagerServiceClient: IManageServiceClient { IDashboardData dashboardData; IEventAggregator eventAggregator; public ManagerServiceCli
public class ManagerServiceClient: IManageServiceClient
{
IDashboardData dashboardData;
IEventAggregator eventAggregator;
public ManagerServiceClient(IDashboardData dashboardData, IEventAggregator eventAggregator)
{
this.dashboardData = dashboardData;
this.eventAggregator = eventAggregator;
}
public void ProcessUserList(User[] users)
{
dashboardData.Users.Clear();
users.ToList().ForEach(u => dashboardData.Users.Add(u));
}
public void NewQueueItem(Queues.Common.QueueItem item)
{
if (item.Action == Queues.Common.QueueActions.Server_ClientConnected)
{
if (dashboardData.Users.Where(u => u.Name == (item.Data as string)).Count() == 0)
{
if (dashboardData.MainObject == null)
{
dashboardData.AddUser(new User() { Id = Guid.NewGuid(), Name = (item.Data as string) });
}
else
{
dashboardData.MainObject.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(() =>
{
dashboardData.AddUser(new User() { Id = Guid.NewGuid(), Name = (item.Data as string) });
}));
}
}
}
以及类ManagerServiceServer:
class ManageServiceServer : ScsService, IManageServiceServer
{
ProcessorQueue waitingQueue;
private readonly ThreadSafeSortedList<long, ServiceClient> clients;
public ManageServiceServer()
{
clients = new ThreadSafeSortedList<long, ServiceClient>();
waitingQueue = new ProcessorQueue(100);
waitingQueue.AddProcessor(QueueActions.Manager_RequestUsers, ProcessRequestUsers);
waitingQueue.Start();
}
private void ProcessRequestUsers(object sender, QueueEventArgs e)
{
if (e.Item.ClientId != null)
{
SendUserListToClient((long)e.Item.ClientId);
}
}
public void SendUserListToClient(long clientId)
{
ServiceClient client = clients[clientId];
List<User> userlist = new List<User>();
client.ClientProxy.ProcessUserList(userlist.ToArray<User>());
}
public void Login(User userInfo)
{
if (clients.GetAllItems().Where(x => x.User.Id == userInfo.Id).FirstOrDefault() != null)
throw new UserAlreadyLoggedInException(String.Format("{0}: {1} already logged in.", userInfo.Id, userInfo.Name));
//Get a reference to the current client that is calling this method
var client = CurrentClient;
//Get a proxy object to call methods of client when needed
var clientProxy = client.GetClientProxy<IManageServiceClient>();
//Create a ChatClient and store it in a collection
var chatClient = new ServiceClient(client, clientProxy, userInfo);
clients[client.ClientId] = chatClient;
//Register to Disconnected event to know when user connection is closed
client.Disconnected += Client_Disconnected;
Console.WriteLine(String.Format("{0}: {1} logged in.", userInfo.Id, userInfo.Name));
waitingQueue.Add(new QueueItem() { Action = QueueActions.Manager_RequestUsers, ClientId = client.ClientId, Data = "" });
}
#region Private Methods
/// Handles Disconnected event of all clients.
/// </summary>
/// <param name="sender">Client object that is disconnected</param>
/// <param name="e">Event arguments (not used in this event)</param>
private void Client_Disconnected(object sender, EventArgs e)
{
//Get client object
var client = (IScsServiceClient)sender;
//Perform logout (so, if client did not call Logout method before close,
//we do logout automatically.
ClientLogout(client.ClientId);
}
/// connection fails.
/// </summary>
/// <param name="clientId">Unique Id of client that is logged out</param>
private void ClientLogout(long clientId)
{
//Get client from client list, if not in list do not continue
var client = clients[clientId];
if (client == null)
{
return;
}
//Remove client from online clients list
clients.Remove(client.Client.ClientId);
//Unregister to Disconnected event (not needed really)
client.Client.Disconnected -= Client_Disconnected;
Console.WriteLine(String.Format("{0}: {1} logged out.", client.User.Id, client.User.Name));
}
#endregion
public void NewQueueItem(QueueItem item)
{
Task.Factory.StartNew(() =>
{
foreach (var client in clients.GetAllItems())
{
try
{
client.ClientProxy.NewQueueItem(item);
}
catch { }
}
}
);
}
类ManageServiceServer:ScsService,IManageServiceServer
{
处理器队列等待队列;
私有只读ThreadSafeSortedList客户端;
公共ManageServiceServer()
{
clients=新的ThreadSafeSortedList();
waitingQueue=新处理器队列(100);
waitingQueue.AddProcessor(QueueActions.Manager_RequestUsers,ProcessRequestUsers);
waitingQueue.Start();
}
私有void ProcessRequestUsers(对象发送方、QueueEventArgs e)
{
如果(e.Item.ClientId!=null)
{
SendUserListToClient((长)e.Item.ClientId);
}
}
public void SendUserListToClient(长clientId)
{
ServiceClient=clients[clientId];
List userlist=新列表();
client.ClientProxy.ProcessUserList(userlist.ToArray());
}
公共无效登录(用户userInfo)
{
if(clients.GetAllItems().Where(x=>x.User.Id==userInfo.Id).FirstOrDefault()!=null)
抛出新的UseralReadyLoggedException(String.Format(“{0}:{1}已登录。”,userInfo.Id,userInfo.Name));
//获取对调用此方法的当前客户端的引用
var-client=CurrentClient;
//获取代理对象,以便在需要时调用客户端的方法
var clientProxy=client.GetClientProxy();
//创建聊天客户端并将其存储在集合中
var chatClient=newserviceclient(客户端、客户端代理、用户信息);
clients[client.ClientId]=chatClient;
//注册到Disconnected事件以了解用户连接何时关闭
client.Disconnected+=client\u Disconnected;
WriteLine(String.Format(“{0}:{1}已登录。”,userInfo.Id,userInfo.Name));
waitingQueue.Add(new QueueItem(){Action=QueueActions.Manager_RequestUsers,ClientId=client.ClientId,Data=”“});
}
#区域私有方法
///处理所有客户端的断开连接事件。
///
///断开连接的客户端对象
///事件参数(不在此事件中使用)
私有无效客户端\u已断开连接(对象发送方,事件参数e)
{
//获取客户端对象
var client=(IScsServiceClient)发送方;
//执行注销(所以,若客户端在关闭之前并没有调用注销方法,
//我们会自动注销。
ClientLogout(client.ClientId);
}
///连接失败。
///
///已注销的客户端的唯一Id
私有无效客户端注销(长客户端ID)
{
//从客户机列表获取客户机,如果不在列表中,请不要继续
var client=clients[clientId];
if(客户端==null)
{
返回;
}
//从联机客户端列表中删除客户端
clients.Remove(client.client.ClientId);
//注销已断开连接的事件(实际上不需要)
client.client.Disconnected-=client\u Disconnected;
WriteLine(String.Format(“{0}:{1}已注销。”,client.User.Id,client.User.Name));
}
#端区
public void NewQueueItem(QueueItem项)
{
Task.Factory.StartNew(()=>
{
foreach(clients.GetAllItems()中的var client)
{
尝试
{
client.ClientProxy.NewQueueItem(项目);
}
捕获{}
}
}
);
}
每个客户端都将被添加到队列中,但managerserviceclient类中的方法NewQueueItem从未到达。这里有什么问题
问候
解决了!我使用了3个需要启动的独立项目,但是1个项目必须在没有调试的情况下启动,因此我的另一个项目中的代码是可以访问的。如果您说这一行
client.ClientProxy.NewQueueItem(item);
从未被调用,那么可能是因为clients.GetAllItems()
不返回任何内容?它返回客户机,但ManagerServiceClient中的方法NewQueueItem从未执行,无法在其内部进行调试。它是否实际返回任何内容?您是否调试了ManagerServiceServer
类中的NewQueueItem
方法?当我调试它时,客户机是正确的,但NewQueueItem方法d实际上没有做什么,没有任何更改或返回什么。它应该在ManageServiceClient类中执行NewQueueItem方法,但它没有这样做。尝试删除任务
,然后看看它是否会工作?你是说如果在NewQueueItem中的第一个if
条件上放置断点在ManagerServiceClient
中的code>方法中,该断点永远不会被命中?