所以我遇到了一个有趣的问题,在使用physicaladdress类型的键时,我在c字典中得到了重复的键。这很有趣,因为它只发生在很长一段时间之后,我不能在完全不同的机器上的单元测试中使用相同的代码来复制它。我可以在windows xp sp3机器上可靠地复制它,但只能在一次运行几天之后,即使这样,它也只能出现一次。
下面是我正在使用的代码,下面是该部分代码的日志输出。
代码

private void ProcessMessages()
{
    IDictionary<PhysicalAddress, TagData> displayableTags = new Dictionary<PhysicalAddress, TagData>();

    while (true)
    {
        try
        {
            var message = incomingMessages.Take(cancellationToken.Token);

            VipTagsDisappeared tagsDisappeared = message as VipTagsDisappeared;

            if (message is VipTagsDisappeared)
            {
                foreach (var tag in tagDataRepository.GetFromTagReports(tagsDisappeared.Tags))
                {
                    log.DebugFormat(CultureInfo.InvariantCulture, "Lost tag {0}", tag);

                    RemoveTag(tag, displayableTags);
                }

                LogKeysAndValues(displayableTags);

                PublishCurrentDisplayableTags(displayableTags);
            }
            else if (message is ClearAllTags)
            {
                displayableTags.Clear();
                eventAggregator.Publish(new TagReaderError());
            }
            else if (message is VipTagsAppeared)
            {
                foreach (TagData tag in tagDataRepository.GetFromTagReports(message.Tags))
                {
                    log.DebugFormat(CultureInfo.InvariantCulture, "Detected tag ({0}) with Exciter Id ({1})", tag.MacAddress, tag.ExciterId);

                    if (tagRules.IsTagRssiWithinThreshold(tag) && tagRules.IsTagExciterValid(tag))
                    {
                        log.DebugFormat(CultureInfo.InvariantCulture, "Detected tag is displayable ({0})", tag);

                        bool elementAlreadyExists = displayableTags.ContainsKey(tag.MacAddress);

                        if (elementAlreadyExists)
                        {
                            displayableTags[tag.MacAddress].Rssi = tag.Rssi;
                        }
                        else
                        {
                            displayableTags.Add(tag.MacAddress, tag);
                        }
                    }
                    else
                    {
                        log.DebugFormat(CultureInfo.InvariantCulture, "Detected tag is not displayable ({0})", tag);

                        RemoveTag(tag, displayableTags);
                    }
                }

                LogKeysAndValues(displayableTags);

                PublishCurrentDisplayableTags(displayableTags);
            }
            else
            {
                log.WarnFormat(CultureInfo.InvariantCulture, "Received message of unknown type {0}.", message.GetType());
            }
        }
        catch (OperationCanceledException)
        {
            break;
        }
    }
}

private void PublishCurrentDisplayableTags(IDictionary<PhysicalAddress, TagData> displayableTags)
{
    eventAggregator.Publish(new CurrentDisplayableTags(displayableTags.Values.Distinct().ToList()));
}

private void RemoveTag(TagData tag, IDictionary<PhysicalAddress, TagData> displayableTags)
{
    displayableTags.Remove(tag.MacAddress);

    // Now try to remove any duplicates and if there are then log it out
    bool removalWasSuccesful = displayableTags.Remove(tag.MacAddress);

    while (removalWasSuccesful)
    {
        log.WarnFormat(CultureInfo.InvariantCulture, "Duplicate tag removed from dictionary: {0}", tag.MacAddress);
        removalWasSuccesful = displayableTags.Remove(tag.MacAddress);
    }
}

private void LogKeysAndValues(IDictionary<PhysicalAddress, TagData> displayableTags)
{
    log.TraceFormat(CultureInfo.InvariantCulture, "Keys");
    foreach (var physicalAddress in displayableTags.Keys)
    {
        log.TraceFormat(CultureInfo.InvariantCulture, "Address: {0}", physicalAddress);
    }

    log.TraceFormat(CultureInfo.InvariantCulture, "Values");
    foreach (TagData physicalAddress in displayableTags.Values)
    {
        log.TraceFormat(CultureInfo.InvariantCulture, "Address: {0} Name: {1}", physicalAddress.MacAddress, physicalAddress.Name);
    }
}

