C # - Using events in Microsoft Network Load Balancing

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

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

    Task : determining the status of the machine (cluster) at a given time.
    Existing solution: Polling - determining status by querying through WMI.
    Disadvantage : there is a high probability of missing a state change at the time of the event itself, a lack of information, the time taken to read the state.
    Possible approach : application of the mechanism of "events". Such a mechanism exists, even with examples, but not for use inside the code.
    Solution :

    Announcement in the main method:
    " " Description of the callback to receive an event notification:
    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.




    “ ” Comments : Instead of “ MicrosoftNLB_NodeControlEvent ” you can receive other events as well - see the full list here: “ Network Load Balancing WMI Classes ” and read property from event structures accordingly. P.S. This is my "pen test." I hope to receive feedback on the usefulness and design, or vice versa - on the inappropriateness of such posts on Habré. Thanks.
    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.







    Also popular now: