简体   繁体   English

WCF客户端对象反序列化通知

[英]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 背景

  1. Client pulls data from the server, over a WCF service. 客户端通过WCF服务从服务器提取数据。
  2. The WCF service reference is generated by Visual Studio with "reuse types in referenced assemblies" so no WCF proxy manipulation can be done. WCF服务引用由Visual Studio使用“引用程序集中的重用类型”生成,因此无法完成WCF代理操作。
  3. A property on the Client side app needs to be modified when any property, on any of the objects received from WCF has changed (these objects implement INotifyPropertyChanged) 当从WCF接收的任何对象的任何属性发生更改时,都需要修改客户端应用程序的属性(这些对象实现INotifyPropertyChanged)

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 按键

  1. The reason this works is the dynamic keyword. 起作用的原因是dynamic关键字。 You have to use the dynamic keyword because the project containing the DTO cannot reference the UI project due to a circular reference. 您必须使用dynamic关键字,因为包含DTO的项目由于循环引用而无法引用UI项目。 If it can't reference the UI project the compiler does not know about the IsAnythingDirty boolean. 如果无法引用UI项目,则编译器将不知道IsAnythingDirty布尔值。
  2. Checking whether Application.Current is null ensures that the code will only run on the client-side, not the server-side. 检查Application.Current是否为null可确保代码仅在客户端运行,而不在服务器端运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM