WCF数据服务SaveChanges未触发WritingEntity事件
本文关键字:WritingEntity 事件 数据 服务 SaveChanges WCF | 更新日期: 2023-09-27 17:52:33
我正在使用WCF数据服务(现在是5.6),由于不支持枚举(以及其他原因),我在客户端类中添加了一些额外的属性,我打算在SaveChanges期间使用WritingEntity事件删除这些属性,示例如下http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data-services-client-library.aspx
我的构造函数附加了事件,但我发现有时事件会触发,而其他时候(更经常)它不会。
public MyDataContext(System.Uri serviceRoot, bool ignoreProperties)
: this(serviceRoot)
{
if (ignoreProperties)
this.WritingEntity += EdiContext_WritingEntity;
this.SendingRequest2+=OnSendingRequest;
}
保存更改
db.AttachTo("Maps", map, "*");
db.UpdateObject(map);
ProcessMapCoordinates(db, map);
ProcessModifiers(map, db);
db.SaveChanges();
SendingRequest2事件触发,我用它来附加一些头信息的请求,以支持多个数据
private void OnSendingRequest(object sender, SendingRequest2EventArgs e)
{
e.RequestMessage.SetHeader("profile", ClientSettings.Instance.Profile);
}
有没有人知道在什么情况下WritingEntity事件不会触发?
是否有另一种方法来防止从部分类被序列化的扩展属性?
谢谢
这似乎是由于在客户端部分类上使用公共枚举引起的。一旦我将枚举的访问修饰符改为internal,问题就解决了。
在这个过程中,我学会了一种更好的方法来控制哪些属性被序列化,通过挂钩到RequestPipeline事件: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);
}
}));
}