locked
WCF service is failing for Operations returning Entity classes RRS feed

  • Question

  • User-758219241 posted

    My database is having following table

    Country >> CountryId, Country

    Following is the exact architecture in my application.

    Geography_EF >> Entity framework project (using EF 5) Geography_Model >> Model generated from EF (in short, entity classes). This contains Country class. Geography_WCF >> WCF application containing various service contracts.

    The issue is that when I write an operation contract returning Country object (or collection) it gives me an error saying "The underlying connection is closed. "The underlying connection was closed: The connection was closed unexpectedly." Here, I can see the call gets completed to this operation contract and immediately this exception occurs.

    whereas if I write an operation to return a specific field only (i.e. CountryId or Country table) then it works fine.

    I am using repository pattern and Unity container as well.

    What do you think, I am missing?

    Thursday, January 24, 2013 1:10 PM

Answers

  • User-1000095884 posted

    Hi,

    The issue may caused by WCF was unable to serialize the circular reference that Entity Framework created when loading the referenced entities, try put DataContract(IsReference = true)] above your model class. For details refer:

    <!--EndFragment-->http://www.binaryforge-software.com/wpblog/?p=129

    If the issue still exists, suggest you enable tracing for your service to help to dignose the actual issue.

    Best Regards.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 29, 2013 4:57 AM

All replies

  • User-1000095884 posted

    Hi,

    The issue may caused by WCF was unable to serialize the circular reference that Entity Framework created when loading the referenced entities, try put DataContract(IsReference = true)] above your model class. For details refer:

    <!--EndFragment-->http://www.binaryforge-software.com/wpblog/?p=129

    If the issue still exists, suggest you enable tracing for your service to help to dignose the actual issue.

    Best Regards.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 29, 2013 4:57 AM
  • User614805505 posted

    Hi,

    You have to detach the returning entity object from context before sending via WCF.

    http://msdn.microsoft.com/en-us/library/system.servicemodel.domainservices.client.entityset.detach(v=vs.91).aspx

    Tuesday, January 29, 2013 10:13 AM