[英]WCF Client-side object deserialize notification
I have a WPF client application receiving objects via a reference to a remote WCF service. 我有一个WPF客户端应用程序,它通过对远程WCF服务的引用来接收对象。 The WCF service references were generated via Visual Studio's 'Add Service Reference...'. WCF服务引用是通过Visual Studio的“添加服务引用...”生成的。
I would like to execute a piece of code each time an object received/deserialized from the WCF service. 每当从WCF服务接收/反序列化对象时,我都想执行一段代码。 The object needs to already be deserialized so I can read properties/call methods on it. 该对象需要已经反序列化,所以我可以读取它的属性/调用方法。 This solution would be global and not something I need to add to every WCF service call. 此解决方案将是全局的,而不是我需要添加到每个WCF服务调用中的解决方案。
Starting with Mike's initial response I was able to come up with the following solution. 从Mike的最初回应开始,我提出了以下解决方案。
Background 背景
Forewarning 预警
I understand that this breaks some object oriented and responsibility rules, however the solution is so short, so easy, and fits my current and anticipated future needs so it is what I went with. 我知道这违反了一些面向对象和责任的规则,但是解决方案非常简短,容易,并且可以满足我当前和预期的未来需求,这正是我所追求的。 This solution is less practical when there is significant logic that needs to occur on each client-side deserialization. 当在每个客户端反序列化上都需要发生重要的逻辑时,此解决方案不太实用。
[DataContract]
public class DataTransferObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[OnDeserialized]
public void OnDeserialized(StreamingContext context)
{
dynamic app = Application.Current;
if(app != null) //Prevents execution on server-side. This code is meant to only execute at the client
{
PropertyChanged += (sender, args) =>
{
app.IsAnythingDirty = true;
};
}
}
}
The keys 按键
You could use the OnDeserialized attribute: 您可以使用OnDeserialized属性:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.