简体   繁体   English

WCF“字典中不存在给定的键”

[英]WCF “The given key was not present in the dictionary”

I'm getting this error even though I'm not using a Dictionary, and what's weird is that it's when I call the service. 即使我没有使用字典,也遇到了这个错误,奇怪的是它是在我致电服务时。

wsSoapClient client = null;
try
{
    client = new wsSoapClient();
}
catch (Exception ex)
{ 
    // - Error in the web.config
}

try
{
    SendData sendData = new SendData();
    sendData.finishDate = myVar.FinishDate;
    sendData.startDate = myVar.StartDate;
    // - Other fields

    // - This lines throw the error below
    ClientResult result = client.FinishCourse(sendData);
}
catch (Exception ex)
{
    // - Message: The given key was not present in the dictionary.
}

The stack trace: 堆栈跟踪:

StackTrace: at System.Collections.Generic.Dictionary``2.get_Item(TKey key) at Project.Model.CourseService.FinishCourse(XmlNode node)

The service is up to date, and I couldn't find info on this anywhere else. 该服务是最新的,我在其他任何地方都找不到此信息。 There's two similar questions on SO but they are about Silverlight and I couldn't figure a relation between this (regular C# WCF calling a service) and the solutions. 在SO上有两个类似的问题,但它们与Silverlight有关,我无法弄清楚它(常规C#WCF调用服务)与解决方案之间的关系。

What causes this and how do I fix it? 是什么原因造成的,我该如何解决?

(Edit) More Info : The binding is a basicHttpBinding, http only. (编辑)更多信息 :绑定是basicHttpBinding,仅http。

Edit² : The WSDL . 编辑²WSDL

Edit³ : Found the issue. 编辑³ :发现问题。 Apparently, there was already another error (a value larger than the field allows), the service was returning an error but for some reason, WCF didn't take that as error and didn't threw the exception, and I'm guessing it tried to proceed normally, causing this dictionary error since the XML is not what is was expecting. 显然,已经存在另一个错误(该值大于字段允许的值),该服务正在返回错误,但由于某种原因,WCF并未将其视为错误并且没有引发异常,我猜是它尝试正常进行,导致此字典错误,因为XML不是预期的。

Assuming your service method is not the one throwing the exception, then it might be the case with the deserialization of the soap message (ie request object) that happens on the service side. 假设您的服务方法不是抛出异常的方法,那么在服务端发生的肥皂消息(即请求对象)的反序列化可能就是这种情况。

Check your code(or wsdl) for SendData and see if there are non nullable properties which you are not setting in the request object ie sendData object. 检查您的代码(或wsdl)中的SendData,并查看是否存在未在请求对象(即sendData对象)中设置的不可为空的属性。 Missing required properties might cause issues during deserialization. 缺少必需的属性可能会在反序列化期间引起问题。

Posting the code of SendData and\\or FinishCourse service method would be great in analyzing the possible issue. 发布SendData和/或FinishCourse服务方法的代码对于分析可能的问题非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM