HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Windows service for monitoring network interface changes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
monitoringinterfaceservicechangesforwindowsnetwork

Problem

I'm currently working on several projects for my company to help reduce the amount of calls we have to deal with so we can focus on higher priority task, such as server resource reduction, etc.

The first project on my list was to reduce the number of proxy issues a user has when migrating from an internal network to an external network such as home Internet.

I have come up with a Windows service idea where the service would be deployed from our servers and installed via a command. Once installed and started the service would monitor the connection states of all the network interface on the computer.

Upon a network interface change such as:

  • Ethernet plugged out



  • IP changes



  • Interfaces enabled / disabled



  • etc



the service would attempt to ping our DomainController. If the ping is a success, we will then check to see if the proxy settings for the machine are set. If not, we will automatically set them and enable them. If the ping is unsuccessful, we will disable the proxy.

The project is not fully complete but there is still a nice bit of code there to review.

ProxyMonitor.cs

```
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Reflection;
using System.Threading;
using System.Net.NetworkInformation;

namespace Serco.Services.ProxyMonitor
{
class ProxyMonitor : ServiceBase
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
if (System.Environment.UserInteractive)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "/install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "/uninstall":
ManagedInstallerClass.InstallHelper(ne

Solution

Just a few points that came to mind as I was reading your code.

-
It appears to me that the main thread terminates once the NetworkChange.NetworkAddressChanged event is hooked, in which case the thread is redundant as the event will be raised by another thread anyway.

-
MainShutdownEvent is created and 'set' but I don't see anything actually using it.

-
NetworkChange.NetworkAddressChanged is hooked but not unhooked, it won't matter much when the appdomain is unloaded on termination but I would just feel better if it was part of the standard 'shutdown' process in OnStop()

-
I don't like the empty UnhandledException event handler, if you are going to hook it I would suggest some form of logging. At a minimum you should use log such events to the event log via the EventLog property on ServiceBase.

-
I'm not sure which thread will be used to rais the NetworkChange.NetworkAddressChanged, but I wouldn't rely on it being the same one as OnStop() so you should probably look at some form of locking/signaling to handle any situations where OnStop() is called while the event handler is running. It might not cause any real issues but I personally would want to play it safe. It might be interesting to temporarily add a long sleep into the event handler just to see what happens if it's busy when told to stop.

Context

StackExchange Code Review Q#536, answer score: 8

Revisions (0)

No revisions yet.