简体   繁体   中英

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.

What causes this and how do I fix it?

(Edit) More Info : The binding is a basicHttpBinding, http only.

Edit² : The 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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