Permalink
Browse files

Fixed WMI connection after a machine reboot (#331)

Fixes #325
  • Loading branch information...
Sacha-K authored and NickCraver committed Nov 27, 2018
1 parent 8a5f950 commit 7fec06b16b59573ecc83b3c407b01fd09a7625fe
Showing with 21 additions and 1 deletion.
  1. +21 −1 Opserver.Core/Monitoring/Wmi.cs
@@ -116,7 +116,27 @@ public Task<ManagementObjectCollection> Result
return _data != null ? Task.FromResult(_data) : Task.Run(() =>
{
try { return _data = _searcher.Get(); }
catch (Exception ex) { throw new Exception($"Failed to query {_wmiNamespace} on {_machineName}", ex); }
catch (Exception ex)
{
// Without this WMI queries will continue to fail after a machine reboots.
if (ex is System.Runtime.InteropServices.COMException)
{
foreach (var scopeCacheItem in _scopeCache)
{
if (scopeCacheItem.Key.StartsWith($@"\\{_machineName}"))
_scopeCache.TryRemove(scopeCacheItem.Key, out var scopeCacheValue);
}
foreach (var searchCacheItem in _searcherCache)
{
if (searchCacheItem.Key.StartsWith($@"\\{_machineName}"))
_searcherCache.TryRemove(searchCacheItem.Key, out var searchCacheValue);
}
}
throw new Exception($"Failed to query {_wmiNamespace} on {_machineName}", ex);
}
});
}
}

0 comments on commit 7fec06b

Please sign in to comment.