📜 ⬆️ ⬇️

C # - Using Network Load Balancing Events

At work, I constantly encounter the lack of MSDN resources.
That is, even if the general description is pretty detailed, there is almost no opportunity to use class / technology due to the lack of examples. It takes quite a long time (sometimes for hours) to search for various resources on the Internet in order to find a reasonable and transparent way to use it.

One of the latest examples I had was using the Microsoft Network Load Balance (what is it you can see here ) inside the C # code. There are many examples of using NLB from VB scripts and almost no examples of using from managed code.

Task : determination of the status of the machine (cluster) at a given time.
The existing solution : Polling - determining status by querying via WMI.
Disadvantage : there is a high probability to skip the state change at the moment of the event itself, the lack of information, the time spent on reading the state.
A possible approach : the use of the mechanism of "events". Such a mechanism exists, even with examples, but not for use within code.
Solution :
')
Announcement in the main method:
" String mePath = "\\\\localhost\\root\\MicrosoftNLB" ;
ManagementEventWatcher watcher1 = new ManagementEventWatcher(mePath, "SELECT * FROM MicrosoftNLB_NodeControlEvent" );
watcher1.EventArrived += new EventArrivedEventHandler(watcher_NodeControlEventArrived);
watcher1.Options.Timeout = new TimeSpan (0, 0, 1);
watcher1.Start(); //need Stop!!!!!!!


* This source code was highlighted with Source Code Highlighter .
String mePath = "\\\\localhost\\root\\MicrosoftNLB" ;
ManagementEventWatcher watcher1 = new ManagementEventWatcher(mePath, "SELECT * FROM MicrosoftNLB_NodeControlEvent" );
watcher1.EventArrived += new EventArrivedEventHandler(watcher_NodeControlEventArrived);
watcher1.Options.Timeout = new TimeSpan (0, 0, 1);
watcher1.Start(); //need Stop!!!!!!!


* This source code was highlighted with Source Code Highlighter .

"

Description callback to receive event notification:
" static void watcher_NodeControlEventArrived( object sender, EventArrivedEventArgs e)
{
//http://msdn.microsoft.com/en-us/library/bb736304(VS.85).aspx
ManagementEventWatcher watcher = (ManagementEventWatcher)sender;
Console .WriteLine(watcher.Query.QueryString.ToString());
PropertyData property;
if ((property = e.NewEvent.Properties[ "Id" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "InstanceName" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "Active" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "AdapterGuid" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "ClusterIPAddress" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "HostPriority" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
}


* This source code was highlighted with Source Code Highlighter .
static void watcher_NodeControlEventArrived( object sender, EventArrivedEventArgs e)
{
//http://msdn.microsoft.com/en-us/library/bb736304(VS.85).aspx
ManagementEventWatcher watcher = (ManagementEventWatcher)sender;
Console .WriteLine(watcher.Query.QueryString.ToString());
PropertyData property;
if ((property = e.NewEvent.Properties[ "Id" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "InstanceName" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "Active" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "AdapterGuid" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "ClusterIPAddress" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
if ((property = e.NewEvent.Properties[ "HostPriority" ]) != null )
{
Console .WriteLine(property.Name + ": " + property.Value);
}
}


* This source code was highlighted with Source Code Highlighter .

"

Comments :
Instead of “ MicrosoftNLB_NodeControlEvent ”, you can receive other events — a full list here: “ Network Load Balancing WMI Classes ” and read the property from the event structures accordingly.

P.S. This is my "attempt at writing." I hope to get feedback on the utility and design, or vice versa - about the inappropriateness of such posts on Habré. Thank.

Source: https://habr.com/ru/post/88345/


All Articles