Search code examples
wcfserializationdatacontract

WCF List(Of CustomClass) inside DataContract Class Results in Random "The connection was closed unexpectedly" error


Is there an issue with returning a List(Of CustomClass) inside a DataContract()? For some reason, I am randomly getting my WCF connection closed and I think it's possibly due to Serialization but I'm not sure how to go about correcting it? The problem does not occur if I do not try to return a List(Of CustomClass).

To give you a better idea of what's going on, I'm trying to pass information about a page to a WCF service. Like a search crawler... Page Title, Content, Links, etc. The List(Of CustomClass) is a list of Links. The CustomClass just holds the links information... Anchor, URL, Is it an Image, etc...

What I was trying to do is pass one class that encapsulated the page's information and a List(Of CustomClass) which represented the links infromation.

Something like:

<OperationContract()>
Public Function Add_Page(Byval Pages as List(Of PageInfo)) as Integer 

<DataContract()>
Public Class PageInfo
    <DataMember()>
    Public Page_Title as string

    <DataMember()>
    Public Page_Links as List(Of Links)
End Class

<DataContract()>
Public Class Links
    <DataMember()>
    Public Anchor as string

    <DataMember()>
    Public URL as string
End Class

It works half the time but for some reason it will give me a generic "The underlying connection was closed: The connection was closed unexpectedly." without any Trace information Server-Side the other half of the time. I've got the MaxItemsInGraph maxed out, along with the other settings, so that shouldn't be the cause. It's weird it works sometimes and not others. The only way I can rid this error is by removing the List(Of Links) from the example above.

I'm in the process of rewriting the code to see if it works by passing the List(Of Links) through the OperationContract, kinda like:

<OperationContract()>
Public Function Add_Page(Byval Pages as List(Of PageInfo), Byval Page_Links as List(Of Links)) as Integer 

Which I have a feeling will work fine as I have several other functions that work in a similar fashion without problem. Something tells me I am doing something wrong though, not serializing correctly or ??? Is this possible to do or is my second example what I will have to resort to using? Thanks

Edit #1 (Did not work)

The DataMember(EmitDefaultValue:=True) suggested in the Comment didn't fix the problem so I wrote client-side code to double check there was no Nulls/Empty values being sent (this was done server-side before saving to DB)... Didn't seem to fix the problem either. Then I realized I was Removing duplicates from the List before sending, after I checked for Nulls/Empty values.

I removed the code that checked for duplicates and now it seems to be working... I'm not sure why, the Remove Dupe code assigned a New List, after it was uniquely populated to a List(Of Links). I'm wondering if it some how was leaving Nulled values after removing Duplicates/reassigning the new list over the old. I'm going to test this a little more, if I don't have any more issues, I'll mark your response as the answer as I believe you are correct in assuming a nulled value was jacking the transfer up.. ... After more testing, this did not fix the problem. In the process of narrowing things down. Will report back.


Solution

  • I received this error as well. I believe it had to do with me returning back a null value with the list of records being returned. I change this in the my DataMember attribute to be: [DataMember(EmitDefaultValue = true)] but I also did a check for null values because I didn't want them returned in my query. You may, but look at the data that is being returned, if there is null values in it, do a check to not send those records through and see if that works.

    Edit: my brackets were because I did this in c# so you just have to conver it to VB.