EF4 Self-Tracking entities and WCF serialization creates stack overflow
I try to get above configuration working, but with no luck.
Step 1)
I started a new solution with a WCF Service Application project.
Step 2)
In this project, I added an edmx file and create a very simple model:
Entity Parent with Id and DisplayName Entity Child with Id and ChildDisplayName Association from Parent to Child, 1-to-m, resulting in NavigationProperties on both entities. I generatedthe database without any problems. After generation, I inserted one Parent object with two related Child objects manually to the database.Step 3)
I added the code generation, using the ADO.NET Self-Tracking Entity Generator. I know that this should be done in diffrent assemblies, but to make it straight and easy, I put it all to the same project (the WCF project)
Step 4)
I just changed the IService Interface to create a simple get
[OperationContract]
Parent GetRootData(Int32 Id);
In the corresponding implementation, I take an Page object from the context and return it:
using (PpjSteContainer _context = new PpjSteContainer() )
{
return _context.ParentSet.Include("Child").Single(x => x.Id == Id);
}
Problem:
If I now run this project (the Service1.svc is start page), VS2010 automatically generates the test client to invoke the service. But once I invoke the service, I get an StackOverflowException! Debugging on the server side looks ok until it returns the object graph.
If I remove the Include("Child") everything is ok, but of course the Child objects are missing now.
I have no idea what I'm missing. I read a lot of howto's and guides, but all do it the way I d开发者_运维技巧id it (at least that's what I think)... I tried the School example here, but this does not work for me as it seems the database generation and the coding in the example does not match.So, I would much appreciate if someone could guide me how to make this work.
P.S.
- Yes, all Entity-Classes are marked "[DataContract(IsReference = true)]"
- Lazy-Loading is set to "false" in the edmx file
Edit:
I changed the WCF to be hosted in a console app and no longer in IIS. Of course then I had to write my own little test client. Funny enough, now everything's working. I of course have no idea why, but at least for my testing this is a solution...Have a look here. Basically you have to make the serializer aware of cycles in the navigation properties.
精彩评论