WMI Exception - System.Runtime.InteropServices.COMException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?am1hZ2FyYW0=?=

    WMI Exception - System.Runtime.InteropServices.COMException

    I have a Windows service written in C# that is configured to automatically
    start. Sometimes - maybe 20% of the time - the service fails to start due to
    an exception in WMI code. I haven't made my service dependent on WMI (or any
    other service) because it appears that .net WMI calls automatically start the
    WMI service if necessary. Below are the details. What causes this? How can I
    make this code work 100% of the time? The code is trying to translate a
    well-known group SID to its name like "Administrators ". I was thinking about
    wrapping my code in try catch and if it fails, waiting 5 seconds and trying
    again, but this is guesswork. I also might make the service dependent on
    winmgmt.

    Here's the exception:

    Service cannot be started.
    System.Runtime. InteropServices .COMException (0x80010002)
    Call was canceled by the message filter.
    (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CAN CELED))
    at
    System.Runtime. InteropServices .Marshal.ThrowE xceptionForHRIn ternal(Int32
    errorCode, IntPtr errorInfo)
    at System.Manageme nt.ManagementSc ope.InitializeG uts(Object o)
    at System.Manageme nt.ManagementSc ope.Initialize( )
    at System.Manageme nt.ManagementOb jectSearcher.In itialize()
    at System.Manageme nt.ManagementOb jectSearcher.Ge t()
    at
    Magaram.TimeLim its.Server.Acco untManager.GetG roupNameFromSid (SecurityIdenti fier sid)
    at Magaram.TimeLim its.Server.Acco untManager..cto r()
    at
    Magaram.TimeLim its.Server.Time LimitService.Cr eateRealService (SessionManager
    sessionManager)
    at
    Magaram.TimeLim its.ServerHost. TimeLimitsWindo wsService.OnSta rt(String[] args)
    at System.ServiceP rocess.ServiceB ase.ServiceQueu edMainCallback( Object
    state)

    My code:

    internal static string GetGroupNameFro mSid(SecurityId entifier sid) {
    SelectQuery query = new SelectQuery("Wi n32_Group",
    string.Format(" Domain='{0}'", Environment.Mac hineName));
    using (ManagementObje ctSearcher searcher = new
    ManagementObjec tSearcher(query )) {
    foreach (ManagementObje ct group in searcher.Get()) {
    string sddl = group["SID"] as string;
    if (sddl.ToLower() .Trim() == sid.Value.ToLow er().Trim()) {
    string groupName = (string)(group["Name"]);
    return group["Name"] as string;
    }
    }
    }
    throw new ArgumentOutOfRa ngeException("s id",
    string.Format(" Could not get the group name for sid: {0}", sid.Value));
    }
Working...