Windows服务中出现意外错误

时间:2017-01-31 10:28:04

标签: c# wcf windows-services

我已经运行了WCF Windows服务。它基本上注册了许多客户端,并在需要时向他们广播消息。 一般情况下它工作正常,但最近我尝试注册一个新客户端时遇到错误,已经有大约24个客户端已经连接,但是当我尝试注册第25个客户端时,我得到了#34;索引超出了数组的边界。"被退回。 如果我重新启动服务,则所有客户端都重新连接,新客户端可以注册。 调用NotifyServer方法以向所有已注册的客户端广播消息。这将通过客户端字典运行,创建发送消息的异步任务。这样做是为了发送到一个客户端时发生的任何问题都不会影响向其他客户端发送消息。 该服务设置为使用可靠连接。

    // List of connected Clients
    //
    private static Dictionary<string, IBroadcastorCallBack> clients = new Dictionary<string, IBroadcastorCallBack>();

    // lock indicator object 
    //
    private static object locker = new object();

    public string RegisterClient(string clientName)
    {
        string returnValue = "";

        if (!string.IsNullOrEmpty(clientName))
        {
            try
            {
                var callback = OperationContext.Current.GetCallbackChannel<IBroadcastorCallBack>();
                lock (locker)
                {
                    // Remove the old client if its already there
                    //
                    if (clients.Keys.Contains(clientName))
                    {
                        clients.Remove(clientName);
                    }

                    // Add the new one
                    //
                    clients.Add(clientName, callback);
                }
            }
            catch (Exception ex)
            {
                // Return any error message
                //
                returnValue = ex.Message;
            }
        }

        return returnValue;
    }

    /// <summary>
    /// Notify the service of a message that can be broadcast
    /// </summary>
    /// <param name="eventData">Message details</param>
    /// 
    public async void NotifyServer(EventDataType eventData)
    {

        DateTime Start = DateTime.Now;

        // Get a list copy of the dictionary  for all clients bar the one sending it. This is so we can't update the list inside the loop.
        //
        var clientlist = clients.Where(x => x.Key != eventData.ClientName).ToList();

        // Logging
        //
        if (MetricBroadcast.Properties.Settings.Default.LogXML)
        {
            log.Debug("XML Broadcast from " + eventData.ClientName + " to " + clientlist.Count.ToString() + " clients:\n" + eventData.EventMessage + "\n");
        }

        // Broadcast to all the valid clients
        //
        var BroadcastToClientList = clientlist.Select(client => BroadcastMessage(client.Value, client.Key, eventData)).ToList();

        // Wait until they are all done
        //
        await Task.WhenAll(BroadcastToClientList);

        // If we are logging and the broadcast time is > 1 second, we make a log entry
        //
        DateTime End = DateTime.Now;

        if (MetricBroadcast.Properties.Settings.Default.LogXML)
        {
            TimeSpan res = End - Start;

            if (res.TotalSeconds > 1)
            {
                var timetaken = string.Format("XML Broadcast Time : {0,2:00}:{1,2:00}.{2,3:000}", res.Minutes, res.Seconds, res.Milliseconds);

                log.Debug(timetaken);
            }
        }

    }

    private async Task<bool> BroadcastMessage(IBroadcastorCallBack clientCallback, string ClientKey, EventDataType eventData)
    {
        bool retval = true;

        Exception savedEx = null;

        DateTime BroadStart = DateTime.Now;

        try
        {
            // Send the message to the current client
            //
            clientCallback.BroadcastToClient(eventData);

        }
        catch (Exception e)
        {
            // If we can't access the current clients callback method, 
            // we remove them from the clients list, as they've probably lost their connection.
            //
            clients.Remove(ClientKey);

            savedEx = e;

            retval = false;
        }

        // Log any broadcast that took > .5 seconds
        //
        DateTime BroadEnd = DateTime.Now;

        if (MetricBroadcast.Properties.Settings.Default.LogXML)
        {
            TimeSpan res = BroadEnd - BroadStart;

            if (res.TotalSeconds > .5)
            {
                var timetaken = string.Format("Single XML Broadcast Time to " + ClientKey + " : {0,2:00}:{1,2:00}.{2,3:000}", res.Minutes, res.Seconds, res.Milliseconds);

                log.Debug(timetaken, savedEx);
            }
        }

        return retval;
    }

1 个答案:

答案 0 :(得分:0)

问题很可能是多线程环境中静态clients Dictionary的使用不正确。新客户可以随时注册,包括BroadcastMessageNotifyServer功能。 Dictionary并非设计用于从多个线程进行访问。以此为例:

clients.Where(x => x.Key != eventData.ClientName).ToList();

如果在此枚举过程中删除了客户端,会发生什么?谁知道,因为它不是为此设计的,但很可能会抛出一些异常(比如你的“索引越界”)。这就是为什么你需要为写入和读取锁定字典,而不仅仅是写入:

List<KeyValuePair<string, IBroadcastorCallback>> clientList;
lock (locker) 
    clientList = clients.Where(x => x.Key != eventData.ClientName).ToList();

另一种选择是使用ConcurrentDictionary类。 专为并发访问而设计,您可以安全地从多个线程读取和写入。