Reputation: 6953
I have been struggling to figure out how to use Rx. Most of the examples are out of date, reference Begin/End or are long and complex.
I have a simple WCF service method that takes an int and returns a JobMaster object.
Here is how I call it at the moment:
public static void GetJob(int jobId)
{
KernServiceClient.GetJobCompleted += GetJobCompleted;
KernServiceClient.GetJobAsync(jobId);
}
private static void GetJobCompleted(object sender, GetJobCompletedEventArgs e)
{
// JobMaster available in e.Result
}
How do I change this to use Rx?
EDIT
Thanks to Paul's help I have got most of the way there. This is what it looks like now. Only problem is the Subscribe never fires. Any ideas?
public static JobMaster GetJob(int jobId)
{
JobMaster retval = null;
IKernService kernServiceInterface = KernServiceClient;
var getJobFunc = Observable.FromAsyncPattern<int, Server.KernMobileWcfService.JobMaster>(
kernServiceInterface.BeginGetJob, kernServiceInterface.EndGetJob);
var result = getJobFunc(jobId);
result
.Subscribe
(
onNext: x => retval = ConvertJobMaster(x),
onError: ex => ShowError(ex.Message)
);
return retval;
}
Upvotes: 0
Views: 649
Reputation: 1
what are you doing with retval when you return it? if you need to do more processing when on next completes do it in the onCompleted event of the subscription
Upvotes: 0
Reputation: 1
Seems you're returning "retval" even though you're computing it asynchronously and assigning it from your OnNext handler. Your logic upon receiving the value from the service should be moved into the OnNext handler, or you should return the IObservable to the caller.
Upvotes: 0
Reputation: 74652
http://blog.paulbetts.org/index.php/2010/09/26/calling-web-services-in-silverlight-using-reactivexaml/ // Ignore the ReactiveXaml part
Summary: Cast KernServiceClient to the Interface it implements to get back the Begin/End methods, use FromAsyncPattern.
Upvotes: 1