Wednesday, May 30, 2007

Connection Monitor Application Block

Configure the Connection Monitor Application Block
add a <section> node to the <configSections>
<configSections>
<section name="ConnectionMonitor" type="Microsoft.Practices.SmartClient.ConnectionMonitor.Configuration.ConnectionSettingsSection, Microsoft.Practices.SmartClient.ConnectionMonitor" />
</configSections>
To use the Connection manager Application Block,
Add configuration information to the application configuration file.
It defines the logical networks and physical network adapters that you want to monitor.
<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>

Instantiate the ConnectionMonitor service and store it in a WorkItem

Call the static CreateFromConfiguration method of the ConnectionMonitorFactory class.

// 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);

Add the ConnectionMonitor instance to the Services collection of your WorkItem
myWorkItem.Services.Add(sampleMonitor);
if (sampleMonitor.Connections.Contains("Internet"))
{
// Check the connection status.
Connection conn = sampleMonitor.Connections["Internet"];
if (!conn.IsConnected)
{
}

Get a reference to the Connection Monitor service from another class

  1. use a [ServiceDependency] attribute on a parameter of the class constructor to accept an injected reference
    public class MyNewClass
    {
    private ConnectionMonitor sampleMonitor;
    public MyNewClass([ServiceDependency] ConnectionMonitor cm)
    {
    this.sampleMonitor = cm;
    }
    ...
    }

  2. Expose a public property for the ConnectionMonitor
    public class MyNewClass
    {
    private ConnectionMonitor sampleMonitor;
    [ServiceDependency]
    public ConnectionMonitor ConnectionMonitor
    {
    get { return this.sampleMonitor; }
    set { this.sampleMonitor = value; }
    }
    ...
    }

  3. Query the Services collection of the WorkItem to obtain a reference to the service
    public class MyNewClass
    {
    private ConnectionMonitor sampleMonitor;
    public MyNewClass();
    {
    this.sampleMonitor = myWorkItem.Services.Get<ConnectionMonitor>();
    }
    ...
    }


Detect and handle changes to networks and connectivity

1. Connect your event handler to the StateChanged event of the Connection or Network
conn.StateChanged += StateChangeHandler;
netwk.StateChanged += StateChangeHandler;
2. Network and the Connection classes both raise a StateChanged event when their state changes.
public void StateChangeHandler(object sender, StateChangedEventArgs e)
{
if (e.IsConnected)
{ MessageBox.Show("Now connected"); }

}

3. 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.


Create a new connection instance
// Create a new NicConnection instance.
NicConnection conn = new NicConnection("NicConnection", 6);
Use the Add method of the ConnectionCollection class
// Add it to the ConnectionMonitor Connections collection.
sampleMonitor.Connections.Add(conn);
It can also use the other methods of the KeyedCollection and Collection classes to check for the presence of a specific connection
// See if a connection named Internet exists.
if (sampleMonitor.Connections.Contains("Internet"))
{
// Check the connection status.
Connection conn = sampleMonitor.Connections["Internet"];
if (!conn.IsConnected)
{
// Display the price and remove it from the Connections collection.
MessageBox.Show("Removing connection with price "
+ conn.Price.ToString());
sampleMonitor.Connections.Remove(conn);
}
}

No comments: