使用 Rx 调用 WCF 服务方法 Async 导致闭包问题

发布于 2024-10-07 08:03:17 字数 783 浏览 0 评论 0原文

我目前正在使用此代码调用 Web 服务来获取应用程序页面的数据。 一切都很好,直到我尝试连续调用此方法 10 次而不等待第一次调用完成。

这样做会导致我遇到闭包问题,并且我的所有结果都得到相同的结果对象。

以前有人在 Rx.Net 中遇到过这个问题吗?如果是的话,有人有想法或线索,以便我可以解决这个问题。

    public void GetPage(long pageId, Action<PageDTO> observer)
    {

        Observable.FromEvent<GetPageCompletedEventArgs>(
            handler => Service.GetPageCompleted += handler,
            handler => Service.GetPageCompleted -= handler)
            .Select(eventHandler => eventHandler.EventArgs.Result)
            .Take(1) // necessary to ensure the observable unsubscribes
            .ObserveOnDispatcher() // controls which thread the observer runs on;
            .Subscribe(observer, HandleError);

        Service.GetPageAsync(pageId);
    }

I am currently using this code tu call a webservice to get data for an application page.
Everything is fine until I try to call this method 10 times in a row without waiting for the first call to finish.

Doing so is causing me to have a problem with closures and I get the same result object for all my results.

has anyone faced this with Rx.Net before? if so does anyone have an idea or a lead so that I may resolve this issue.

    public void GetPage(long pageId, Action<PageDTO> observer)
    {

        Observable.FromEvent<GetPageCompletedEventArgs>(
            handler => Service.GetPageCompleted += handler,
            handler => Service.GetPageCompleted -= handler)
            .Select(eventHandler => eventHandler.EventArgs.Result)
            .Take(1) // necessary to ensure the observable unsubscribes
            .ObserveOnDispatcher() // controls which thread the observer runs on;
            .Subscribe(observer, HandleError);

        Service.GetPageAsync(pageId);
    }

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

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

发布评论

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

评论(1

Service 总是同一个实例吗?如果是这样,您将遇到各种疯狂的情况,其中 GetPageCompleted 事件将由不同调用(具有不同参数)创建的 FromEvent 处理,这将解释为什么同时调用的所有方法的结果都相同。

您可以通过使用 Begin/End 方法来解决这个特定问题,但您仍然可能会遇到底层连接争用的问题。

public void GetPage(long pageId, Action<PageDTO> observer)
{
    Observable.FromAsyncPattern<long, PageDTO>(
            service.BeginGetAwards, service.EndGetAwards)(pageId)
        .ObserveOnDispatcher() // controls which thread the observer runs on;
        .Subscribe(observer, HandleError);
}

Is Service always the same instance? If so, you're going to run into all kinds of craziness whereby GetPageCompleted events will be handled by the FromEvent created by a different call (with different arguments), which would explain why your results are the same for all methods that were called at the same time.

You can get around this specific issue by using the Begin/End methods, though you will still likely run into problems with contention on the underlying connection.

public void GetPage(long pageId, Action<PageDTO> observer)
{
    Observable.FromAsyncPattern<long, PageDTO>(
            service.BeginGetAwards, service.EndGetAwards)(pageId)
        .ObserveOnDispatcher() // controls which thread the observer runs on;
        .Subscribe(observer, HandleError);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文