开发者

Problem with WCF/EF 4.1 Lazy Loading

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 s开发者_StackOverflow社区ervice 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.

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

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 ?


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.


I think your problem is more like to be related to the cyclic relationship. this all happens when serialization comes in scope. lets say u have e entity person which has property addresses which is marked as lazy. on the other side of relationship Addresses entity has a navigation property for Person entity. as serialization takes place it goes through each property of the person entity and as it access Addresses navigation property starts to load it. now it starts to serialize Address entity and as it access Person navigation property it goes back to that property and starts to serialize it and it creates situation when cyclic serialization starts and can not be completed. to overcome this problem u do not have to turn of lazy loading but all u have to do is to decorate you datacontract in (in this case Person and Addresse) with [DataContract(IsReference = true)] and everything works like an charm.


I know this is a bit of an old question, but based on your experiences in your question, I think it might help if you take a look through one of my recent posts http://sanderstechnology.com/2013/more-with-the-entity-framework-v6-rc1/12423/

I walk through how to send a semi-complex EF object graph which is deserialized through WCF, and I handle lazy loading, proxy generation and cyclic reference issues. It might be worth a look.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