We have a WCF service that uses Entity Framework to query a SQL database. The WCF service is our data access layer. If any of our applications what to read/write data to and from the database, we call a method on the WCF service. The WCF service serializes the EntityObjects to the client apps.
In the client app (e.g. WPF app or ASP.NET app) we may update an entity property and call a WCF service, passing the entity as a parameter, to perform the database update. The entity that was updated may have hundreds of child entities related to it.
For example, a Customer entity will have a Projects property which is a list of Project entities. If our client app changes a simple scalar property on a Customer entity, like CustomerName, we need to save that change to the database by calling an update method on the WCF service. The issue is, we are passing the Customer entity to the service to be updated, but all the attached Projects are also being serialized and passed to the WCF service even though the WCF service update method is only concerned with changes to scalar properties on the Customer entity. Obviously, we are serializing and transferring much more data than is needed.
Has anyone had any experience with this issue? Somehow, I would like to only have the Customer entity serialized back to the WCF service, without doing a deep serialization of the entire object graph.
Thanks.
You could use the maxItemsInObjectGraph property of the DataContractSerializer object to restrict the serialization. You can easily try it by using the configuration element in the behavior configuration. For instance
One other option I can think of is to create a DataContract Surrogate that removes the info you don’t want serialized. To do so you should create a class that implements the IDataContractSurrogate and use the GetObjectToSerialize method to modify the Customer Entity to not include any of the related entities collections.
This approach really bothers me for a number of reasons
So I would definitely try to use
maxItemsInObjectGraphfirstHTH