francezu13k50 francezu13k50 - 2 years ago 484 Question

Await Observable in Net 4.0 with Microsoft.Bcl.Async

I am using Microsoft Visual Studio Comunity 2015 targeting Net 4.0 . I have installed via Nuget the followings: Rx-Main 2.2.5, Microsoft.Bcl.Async 1.0.168. I need to await an Observable, but when i write

Await Observable.Return(42)

i get a compiler error :

"BC36930 'Await' requires that the type 'IObservable(of Integer)' have a suitable GetAwaiter method."

Note that i am able to await a
, as in
Await TaskEx.FromResult(42)
, but not an Observable. I've used Object Browser to inspect Rx Library, but there is no GetAwaiter extension method, while Github repo does contain it in System.Reactive.Linq.Observable class. Is there a way to resolve this, or should i stick to
Await Observable.Return(42).ToTask

Answer Source

Unfortunately the implementation of GetAwaiter and INotifyCompletion is only compiled into Rx when you target .NET 4.5 and above. Check out this example in the source code.

This being the case, you want to use ToTask() to convert the observable into a task or by using one of the XXXAsync methods provided, FirstAsync() for example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download