Friday, August 31, 2007

The Connection Monitor Application Block

The Connection Monitor Application Block provides the following features

Network connectivity - Network status changes are detected when the connectivity status of physical network adapters changes.
Connectionmonitoring - this feature provides an indication of the current status of physical network adapters.
Pricing - Applications can choose whether to use a network, depending on the current connection's price.

you can use the Connection Monitoring Application Block to detect changes in connectivity to networks. There are two types of networks

1. Logical networks
2. Physical networks

The following are examples of logical networks
1. The internet
2. A corporate or home network
3. A virtual private network
A logical network is a collection of network addresses for a set of resources required by an application.
A physical network adapters include wired and wireless adapters.

Network Class - represents a logical network and exposes the network name and address.
NicConnection Class - provides the capability to detect if the application has a connection to a network.
DesktopConnection Class - this represents a local connection.


Configuring the Connection Monitor Application Block

Configuration information defines the logical networks and physical network adapters that you want to monitor.
with the following configuration section example, the application block will monitor connection to two logical networks and one physical network adapter.
<connectionmonitor>
<networks>
<add name="Intranet" address="http://contoso">
<add name="Internet" address="http://www.microsoft.com">
</networks>
<connections>
<add type="WiredConnection" price="1">
</connections>
</connectionmonitor>

Configure the Connection Monitor Application block

<configsections>
<section name="ConnectionMonitor" type="Microsoft.Practices.SmartClient.ConnectionMonitor.Configuration.ConnectionSettingsSection, Microsoft.Practices.SmartClient.ConnectionMonitor">
</configsections>


Creating a Connection Monitor Instance

Call the static CreateFromConfiguration method of the ConnectionMonitorFactory class. You can specify the name of the section in the application's configuration file that contains the definitions of the connections and networks configured for the application, or you can omit this parameter to use the default section name "ConnectionMonitor." This returns the single instance of the ConnectionMonitor service containing details of all the configured connections and networks for the application.

// Use the default section name "ConnectionMonitor."
ConnectionMonitor sampleMonitor = ConnectionMonitorFactory.CreateFromConfiguration();

// Instead, you can specify the section name in the configuration file.
String configurationName = "SampleConnectionSection";
ConnectionMonitor sampleMonitor = ConnectionMonitorFactory.CreateFromConfiguration(configurationName);


If you are building a Composite UI Application Block application, you can add the ConnectionMonitor instance to the Services collection of your WorkItem so that it is available by using ObjectBuilder injection techniques in any other class within the scope of this WorkItem. In general, you should add it to the root WorkItem of your application.

myWorkItem.Services.Add(sampleMonitor);

Referencing the Connection Monitor Service

1. If you have stored an instance of the ConnectionMonitor class in your WorkItem.
You can use a [ServiceDependency] attribute

public class MyNewClass
{
private ConnectionMonitor sampleMonitor;

public MyNewClass([ServiceDependency] ConnectionMonitor cm)
{
this.sampleMonitor = cm;
}
...
}


2. Alternatively, you can expose a public property for the ConnectionMonitor and have ObjectBuilder set it to the ConnectionMonitor instance in the WorkItem.

public class MyNewClass
{
private ConnectionMonitor sampleMonitor;

[ServiceDependency]
public ConnectionMonitor ConnectionMonitor
{
get { return this.sampleMonitor; }
set { this.sampleMonitor = value; }
}
...
}


3. Another approach is to directly query the Services collection of the WorkItem to obtain a reference to the service you want.

public class MyNewClass
{
private ConnectionMonitor sampleMonitor;

public MyNewClass();
{
this.sampleMonitor = myWorkItem.Services.Get();
}
...
}



Handling Connectivity Change Events

The Network and Connection classes both raise a StateChange event when their state changes.

public void StateChangeHandler(object sender, StateChangedEventArgs e)
{
if (e.IsConnected)
{
MessageBox.Show("Now connected");
}
else
{
MessageBox.Show("Now disconnected");
}
}


Connect your event handler to the StateChanged event of the Connection or Network you want to monitor.


conn.StateChanged += StateChangeHandler;
netwk.StateChanged += StateChangeHandler;


The Connection class exposes a method named UpdateStatus that you can call to raise the StateChanged event and obtain information through your event handler on the connectivity status.

conn.UpdateStatus();



Creating Connections and Networks

You can programmatically create connections and networks and add them to or remove them from the Connection Monitor Service.

1. To create a new connection instance, call he constructor of the appropriate concreate class and specify the name and the price for this connection.
// Create a new NicConnection instance.
NicConnection conn = new NicConnection("NicConnection", 6);

2. Use the Add method of the ConnectionCollection class to add the new connection instance.

// Add it to the ConnectionMonitor Connections collection.
sampleMonitor.Connections.Add(conn);

No comments: