c# - 调用具有两个相同参数的方法返回两个不同的结果

标签 c# wcf debugging keynotfoundexception

我有一个简单的项目,使用 MVC 模式、ET 和 WCF。它使用 Dijkstra 来寻找城市之间的路径。 This is the class itself.

但是,当使用 map 对象中的两个相同列表调用 FindPathFromCityToCity() 时,相同的 fromto,当它在普通的控制台应用程序上时,它会返回所需的结果。但是,当从 WCF 使用时,它在第 39 行失败并出现异常 System.Collections.Generic.KeyNotFoundException

为了检查我的输入是否不正确,我添加了对 txt 文件的写入(第 17 到 27 行),这就是它的结果(TL;DR - 两者完全相同):

控制台:

COUNT: 14 - RANGE 120
FROM: Aalborg - TO: Copenhagen
C: Frederikshavn - E: 2 - T: BLL.BatteryCenter
C: Aalborg - E: 6 - T: BLL.BatteryCenter
C: Hobro - E: 4 - T: BLL.BatteryCenter
C: Randers - E: 6 - T: BLL.BatteryCenter
C: Viborg - E: 8 - T: BLL.BatteryCenter
C: Aarhus - E: 8 - T: BLL.BatteryCenter
C: Herning - E: 6 - T: BLL.BatteryCenter
C: Vejle - E: 8 - T: BLL.BatteryCenter
C: Kolding - E: 6 - T: BLL.BatteryCenter
C: Odense - E: 6 - T: BLL.BatteryCenter
C: Aabenraa - E: 2 - T: BLL.BatteryCenter
C: Koge - E: 4 - T: BLL.BatteryCenter
C: Copenhagen - E: 2 - T: BLL.BatteryCenter
C: Soro - E: 4 - T: BLL.BatteryCenter

WCF:

COUNT: 14 - RANGE 120
FROM: Aalborg - TO: Copenhagen
C: Frederikshavn - E: 2 - T: BLL.BatteryCenter
C: Aalborg - E: 6 - T: BLL.BatteryCenter
C: Hobro - E: 4 - T: BLL.BatteryCenter
C: Randers - E: 6 - T: BLL.BatteryCenter
C: Viborg - E: 8 - T: BLL.BatteryCenter
C: Aarhus - E: 8 - T: BLL.BatteryCenter
C: Herning - E: 6 - T: BLL.BatteryCenter
C: Vejle - E: 8 - T: BLL.BatteryCenter
C: Kolding - E: 6 - T: BLL.BatteryCenter
C: Odense - E: 6 - T: BLL.BatteryCenter
C: Aabenraa - E: 2 - T: BLL.BatteryCenter
C: Koge - E: 4 - T: BLL.BatteryCenter
C: Copenhagen - E: 2 - T: BLL.BatteryCenter
C: Soro - E: 4 - T: BLL.BatteryCenter

有人能看出发生这种情况的原因吗?


编辑:

我做了进一步的测试,使用这个片段和 there is no difference 打印所有可能的对象在对象中(当然除了散列):

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\log\check.txt", true))
{
    foreach (var m in map.Stations)
    {
        file.WriteLine(" --- {0} - {1} - {2} - {3} --- ", m.name, m.Edgelist.Count, m.GetType(), m.GetHashCode());
        foreach(var e in m.Edgelist)
        {
            file.WriteLine();
            file.WriteLine(" # {0} - {1} - {2} - {3}", e.BatteryStation.name, e.BatteryStation1.name, e.distance, e.edgeID);
            file.WriteLine(" + {0} - {1} - {2}", e.BatteryStation.GetType(), e.BatteryStation.stationID, e.BatteryStation.GetHashCode());
            file.WriteLine(" - {0} - {1} - {2}", e.BatteryStation1.GetType(), e.BatteryStation1.stationID, e.BatteryStation1.GetHashCode());
        }
        file.WriteLine();
   }
}

最佳答案

这一切都取决于 BatteryCenter 对象的机制。

调用代码将其作为参数传入,函数将其用作映射中的键。

您是否覆盖了 BatteryCenter 上的函数 EqualsGetHashCode

如果不是,则字典查找将搜索确切的 对象。不是具有所有相同字段的 BatteryCenter,而是具有相同引用的对象。

WCF 客户端将传递一个等效但仍然不同的 BatteryCenter,字典无法找到它。

一旦您为 BatteryCenter.EqualsBatteryCenter.GetHashCode 添加覆盖,这可能会起作用。

例如

public override int GetHashCode() 
{
     return this.stationID.GetHashCode() ^ this.name.GetHashCode();
}

public override bool Equals(Object obj) 
{
     if(Object.ReferenceEquals(this, obj))
     {
          return true;
     }
     BatteryStation other = obj as BatteryStation;
     if(Object.ReferenceEquals(other, null))
     {
         return false;
     }
     return ((this.stationID == other.stationID) && (this.name == other.name));
}

关于c# - 调用具有两个相同参数的方法返回两个不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671737/

相关文章:

debugging - 调试嵌入式软件(核心转储)

c# - Hangfire:设置重复作业的结束时间

c# - 具有调用问题的线程和 GUI

c# - ASP.NET Core 是否具有内置的 OAuth2 token 交换功能?

WCF Web 服务在接收大数据时失败

wcf - 从客户端端点配置获取/修改地址

c - 我的程序在调试时运行正确,但在调试结束时运行不正确

c# - 在 C# 中实现管道 - 使用哪些(流?)类?

C# SOAP-Service 方法参数上缺少命名空间

c++ - 在VScode中进行调试时,如何在Docker容器中找到源代码文件夹