使用Rx简化异步Silverlight Web服务请求

使用Rx简化异步Silverlight Web服务请求,第1张

概述我使用Rx为我的WCF Web服务编写了一个简化的Silverlight客户端库,但是有时我注意到我缺少已完成的事件. public IObservable<XElement> GetReport(string reportName){ return from client in Observable.Return(new WebServiceClient()) f 我使用Rx为我的WCF Web服务编写了一个简化的Silverlight客户端库,但是有时我注意到我缺少已完成的事件.

public IObservable<XElement> GetReport(string reportname){    return from clIEnt in Observable.Return(new WebServiceClIEnt())           from request in Observable.ToAsync<string>(clIEnt.GetReportDataAsync)(reportname)           from result in Observable.FromEvent<GetReportDataCompletedEventArgs>(clIEnt,"GetReportDataCompleted").Take(1)           from close in this.CloseClIEnt(clIEnt)           select result.EventArgs.Result;}

我认为问题是由于Web服务被调用并在订阅已完成的事件之前返回.我无法弄清楚如何让Rx在异步调用之前订阅该事件.我试过StartWith,但这需要输入和输出类型相同,任何想法?

解决方法 似乎最好的答案是使用Observable.CreateWithdisposable()

例如

public IObservable<XElement> GetReport(string reportname){    return from clIEnt in Observable.Return(new WebServiceClIEnt())            from completed in Observable.CreateWithdisposable<GetReportDataCompletedEventArgs>(observer =>                {                    var subscription = Observable.FromEvent<GetReportDataCompletedEventArgs>(clIEnt,"GetReportDataCompleted")                        .Take(1)                        .Select(e => e.EventArgs)                        .Subscribe(observer);                    clIEnt.GetReportDataAsync(reportname);                    return subscription;                })            from close in this.CloseClIEnt(clIEnt)            select completed.Result;}

为了使这更容易使用,我将CreateWithdisposable重构为一个可以与我的所有Web服务调用一起使用的公共函数,包括从事件args类型自动确定事件名称:

private IObservable<T> CallService<T>(ICommunicationObject serviceClIEnt,Action start) where T : AsyncCompletedEventArgs{    if (typeof(T) == typeof(AsyncCompletedEventArgs))    {        throw new InvalIDOperationException("Event arguments type cannot be used to determine event name,use event name overload instead.");    }    string completedEventname = typeof(T).name.TrimEnd("EventArgs");    return CallService<T>(serviceClIEnt,start,completedEventname);}private IObservable<T> CallService<T>(ICommunicationObject serviceClIEnt,Action start,string completedEventname) where T : AsyncCompletedEventArgs{    return Observable.CreateWithdisposable<T>(observer =>    {        var subscription = Observable.FromEvent<T>(serviceClIEnt,completedEventname).Take(1).Select(e => e.EventArgs).Subscribe(observer);        start();        return subscription;    });}// Example usage:public IObservable<XElement> GetReport(string reportname){    return from clIEnt in Observable.Return(new WebServiceClIEnt())            from completed in this.CallService<GetReportDataCompletedEventArgs>(clIEnt,() => clIEnt.GetReportDataAsync(reportname))            from close in this.CloseClIEnt(clIEnt)            select completed.Result;}/// <summary>/// Asynchronously closes the web service clIEnt/// </summary>/// <param name="clIEnt">The web service clIEnt to be closed.</param>/// <returns>Returns a cold observable sequence of a single success Unit.</returns>private IObservable<AsyncCompletedEventArgs> CloseClIEnt(WebServiceClIEnt clIEnt){    return this.CallService<AsyncCompletedEventArgs>(clIEnt,clIEnt.CloseAsync,"CloseCompleted");}

希望这有助于其他人!

总结

以上是内存溢出为你收集整理的使用Rx简化异步Silverlight Web服务请求全部内容,希望文章能够帮你解决使用Rx简化异步Silverlight Web服务请求所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004794.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存