Silverlight 异步设计模式问题

标签 silverlight design-patterns asynchronous

我正在 Silverlight 应用程序中间,我有一个函数需要调用网络服务并使用结果完成函数的其余部分。

我的问题是,我通常会进行同步 Web 服务调用以获取结果并使用该函数继续执行该操作。由于 Silverlight 不支持同步 Web 服务调用而无需额外的自定义类来模拟它,因此我认为最好顺应异步流程而不是对抗它。所以我的问题涉及在程序流中使用异步调用的最佳设计模式是什么。

在下面的示例中,我想根据 Web 服务调用的返回值使用 myFunction TypeId 参数。但在调用此函数之前,我不想调用 Web 服务。如何更改我的代码设计以允许异步调用?

        string _myPath;

    bool myFunction(Guid TypeId)
    {
        WS_WebService1.WS_WebService1SoapClient proxy = new WS_WebService1.WS_WebService1SoapClient();
        proxy.GetPathByTypeIdCompleted += new System.EventHandler<WS_WebService1.GetPathByTypeIdCompleted>(proxy_GetPathByTypeIdCompleted);
        proxy.GetPathByTypeIdAsync(TypeId);

        // Get return value

        if (myPath == "\\Server1")
        {
            //Use the TypeId parameter in here
        }
    }

    void proxy_GetPathByTypeIdCompleted(object sender, WS_WebService1.GetPathByTypeIdCompletedEventArgs e)
    {
        string server = e.Result.Server;
        myPath = '\\' + server;
    }

提前致谢, 迈克

最佳答案

最好是使用 Reactive Extensions .然后(假设您要在 IObservable<string> GetPathByTypeId(string typeId) 上创建扩展方法 WS_WebService1SoapClient,您可以这样做:

proxy
    .GetPathByTypeId(TypeId)
    .Subscribe(server => 
        { 
            //Here you can do stuff with the returned value
        });

尽可能接近同步调用:)

关于Silverlight 异步设计模式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2763903/

相关文章:

ios - 具有异步网络请求的 ReactiveCocoa 排序

javascript - 中介模式与直接调用中间函数

ios - 在 iOS 中完成调度组操作后延迟执行方法

asp.net - 在MVC路由中使用 “async”(即使应该完成)也会使路由死锁;如何避免这种情况?

c# - 包含 silverlight 控件的母版页

c++ - 如何解决同时设置成员的 setter/getter ?

Silverlight RIA 服务 - 如何最好地处理客户端身份验证 session 超时?

node.js - 如何将不同请求的响应与nodejs和请求放在同一个文档中?

Silverlight 运行时错误 1001

银光性能测试