WCF - Decorating IEnumerable<T> with DataMember causes Exception:The underlying connection was closed: The connection was closed unexpectedly
Asked Answered
R

1

8

I have created a WCF service which returns IEnumerable<CyberResourceProvisioningAction>.

The CyberResourceProvisioningAction type has a property of AccountInformation IEnumerable<CyberResourceProvisioningActionAccountInfo>. When I decorate the AccountInformation property with DataMemberAttribute I receive the exception:

WCF System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly

Obviously a very generic exception, but my Google-fu indicates that the issue most commonly occurs when returning large numbers of objects in a collection. The suggested fix is to set the <dataContractSerializer maxItemsInObjectGraph="2147483646"/>. Unfortunately this has not fixed my issue. (Didn't think it would as I am returning a small amount of data).

The properties are being set correctly so I am pretty sure my issue has to do with my serialization configuration. Is there something wrong with my classes which is causing the WCF service to error in this way?

[DataContract]
public class CyberResourceProvisioningAction
{
    [DataMember]
    public string Action { get; set; }

    [DataMember]
    public DateTime RcdChgDateTime { get; set; }

    [DataMember]
    public string CyberResourceName { get; set; }

    [DataMember]
    public IEnumerable<CyberResourceProvisioningActionAccountInfo> AccountInformation
    { get; set; }
}

CyberResourceProvisioningActionAccountInfo

[DataContract]
public class CyberResourceProvisioningActionAccountInfo
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Value { get; set; }
}

If additional configuration information is required let me know and I'll edit the post.

Retractor answered 3/2, 2011 at 20:39 Comment(3)
if you have the help page enabled, browse to it to see if you're getting an activation exception; otherwise attach a debugger and trap exceptions. It sounds like it might be a DataContract programming model violation.Ethelyn
@Ethelyn I've attached a debugger but never trap any exceptions. The exception shows up in the WCF Test Client but not my debugger.Retractor
Try debugging your WCF host project directly, and test your servicecall with the WcfTestClientTalie
R
14

Because of the comment about "DataContract programming model violation" left by alexdej I started looking a bit closer at what was in my properties. I had a Linq type in the property and though it was an IEnumerable it wasn't being enumerated for serialization. Added a .ToList() and all is well.

Retractor answered 3/2, 2011 at 21:7 Comment(1)
I receive from my Service following error Type 'System.Data.Entity.Infrastructure.DbQuery1[MYCLASS]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.. Adding .ToList() before return statement sort out my problem. Thanks!Marrowfat

© 2022 - 2024 — McMap. All rights reserved.