WCF/EF 4.1延迟加载出现问题

问题描述:

我正在通过WCF服务获取应用程序的数据.在服务器端,该服务使用EF4.1作为数据访问. 服务方法看起来像这样:

I'm getting data for my application through WCF service. And on the server side the service is using EF4.1 as a data access. Service method looks kind of like this :

public List<JobOffer> GetAllJobOffers()
{
    var allJobOffers = _jobOffersRepository.GetAll().ToList();
    return allJobOffers;
}

存储库是通过这种方式完成的

And the repository is done this way

public override IQueryable<JobOffer>GetAll()
{
    return _context.JobOffers.Include(c => c.Company);
}

我遇到了这个奇怪的错误:

I am getting this strange error :

An error occurred while receiving the HTTP response to http://localhost:8080/JobsService/ws. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

An error occurred while receiving the HTTP response to http://localhost:8080/JobsService/ws. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

在调试模式下,按下F5后,它将开始永远循环.

In debug mode after hitting F5 it starts to loop forever.

在我的情况下,服务器端的延迟加载应该会出现一些问题.因为当我调用该服务以返回具有简单结构的对象时,它的工作原理就像冠军.可能会搞砸什么?渴望用EF加载对象的最佳实践是什么?

In my case it should some problem with lazy loading on the server side. Because when I am calling the service to return objects with simple structure it works like a champ. What could be messed up? And what is the best practice to eager load objects with EF ?

在WCF上使用EF时,应禁用延迟加载,因为序列化将触发每个导航属性的延迟加载.另一个问题是循环引用,它们默认情况下无法序列化.如果您的Company也具有对其Jobs的导航属性,则会进行循环引用.

When using EF over WCF lazy loading should be turned off because serialization will trigger lazy loading of every navigation property. Another problem are cyclic references which are not serializable by default. If your Company has also navigation property to its Jobs it makes cyclic reference.