处理消息使用如下:
Thread processingThread = new Thread(ProcessMessages);

GetFromTagReports代码
public IEnumerable<TagData> GetFromTagReports(IEnumerable<TagReport> tagReports)
{
    foreach (var tagReport in tagReports)
    {
        TagData tagData = GetFromMacAddress(tagReport.MacAddress);
        tagData.Rssi = tagReport.ReceivedSignalStrength;
        tagData.ExciterId = tagReport.ExciterId;
        tagData.MacAddress = tagReport.MacAddress;
        tagData.Arrived = tagReport.TimeStamp;

        yield return tagData;
    }
}

public TagData GetFromMacAddress(PhysicalAddress macAddress)
{
    TagId physicalAddressToTagId = TagId.Parse(macAddress);

    var personEntity = personFinder.ByTagId(physicalAddressToTagId);

    if (personEntity.Person != null && !(personEntity.Person is UnknownPerson))
    {
        return new TagData(TagType.Person, personEntity.Person.Name);
    }

    var tagEntity = tagFinder.ByTagId(physicalAddressToTagId);

    if (TagId.Invalid == tagEntity.Tag)
    {
        return TagData.CreateUnknownTagData(macAddress);
    }

    var equipmentEntity = equipmentFinder.ById(tagEntity.MineSuiteId);

    if (equipmentEntity.Equipment != null && !(equipmentEntity.Equipment is UnknownEquipment))
    {
        return new TagData(TagType.Vehicle, equipmentEntity.Equipment.Name);
    }

    return TagData.CreateUnknownTagData(macAddress);
}

创建物理地址的位置
var physicalAddressBytes = new byte[6];
ByteWriter.WriteBytesToBuffer(physicalAddressBytes, 0, protocolDataUnit.Payload, 4, 6);

var args = new TagReport
{
    Version = protocolDataUnit.Version,
    MacAddress = new PhysicalAddress(physicalAddressBytes),
    BatteryStatus = protocolDataUnit.Payload[10],
    ReceivedSignalStrength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(protocolDataUnit.Payload, 12)),
    ExciterId = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(protocolDataUnit.Payload, 14))
};

public static void WriteBytesToBuffer(byte[] oldValues, int oldValuesStartindex, byte[] newValues, int newValuesStartindex, int max)
{
    var loopmax = (max > newValues.Length || max < 0) ? newValues.Length : max;

    for (int i = 0; i < loopmax; ++i)
    {
        oldValues[oldValuesStartindex + i] = newValues[newValuesStartindex + i];
    }
}

注意以下几点:
邮件中的每个“标记”。标记包含一个“新”物理地址。
返回的每个标记数据也是“new”。
“tagrules”方法不会以任何方式修改传入的“tag”。
通过将物理地址(由相同字节创建的两个实例)放入字典中进行单独测试,会引发“KelaRealyEn存在”异常。
我也试过trygetvalue,结果是一样的。
一切正常的日志输出:
2013-04-26 18:28:34,347 [8] DEBUG ClassName - Detected tag (000CCC756081) with Exciter Id (0)
2013-04-26 18:28:34,347 [8] DEBUG ClassName - Detected tag is displayable (Unknown: ?56081)
2013-04-26 18:28:34,347 [8] TRACE ClassName - Keys
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC755898
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC756081
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC755A27
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC755B47
2013-04-26 18:28:34,347 [8] TRACE ClassName - Values
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC755898 Name: Scotty McTester
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC756081 Name: ?56081
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC755A27 Name: JDTest1
2013-04-26 18:28:34,347 [8] TRACE ClassName - Address: 000CCC755B47 Name: 33 1
2013-04-26 18:28:34,347 [8] TRACE ClassName - Current tags: Scotty McTester, ?56081, JDTest1, 33 1

