PerformanceCounter.NextValue() throws InvalidOperationException
Asked Answered
M

3

8

This is the code that creates the performance counter:

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

This is where the exception happens:

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

Here's the Exception message:

"Error Message: The Counter layout for the Category specified is invalid, a counter of the type: AverageCount64, AverageTimer32, CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns, CounterMultiTimer100NsInverse, RawFraction, or SampleFraction has to be immediately followed by any of the base counter types: AverageBase, CounterMultiBase, RawBase or SampleBase."

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

Details

This happens on Windows Server 2008 R2 64-bit OS. The FTP Server is IIS.

Minuscule answered 24/5, 2012 at 14:47 Comment(0)
O
2

At least on my Windows Server 2008 R2 with IIS 7.5 the performance counter category is called "Microsoft FTP Service" not "FTP Service". Perhaps it's different for different OS/IIS versions, buts its easy to check.

On your target server/machine:

  1. Run "Performance Monitor"
  2. Click "Performance Monitor in the left navigation pane
  3. Click the plus icon to Add a performance counter
  4. Scroll down the list of available counters. Look for FTP or Microsoft FTP to see if it exists... note if you don't have Microsoft FTP services installed you won't see the performance counter.
  5. Once found, select it, and you'll see a list of specific counter instances you can query. One of those instances is the '_Total' you were interested in.

Of course this also proves that the performance counter works independently of your code. That's a good thing to know!

For specific best practices of how to work with Performance Counters in C#/.NET see this stackoverflow post.

Oas answered 10/8, 2012 at 18:59 Comment(0)
C
1

I don't have the IIS installed on my PC, but if I did, here's what I'd try:

  1. Try to use Visual Studio's code generator for WMI by drag-and-drop from Server Explorer. Inspect the generated code and see if there are any differences from your code.
  2. Use reflector to see where the exception is thrown, set a custom breakpoint inside BCL, and reproduce. Then analyze the call stack contents and look for suspicious code paths or unexpected state.
Clippard answered 2/8, 2012 at 18:14 Comment(0)
W
0

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

Wiper answered 10/8, 2012 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.