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

发布于 2025-01-04 07:55:28 字数 185 浏览 1 评论 0原文

我有一个 WPF 客户端应用程序通过对远程 WCF 服务的引用接收对象。 WCF 服务引用是通过 Visual Studio 的“添加服务引用...”生成的。

我想在每次从 WCF 服务接收/反序列化对象时执行一段代码。该对象需要已经被反序列化,以便我可以读取其属性/调用方法。该解决方案将是全局的,不需要我添加到每个 WCF 服务调用中。

I have a WPF client application receiving objects via a reference to a remote WCF service. The WCF service references were generated via Visual Studio's 'Add Service Reference...'.

I would like to execute a piece of code each time an object received/deserialized from the WCF service. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2025-01-11 07:55:28

从迈克的最初反应开始,我想出了以下解决方案。

后台

  1. 客户端通过 WCF 服务从服务器提取数据。
  2. WCF 服务引用由 Visual Studio 使用“引用程序集中的重用类型”生成,因此无法执行 WCF 代理操作。
  3. 当从 WCF 接收的任何对象上的任何属性发生更改时,需要修改客户端应用程序上的属性(这些对象实现 INotifyPropertyChanged)

预警

我知道这会破坏一些面向对象和责任规则,但是该解决方案是如此简短、如此简单,并且适合我当前和预期的未来需求,所以这就是我所采用的。当每个客户端反序列化都需要发生重要逻辑时,此解决方案不太实用。

[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;
                                   };
        }
    }
}

按键

  1. 其起作用的原因是动态关键字。你必须使用
    动态关键字,因为包含DTO的项目无法引用
    UI项目由于循环引用。如果它无法引用 UI 项目,则编译器不知道 IsAnythingDirty 布尔值。
  2. 检查Application.Current是否为null可确保代码仅在客户端运行,而不是在服务器端运行。

Starting with Mike's initial response I was able to come up with the following solution.

Background

  1. Client pulls data from the server, over a WCF service.
  2. The WCF service reference is generated by Visual Studio with "reuse types in referenced assemblies" so no WCF proxy manipulation can be done.
  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)

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. You have to use the
    dynamic keyword because the project containing the DTO cannot reference the
    UI project due to a circular reference. If it can't reference the UI project the compiler does not know about the IsAnythingDirty boolean.
  2. Checking whether Application.Current is null ensures that the code will only run on the client-side, not the server-side.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文