Why does my call to Activator.CreateInstance intermittently fail?

Posted by Daniel Stutzbach on Stack Overflow See other posts from Stack Overflow or by Daniel Stutzbach
Published on 2010-05-22T23:26:51Z Indexed on 2010/05/22 23:30 UTC
Read the original article Hit count: 356

Filed under:
|
|
|
|

I'm using the following code to access the Windows Explorer Shell's band site service:

Guid GUID_TrayBandSiteService = new Guid(0xF60AD0A0, 0xE5E1, 0x45cb, 0xB5, 0x1A, 0xE1, 0x5B, 0x9F, 0x8B, 0x29, 0x34);
Type shellTrayBandSiteService = Type.GetTypeFromCLSID(GUID_TrayBandSiteService, true);
site = Activator.CreateInstance(shellTrayBandSiteService) as IBandSite;

Mostly, it works great. A very small percentage of the time (less than 1%), the call to Activator.CreateInstance throws the following exception:

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {F60AD0A0-E5E1-45CB-B51A-E15B9F8B2934} failed due to the following error: 80040154.
  at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
  at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
  at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
  at System.Activator.CreateInstance(Type type, Boolean nonPublic)

I've looked up the error code, and it appears to indicate that the service isn't registered. That's nonsense in my case, since I'm trying to access a service provided by the operating system (explorer.exe, to be specific).

I'm stumped. What might cause Activator.CreateInstance fail, but only rarely?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms