The simple case where my OperationContract implementation is like:
public List<Directory> GetDirectories(bool includeFiles)
{
if (includeFiles)
{
return this.db.Directories.Include(e => e.Files).ToList();
}
else
{
return this.db.Directories.ToList();
}
}
where GetDirectories(false); works perfectly ok and GetDirectories(true); throws a CommunicationObjectFaultedException with message:
The communication object, System.ServiceModel.Channels.ServiceChannel,
cannot be used for communication because it is in the Faulted state.
Obviously my File entities have reference to Directory entities, and the Directory entities have a list of files. First I thought this would be the typical cyclic reference trap but I get no signs of it in the exception message. Any ideas on this problem?
It will be cyclic reference trap (here is something about this topic) and reason for your
CommunicationObjectFaultedExceptionwill be something like:The reason is that unhandled exception has faulted the channel and
usingis trying to callCloseon that faulted channel – it is invalid transition in channel state machine (and one big WCF strangeness) resulting it the exception you mentioned. There are many ways to avoid it but the basis is: