Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Reactive Extensions for .NET 4.5

Reactive Extensions for .NET 4.5

This item in japanese

The new functionality in .NET 4.5 with it the opportunity to revisit the out of band libraries such as Reactive Extensions. Bart De Smet talks about what’s in the Rx experimental branch. Also mentioned is Rx for JavaScript and C++.

First up is Rx support for ExceptionDispatchInfo. In previous versions of .NET exceptions were expected to be caught on the same thread that they were thrown on. Libraries that needed to move them to another thread passed them as a field to a marshaled callback where they were then wrapped with a generic exception. This new exception would have to be unwound to see the real error underneath.

The ExceptionDispatchInfo.Capture method is used to capture an exception. It is then rethrown on the correct thread using ExceptionDispatchInfo.Throw. The stack trace includes frames from both the new and original location, with the text “End of stack trace from previous location where exception was thrown” separating the two.

When Reactive Extensions was created they could not take a dependency on Task<T>. The Task Parallel Library was simply not ready yet and even today many platforms such as Silverlight still don’t support the TPL. As a work-around the Rx libraries were instead built on top of .NET’s original async pattern. Instead observables were created via the Observable.FromAsyncPattern method.

With the async/await support in C# 5 and VB 11 being built on top of tasks, the various platforms are now intending to fully support the Task Parallel Library. This is allowing the Rx project to finally build observables directly from Task<T>.

In addition to the conversions between tasks and observables, Reactive Extensions will also directly support the await keyword. This will return the last item in the IObservable sequence. If one wants all of the items in the sequence then one can convert the IObservable<T> into an IObservable<List<T>> using ToList.

Another change is the replacement of the blocking operations such as Average and ForEach. For example, the new ForEachAsync method will return a Task that can be awaited upon.

RxJS, the JavaScript version of the library, will be seeing a new version based on the current production build of Rx. No timeline was given, but it was stated that they were in the polishing stage. The Reactive Extensions team also taken on a native developer to work on a C++ version tentatively known as RxC.

Rate this Article