One of the ways to monitor WCF is by using the Windows Management Instrumentation (WMI) provider. In short, WMI provides you with a way to view information about running services in your machine and in other machines in the network. You can view their process information, service information, endpoint configuration, and even change some of the service’s configuration in runtime, without needing to restart the service.
Little has been written about this feature. The basics of it is document of course on MSDN, which tells you what to set in your configuration to make the WMI provider work. The MSDN article also provides a link to download the WMI Administrative Tools which can be used to look at the information of running services by using WMI.
Aside from MSDN, there is a also quite a good blog post from Pablo Cibraro on how to use the WMI Object Browser (part of the WMI Administrative Tools) to view information about running services, and also on how to change some of the service’s settings through the WMI Object Browser. For example, you can use WMI to change the message logging level of a running WCF service, and instruct it to enable/disable logging messages at the transport level or at the service level.
This feature works, and it is actually very cool, except for one issue – the WMI Administrative Tools where written back in 2002, and work well with 32-bit operating systems, but don’t work too good with 64-bit systems. Unfortunately my machine is running a 64-bit Windows 7, and I can’t get the damn thing to work.
I tried some other WMI utilities, such as the WMI explorer, but none seemed to give the same capabilities of the WMI Administrative Tools. So I went on to look for a different way to control my WCF services through WMI.
At first I thought about writing a .NET application, but eventually I used PowerShell to write a short script (it took me about an hour to write it, mainly because this is only the third time I’m using PowerShell, so I had to learn a bit of syntax).
To run the script, you need to give it the name of your service (class name, without the namespace), the type of the logging level – transport/service, and whether to turn logging on or off, for example the following command will turn off the transport level message logging of service myService which is running currently in the machine:
.\SetMessageLogging.ps1 myService transport off
If you configured your service to use the WMI provider, the script will find it and change its configuration, whether it runs in a self-hosted application, or in IIS. Your service doesn’t need to be restarted and connected clients don’t even need to reconnect to the service.
To be able to use this script, you also need to prematurely add listeners to your <system.diagnostics> section in the service configuration, before starting the service.
You can download the script from my skydrive folder. Enjoy!
BTW, I will show this script next week in my advanced WCF workshop, which I will host in Sela’s DevDays. So if you want to learn more about the hidden gems of WCF, I suggest you hurry up and register, maybe you’ll be the last one who manages to squeeze into my class.