我正在使用带有处理器队列的客户端-服务器实现。但是当必须将用户添加到队列时,我遇到了问题,添加的方法无法访问。
这是类 ManagerServiceClient
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 { }
}
}
);
}
每个客户端都将被添加到队列中,但从未到达 managerserviceclient 类中的 NewQueueItem 方法。这里有什么问题?
问候
解决了!我正在使用 3 个需要启动的单独项目,但是 1 个项目必须在没有调试的情况下启动,所以我的另一个项目中的代码是可以访问的..