将用户添加到队列的WPF客户端 - 服务器未到达方法

时间:2013-09-02 08:05:11

标签: c# client-server queue

我正在使用带有processerqueues的客户端 - 服务器实现。 但是当我必须将用户添加到队列时遇到问题,无法访问要添加的方法。

这是类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 { }
            }
        }
        );
    }

每个客户端都将添加到队列中,但是从未到达managererviceclient类中的方法NewQueueItem。这有什么不对吗?

迎接

解决!我正在使用需要启动的3个单独的项目,但是1个项目必须在没有调试的情况下启动,因此我的其他项目中的代码可以访问..

0 个答案:

没有答案