3

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 did 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...

Sebastian
  • 31
  • 3
  • did you solve that problem? I have it now... please see: http://stackoverflow.com/questions/9150920/preventing-stackoverflowexception-while-serializing-ef-object-graph-into-json – Ofer Zelig Feb 05 '12 at 17:37

1 Answers1

0

Have a look here. Basically you have to make the serializer aware of cycles in the navigation properties.

Johann Blais
  • 9,389
  • 6
  • 45
  • 65
  • I've never had to do that. I experienced cyclic references / stack overflow errors only when serializing with JSON. I created my own json processor to use JSON.NET which supports cyclic references and object identities. For xml, use DataContractSerializer and set [DataContract(IsReference = true)] like stated in the post. – Daniel Mar 18 '11 at 03:35
  • Although it sound as if cyclic references would be the problem, I don't think the above article is still valid for EF4 and STE? Especially because of the fact that if WCF is hosted on console-app, everything is fine even without a custom attribute, I assume that this is a configuration problem? (pls. see my edit) – Sebastian Mar 21 '11 at 07:37