[英]WCF + EF 4.1 Eager Loading and Serialization Problem
The simple case where my OperationContract
implementation is like: 我的OperationContract
实现的简单情况如下:
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);
其中GetDirectories(false);
works perfectly ok and GetDirectories(true);
可以正常工作,并且GetDirectories(true);
throws a CommunicationObjectFaultedException
with message: 抛出带有消息的CommunicationObjectFaultedException
:
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state. 通信对象System.ServiceModel.Channels.ServiceChannel由于处于故障状态,因此无法用于通信。
Obviously my File
entities have reference to Directory
entities, and the Directory
entities have a list of files. 显然,我的File
实体引用了Directory
实体,并且Directory
实体具有文件列表。 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 CommunicationObjectFaultedException
will be something like: 这将是循环引用陷阱( 这是有关此主题的内容),而CommunicationObjectFaultedException
原因将类似于以下内容:
using (var client = new ServiceClient())
{
data = client.GetDirectories(true);
}
The reason is that unhandled exception has faulted the channel and using
is trying to call Close
on that faulted channel - it is invalid transition in channel state machine (and one big WCF strangeness) resulting it the exception you mentioned. 其原因是,未处理的异常故障的通道和using
试图调用Close
对故障通道-它是在通道状态机(和一个大的WCF陌生感),导致它,你提到的异常无效化。 There are many ways to avoid it but the basis is: 有很多方法可以避免这种情况,但是基础是:
ServiceClient client = null;
try
{
client = new ServiceClient();
data = client.GetDirectories(true);
}
finally
{
if (client != null)
{
if (client.State == CommunicationState.Faulted)
{
client.Abort();
}
else
{
client.Close();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.