PerformanceCounter.NextValue () throws an InvalidOperationException

This is the code that creates the performance counter:

var ftpPerfCounter = new PerformanceCounter("FTP Service", "Current Connections", "_Total"); 

An exception occurs here:

 int cnt = (int)Math.Round(ftpPerfCounter.NextValue()); 

Here's the Exception message:

"Error message: the counter layout for the specified category is not valid, the counter type: AverageCount64, AverageTimer32, CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns, CounterMultiTimer100NsInverse, RawFraction or SampleFraction should immediately be followed by any types of the base counter: MiddleBase, BaseBase, MiddleBase,.

The error message is rather cryptic. I am not sure what can be done to avoid an exception in the future.

More details

This occurs on 64-bit Windows Server 2008 R2. The FTP server is IIS.

+7
source share
3 answers

At least on my Windows Server 2008 R2 with IIS 7.5, the performance counter category is called "Microsoft FTP Service" and not "FTP Service". It may be different for different OS / IIS versions, but it is easy to verify.

On your target server / machine:

  • Launch Performance Monitor
  • Click "Performance Monitor in the left navigation bar.
  • Click the plus sign to add a performance counter.
  • Scroll through the list of available counters. Check FTP or Microsoft FTP to see if it exists ... note that if you do not have Microsoft FTP services installed, you won’t see the performance counter.
  • After that, select it and you will see a list of specific counter instances that you can request. One such case is the "_Total" that you were interested in.

Of course, this also proves that the performance counter works regardless of your code. This is a good thing to know!

For specific recommendations on working with Performance Counters in C # /. NET, see this column column .

+2
source

I do not have IIS installed on my PC, but if I did, this is what I will try:

  • Try using the Visual Studio Code Generator for WMI by dragging it from Server Explorer. Inspect the generated code and see if there are any differences from your code.
  • Use the reflector to see where the exception is thrown, set a custom breakpoint inside the BCL, and play. Then, analyze the contents of the call stack and look for suspicious code paths or unexpected status.
+1
source

They seem to have solved a similar problem here . Maybe you need to set up a basic counter?

0
source

All Articles