Cannot attach to a network adapter on one VM, same code works on another VM

泄露秘密 提交于 2019-12-11 16:53:11

问题


I need to get performance counters from the network card. To make things easy, the user selects the needed adapter by typing an index number in a console application.

This is the code that gets the user input and creates performance counter instance

var connectionNames = NetworkCardLocator.GetConnectedCardNames().ToArray();
log.Debug("Please select one of the available connections");
log.Debug("--");
for (int i = 0; i < connectionNames.Count(); i++)
{
    log.Debug(i + ". " + connectionNames[i]);
}
log.Debug("--");
var key = Console.ReadLine();
int idx = 0;
Int32.TryParse(key, out idx);
string connectionName = connectionNames[idx];

var networkBytesSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", connectionName);
var networkBytesReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", connectionName);
var networkBytesTotal = new PerformanceCounter("Network Interface", "Bytes Total/sec", connectionName);

This is how I select available adapters

internal static IEnumerable<string> GetConnectedCardNames()
{
    string query = String.Format(@"SELECT * FROM Win32_NetworkAdapter");
    var searcher = new ManagementObjectSearcher
    {
        Query = new ObjectQuery(query)
    };

    try
    {
        log.Debug("Trying to select network adapters");
        var adapterObjects = searcher.Get();

        var names = (from ManagementObject o in adapterObjects
                        select o["Name"])
                            .Cast<string>();

        return names;
    }
    catch (Exception ex)
    {
        log.Debug("Failed to get needed names, see Exception log for details");
        log.Fatal(ex);
        throw;
    }
}

Problem
Given I have selected the needed adapter, the code works on my machine (Win 2008 R2 Ent x64). It doesn't work on some of the VMs I use for testing (Win 2008 R1 DC x86). Any selection there gives me an exception (still works on my PC and VM Win 2008 R1 Std x86)

foreach (PerformanceCounter counter in counters)
{
    float rawValue = counter.NextValue(); //thrown here
    ...
}

2011-06-10 11:08:20,505 [10] DEBUG TH.Exceptions Instance 'WAN Miniport (PPTP)' does not exist in the specified Category.
System.InvalidOperationException: Instance 'WAN Miniport (PPTP)' does not exist in the specified Category.
   at System.Diagnostics.CounterDefinitionSample.GetInstanceValue(String instanceName)
   at System.Diagnostics.PerformanceCounter.NextSample()
   at System.Diagnostics.PerformanceCounter.NextValue()
   at TH.PerformanceMonitor.API.Internal.PerformanceLogService.DoPerformanceLogging(IEnumerable`1 counters, Int32 interval, TimeSpan duration) in C:\Projects\...\PerformanceLogService.cs:line 122
   at TH.PerformanceMonitor.API.PerformanceManager.DoPerformanceLogging() in C:\Projects\...\PerformanceManager.cs:line 294
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Q
What might be the problem, or how can I trace the reasons?


回答1:


The problem was in the phantom network adapters. Once they were removed from the VM, everything started to work.



来源:https://stackoverflow.com/questions/6305016/cannot-attach-to-a-network-adapter-on-one-vm-same-code-works-on-another-vm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!