Reputation: 21
I have a WCF server application using Entity Framework 5.0 and POCO entities. The server serializes POCOs directly to the client application over WCF (I managed to do this by following instructions in the following blog post: http://www.sanderstechnology.com/?p=10142). This has been working pretty well until I encountered a new problem today.
I have a simple situation in my data model shown in the following image: http://roponenp.kapsi.fi/images/datamodel.png
In my server I have a method that can be used for getting blocks:
[OperationContract]
[ApplyDataContractResolver]
[CyclicReferencesAware(true)]
List<Block> GetBlocks();
I need to include projects linked to the blocks to my response, so I do the following:
public List<Block> GetBlocks()
{
using (ModelContainer db = new ModelContainer())
{
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
return db.Block.Include(it => it.Project).ToList();
}
}
In my database I have instances of types ProjectA and ProjectB (that are inherited from Project). So the LINQ query above includes actually types of ProjectA and ProjectB to my response. Those types get serialized and are sent to the client. When this happens, I get the following error in the client side (there comes no errors in the server application):
The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.
The problem seems to be that in my data contract type Block is linked to type Project. However, my response returns data containing types ProjectA and ProjectB linked to Blocks.
I have no idea how I can solve this problem. I think it can be solved by implementing a custom DataContractResolver, but I haven't found any working examples how this could be done.
Any ideas?
Upvotes: 0
Views: 546
Reputation: 77364
You will need a KnownType Attribute if your object contains other types than obvious (for example interface implementations or derived classes).
Check the example in the MSDN, it's pretty good.
[ServiceContract]
[KnownType(typeof(ProjectA))]
[KnownType(typeof(ProjectB))]
class SomeService
{
[OperationContract]
[ApplyDataContractResolver]
[CyclicReferencesAware(true)]
List<Block> GetBlocks();
Upvotes: 1