Question

I'd like to be able to return an IObservable from a WCF call, either directly or from a callback. The reason for this is so that I can receive an enumerable that I will subscribe to and be able to process the results one at a time.

I could easily use a callback to just get push notification of the results one at a time, but then I would have no knowledge of when the series ends, and not be able to unsubscribe.

The obvious reason I can't return an IObservable would seem to be because IObservable isn't serializable. Is there something I haven't thought of that will get this to work?

Was it helpful?

Solution

An IObservable pushes results at you. You can't tell when an IObservable will end, only that it has. This is their nature.

There is no reason for you to serialize the interface itself.

I'm a bit confused about what you are expecting this method to return - you mention you are expecting an enumerable (i.e. IEnumerable?) making this a request/response call with a single result. If this is the case, you can use Observable.FromAsyncPattern to easily create a IObservable for the result.

If, however, the results will be pushed individually from the Server over a Stream or via a Callback, then you can just create a wrapper via Observable.Create that initiates the connection/request on Subscribe and pushes results out via OnNext as they come back, and have the Dispose close the stream etc.

Essentially either way, you are creating a proxy on the client side that exposes a regular WCF client through a locally created IObservable interface. This is pretty much how all IObservable services work.

If on the server side you have an IObservable to return, you'll need to do the reverse there - translate a regular client side call into a subscription against the IObservable from which you send results back to the client via regular WCF Streams or Callbacks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top