Using Impersonation to query MSMQ and putting results in a Dundas Gauge.

by MikeHogg 29. April 2009 19:58

I had a lot of functions like this, all running on BackgroundWorker threads, on a status page that refreshed itself every three minutes.  Most of them queried different database, most of them also were hooked up to more complicated Dundas Gauges and Charts.  Here’s one that’s really simple, but illustrates accessing a particular MSMQ, which needed impersonation of a service account.

    private void loadMSMQGauge()
    {
        int numInQueue = -1;
        try
        {
            Utils.ImpersonateUser iu = new Utils.ImpersonateUser();
            iu.Impersonate("corp", "someserviceaccount", "password");
            //PerformanceCounter pc = new PerformanceCounter("MSMQ Queue",
            //    "Messages in Queue", "someservername\\private$\\ssome_error_queue", "someservername");
            // HP openview kept horking the msmq perf counter, after re-registering the counters several times 
            //   and getting horked again after reboots we will take the long route.
            using (MessageQueue mq = new MessageQueue("FormatName:DIRECT=OS:someservername\\private$\\some_error_queue", QueueAccessMode.Peek))
            {
                Message[] messages = mq.GetAllMessages();
                numInQueue = messages.Length;
            }
            this.GaugeContainer1.LinearGauges[4].Pointers[0].Value = numInQueue; //  pc.RawValue;
            this.GaugeContainer1.NumericIndicators[4].Value = this.GaugeContainer1.LinearGauges[4].Pointers[0].Value;
            iu.Undo();
        }
        catch (System.InvalidOperationException)  // this was from PerfCounter object
        {
            //empty queue, do nothing
        }
        catch (MessageQueueException ex) //this comes from mq object, just write it out also
        {
            Response.Write(ex.ToString());
        }
        catch (Exception ex)
        {

 

Simple hookup, but it showed on a Widescreen TV hanging on the wall of our department, the current state of one of our systems, at a glance, with big digital numbers and red and yellow colors.

Tags:

C#

About Mike Hogg

Mike Hogg is a c# developer in Brooklyn.

More Here

Favorite Books

This book had the most influence on my coding style. It drastically changed the way I write code and turned me on to test driven development even if I don't always use it. It made me write clearer, functional-style code using more principles such as DRY, encapsulation, single responsibility, and more. amazon.com

This book opened my eyes to a methodical and systematic approach to upgrading legacy codebases step by step. Incrementally transforming code blocks into testable code before making improvements. amazon.com

More Here