Question

I am using WCF data services (5.6 now) and since Enums are not supported (and for other reasons), I have some additional properties added to the client side classes that I intend to remove during SaveChanges using the WritingEntity event following the example in http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data-services-client-library.aspx

My constructor attaches the event but I find that sometimes the event fires and other times (more often) it doesn't.

public MyDataContext(System.Uri serviceRoot, bool ignoreProperties)
    : this(serviceRoot)
{
    if (ignoreProperties)
        this.WritingEntity += EdiContext_WritingEntity;
    this.SendingRequest2+=OnSendingRequest;
}

To Save changes

db.AttachTo("Maps", map, "*");
db.UpdateObject(map);
ProcessMapCoordinates(db, map);
ProcessModifiers(map, db);
db.SaveChanges();

The SendingRequest2 event does fire, I use it to attach some header information to the request in order to support multiple data

private void OnSendingRequest(object sender, SendingRequest2EventArgs e)
{
    e.RequestMessage.SetHeader("profile", ClientSettings.Instance.Profile);
}

Does anyone know under what circumstances the WritingEntity event will not fire?

Is there another way to prevent extended properties from the partial class from being serialized?

Thanks

Was it helpful?

Solution

It appears this was caused by the use of public Enums on the client side partial class. Once I changed the access modifier of the enum to internal the problem went away.

In the process I learned an even better way of controlling what properties are serialized, by hooking into the RequestPipeline events:

if (ignoreProperties)
{
    this.Configurations.RequestPipeline.OnEntryStarting((a =>
    {
        entityType = Type.GetType(a.Entry.TypeName);
        if (entityType != null)
        {
            var props =
                entityType.GetProperties()
                    .Where(
                        property =>
                            property.GetCustomAttributes(typeof (DoNotSerializeAttribute), false).Length > 0)
                    .Select(p => p.Name)
                    .ToArray();
            a.Entry.RemoveProperties(props);
        }
    }));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top