获取重复密钥的日志输出:
2013-04-26 18:28:35,608 [8] DEBUG ClassName - Detected tag (000CCC756081) with Exciter Id (0)
2013-04-26 18:28:35,608 [8] DEBUG ClassName - Detected tag is displayable (Unknown: ?56081)
2013-04-26 18:28:35,608 [8] TRACE ClassName - Keys
2013-04-26 18:28:35,608 [8] TRACE ClassName - Address: 000CCC755898
2013-04-26 18:28:35,608 [8] TRACE ClassName - Address: 000CCC756081
2013-04-26 18:28:35,618 [8] TRACE ClassName - Address: 000CCC755A27
2013-04-26 18:28:35,618 [8] TRACE ClassName - Address: 000CCC755B47
2013-04-26 18:28:35,618 [8] TRACE ClassName - Address: 000CCC756081
2013-04-26 18:28:35,618 [8] TRACE ClassName - Values
2013-04-26 18:28:35,618 [8] TRACE ClassName - Address: 000CCC755898 Name: Scotty McTester
2013-04-26 18:28:35,618 [8] TRACE ClassName - Address: 000CCC756081 Name: ?56081
2013-04-26 18:28:35,648 [8] TRACE ClassName - Address: 000CCC755A27 Name: JDTest1
2013-04-26 18:28:35,648 [8] TRACE ClassName - Address: 000CCC755B47 Name: 33 1
2013-04-26 18:28:35,648 [8] TRACE ClassName - Address: 000CCC756081 Name: ?56081
2013-04-26 18:28:35,648 [8] TRACE ClassName - Current tags: Scotty McTester, ?56081, JDTest1, 33 1, ?56081

注意,所有的事情都发生在一个线程上(参见[8]),所以字典不可能同时被修改。这些摘录来自同一个日志和同一个流程实例。还要注意,在第二组日志中,我们得到了两个相同的键!
我正在调查的是:我已经将physicaladdress改为一个字符串,看看是否可以从嫌疑犯列表中删除它。
我的问题是:
上面的代码中有没有我没有看到的问题?
物理地址上的相等方法有问题吗?(这只是偶尔的错误?)
这本词典有问题吗?

最佳答案

dictionary期望不可变对象作为键,并具有稳定的gethashcode/equals实现。
这意味着对象放入字典后,gethashcode返回的值应该
不更改,对此对象所做的任何更改都不应影响equals方法。
虽然physicaladdress类设计为不可变的,但它仍然包含一些扩展点,
它的不变性是有缺陷的。
首先,它可以通过输入字节数组进行更改,
不是复制而是通过引用传递,如下所示:

var data = new byte[] { 1,2,3 };
var mac = new PhysicalAddress(data);
data[0] = 0;

其次,physicaladdress不是一个密封类,可以通过派生
通过重写构造函数/gethashcode/equals方法实现。
但是这个用例看起来更像是一个黑客,所以我们将忽略它,以及通过反射进行的修改。
只有先将physicaladdress对象放入字典中,才能实现这种情况,
然后修改其源字节数组,然后将其包装到新的physicaladdress实例中。
幸运的是,physicaladdress的gethashcode实现只计算一次hash,
如果修改了同一个实例,它仍然放在同一个字典桶中,
再按等号定位。
但是,如果源字节数组被传递到physicaladdress的另一个实例中,则
尚未计算-重新计算新字节[]值的哈希值,找到新的存储桶,
把复本插入词典。在极少数情况下,可以找到相同的桶
从新的散列,再次,没有重复插入。
下面是重现问题的代码:
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;

class App
{
  static void Main()
  {
    var data = new byte[] { 1,2,3,4 };
    var mac1 = new PhysicalAddress(data);
    var mac2 = new PhysicalAddress(data);
    var dictionary = new Dictionary<PhysicalAddress,string>();
    dictionary[mac1] = "A";
    Console.WriteLine("Has mac1:" + dictionary.ContainsKey(mac1));
    //Console.WriteLine("Has mac2:" + dictionary.ContainsKey(mac2));
    data[0] = 0;
    Console.WriteLine("After modification");
    Console.WriteLine("Has mac1:" + dictionary.ContainsKey(mac1));
    Console.WriteLine("Has mac2:" + dictionary.ContainsKey(mac2));

    dictionary[mac2] = "B";
    foreach (var kvp in dictionary)
      Console.WriteLine(kvp.Key + "=" + kvp.Value);
  }
}

注意注释行-如果我们取消注释它,“containskey”方法将为mac2预计算散列,并且即使在修改之后它也将是相同的。
所以我的建议是找到生成physicaladdress实例的一段代码,并创建
为每个构造函数调用创建新的字节数组副本。

10-08 16:24