Anyone doing Windows Mobile development should be aware of the fantastic work the team over at OpenNETCF have been doing with the Smart Device Framework. This has provided missing functionality for both v1 and v2 of the .NET Compact Framework. Recently they also released a series of application blocks that made application development much easier.
Now the Patterns and Practices team at Microsoft has come to the table and have started to work with the community to improve support for writing enterprise grade mobile applications. The project, entitled the Mobile Client Software Factory, is made up of a series of application blocks, a testing framework and the mobile composity UI block. Up to their 5th community drop, this project is progressing nicely and I would recommend that you take a look.
We will use a simple example to illustrate how the connection manager application block can be used.
Dim configuration As String = “<Connections>” & _
” <Connection Type=’CellConnection’ Price=’8’/> ” & _
” <Connection Type=’NicConnection’ Price=’2’/> ” & _
” <Connection Type=’DesktopConnection’ Price=’1’/> ” & _
Dim factory As IConnectionManagerFactory = New ConnMgrApiFactory(configuration)
Dim conManager As ConnectionManager = factory.Create()
AddHandler conManager.ActiveNetworkChanged, AddressOf ActiveChanged
In this example we use a connection string (typically stored in a configuration file) which defines a series of connection types with their associated price. One of the problems that is encountered when writing a occasionally connected application is determining when a web request can be made. For example if the device is connected via a GPRS connection you would only want to make essential request, queuing less urgent and larger requests until a cheaper connection is made available. The solution to this problem is very similar to the postal system where you have to purchase sufficient stamps in order to send a package of a given size a certain distance. In the case of connections each connection type has an associated price. When making web requests you allocate them a number of stamps. If the number of stamps is >= the price of the active network the request will be made, otherwise it will be queued until a cheaper network is available.
The XML connection string is passed into the constructor for the ConnMgrApiFactory which implements the IConnectionManagerFactory interface. The Create method on this interface returns a ConnectionManager that contains a list of networks and connections that are defined for the mobile device.
In addition to exposing the list of networks and connection, the ConnectionManager also exposes the ActiveNetworkChanged event. We will see later that this event is used by the DisconnectedService application block to automatically invoke queued web requests when a connection with an appropriate price becomes active.
The following diagram illustrates the interfaces and the classes that are currently included in the Connection Manager application block.
The main class that you will need to interact with is the ConnectionManager class. When the active network event is raised you can access the active network via the ActiveNetwork property. The Network exposes properties such as IsConnected and a PingComponent that can be used to call DoPing to invoke a ping (this can be used to determine if a certain endpoint can be connected to).