This project has moved. For the latest updates, please go here.

Networking

.NET 4.0 Silverlight 4 Silverlight 5 Phone 7.0 Phone 7.1
S.png P.png P.png P.png P.png

Overview

Acquiring and serving data over a network are common requirements in today's applications running on a server, desktop or device. To ensure the best possible performance and user experience in our applications, we invoke and serve web requests asynchronously.

Clients

Rxx provides several APIs that make it easy to invoke web requests asynchronously, including System.Net.ObservableWebClient, System.Net.Sockets.ObservableSocket, System.Net.Mail.ObservableSmtpClient, System.Net.ObservableDns, System.Net.NetworkInformation.ObservablePing, System.Net.NetworkInformation.ObservableNewtworkChange and System.ServiceModel.Syndication.ObservableSyndication. Many of these APIs are wrappers around native asynchronous .NET Framework APIs. Their purpose is to simplify and convert various asynchronous patterns into observable sequences, for your convenience. These conversions are also exposed as extension methods, many of which include additional overloaded extensions to provide more control when needed. For instance, Rxx offers extension methods for System.Net.WebRequest, System.Net.HttpWebRequest, System.Net.FtpWebRequest, System.Net.WebClient, System.Net.Sockets.Socket, System.Net.Sockets.TcpClient, System.Net.NetworkInformation.Ping, System.Net.Mail.SmtpClient, System.ServiceModel.Syndication.SyndicationFeedFormatter and System.Data.SqlClient.SqlCommand.

Given the unpredictable nature of web services and network connectivity, it's often required to implement some kind of exception handling routine and back-off algorithm in clients. Rxx provides various extension methods for IObservable<T>, including overloads to existing Rx extension methods, that make error-handling behaviors easy to implement. For instance, Catch, Retry and OnErrorResumeNext are overloaded to generate observables with out-of-band exception channels and parameters that allow you to specify custom back-off algorithms. See the OnError topic for details.

Web request scenarios often deal with different types of System.IO.Stream objects and file I/O. Rxx provides useful extensions that make it easy to read/write streams and files asynchronously. See the I/O Extensions topic for details.

Servers

To enable asynchronous services in server and desktop applications, Rxx provides several APIs that make it easy to receive and process requests asynchronously using observables, including System.Net.ObservableHttpListener, System.Net.Sockets.ObservableSocket and System.Net.Sockets.ObservableTcpListener. Like the client APIs above, most of these APIs are wrappers around native asynchronous .NET Framework APIs. They are also exposed as extension methods, many of which include additional overloaded extensions to provide more control when needed. For instance, Rxx offers extension methods for System.Net.HttpListener, System.Net.Sockets.Socket and System.Net.Sockets.TcpListener. Additionally, an extension method for IObservable<T> named System.Reactive.Linq.Observable2.Serve provides a way to generate observables in a loop and merge them into a concurrent observable sequence, up to a specified maximum concurrency.

Web service scenarios often deal with different types of System.IO.Stream objects and file I/O. Rxx provides useful extensions that make it easy to read/write streams and files asynchronously. See the I/O Extensions topic for details.

WCF

Rxx provides a behavior for Windows Communication Foundation (WCF) that allows service definitions to return IObservable<T> from any operation. Those operations that return an observable have the same asynchronous behavior as legacy async pattern operations, without requiring a pair of Begin* and End* methods. To use this new behavior in your WCF services, simply apply System.ServiceModel.Reactive.ObservableServiceAttribute to your service interface or class and define operations that return IObservable<T>. Optionally, you can also apply System.ServiceModel.Reactive.ObservableOperationAttribute to individual operations when you need more control; e.g., to indicate whether an observable is to be treated as a scalar value or a list. You may find the System.ServiceModel.ICommunicationObject extensions to be useful when defining duplex services to control the lifetime of observable callbacks based on the connection state of the client.

Platform Support

Client extensions are supported on the full .NET 4.0 platform, though some aren't supported in Silverlight and Phone applications simply because their .NET Framework profiles don't support them natively. Server extensions are only supported on the full .NET 4.0 platform, with the exception of the System.Reactive.Linq.Observable2.Serve extension method, which is supported on all platforms.

Lab

Download the labs application for your target platform and then run one of the labs in the Reactive / Networking category. These labs contain working examples of several of the APIs described above. The source code for each lab is displayed in the application, for your convenience.

For more information, see Rxx Hands-on Labs.

Last edited Mar 10, 2012 at 6:20 PM by davedev, version 2