简体   繁体   English

为什么我对Activator.CreateInstance的调用会间歇性地失败?

[英]Why does my call to Activator.CreateInstance intermittently fail?

I'm using the following code to access the Windows Explorer Shell's band site service: 我正在使用以下代码访问Windows资源管理器Shell的乐队网站服务:

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: 在很小的时间内(小于1%),对Activator.CreateInstance的调用会引发以下异常:

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. 我查找了错误代码,似乎表明该服务未注册。 I'm pretty sure that's not actually the case since the call will work just fine a few moments later, and the CLSID is provided by explorer.exe . 我很确定实际情况并非如此,因为调用会在稍后时间正常工作,而且CLSID由explorer.exe提供。

I'm stumped. 我很难过。 What might cause Activator.CreateInstance to fail, but only rarely? 什么可能导致Activator.CreateInstance失败,但很少?

Permissions? 权限? Yes, there is a problem with high-integrity applications accessing COM objects in medium-integrity applications. 是的,高完整性应用程序在中等完整性应用程序中访问COM对象时出现问题。 Somewhere at my website there is a demonstration of exactly this. 在我的网站的某个地方有一个这样的演示。 Ah, but for the TrayNotify class, not TrayBandSiteService . 啊,但是对于TrayNotify类,不是TrayBandSiteService I hope you didn't spend too much time figuring out something you might easily just have read. 我希望你没有花太多时间搞清楚你可能很容易读到的东西。

Anyway, see the last paragraph of this , from which I now quote: 无论如何,看到的最后一段这样 ,从我现在的报价:

As an aside, note that the program fails if User Account Control (UAC) is enabled and the program is run with elevated privilege. 另外,请注意,如果启用了用户帐户控制(UAC)并且程序以提升的权限运行,则程序将失败。 EXPLORER is the COM server and TRAYNOT (my demonstration program) is a COM client. EXPLORER是COM服务器,TRAYNOT(我的演示程序)是COM客户端。 Yet EXPLORER has only medium integrity. 然而,EXPLORER只具有中等完整性。 Though Microsoft's notes on UAC - they're not really substantial enough to count as programming documentation - deal with the situation of a lower-integrity COM client trying to communicate with a higher-integrity COM server, curiously little is said about the reverse. 虽然微软关于UAC的说明 - 它们并不足以算作编程文档 - 处理低完整性COM客户端试图与更高完整性的COM服务器通信的情况,但奇怪的是反过来说。 A higher-integrity COM client is protected from a lower-integrity COM server, much as a higher-integrity program is protected from receiving window messages from a lower-integrity program. 保护较高完整性的COM客户端免受较低完整性COM服务器的影响,就像保护较高完整性程序免受从较低完整性程序接收窗口消息一样。 For the latter, Microsoft provides the higher-integrity program with explicity means to open itself to expected messages from a lower-integrity source. 对于后者,Microsoft为更高完整性的程序提供明确的方法,以便从较低完整性的源中打开自己的预期消息。 Finding the analogous provision for COM may be a worthwhile exercise for another time. 找到COM的类似条款可能是另一次有价值的练习。

I regret to say I have never found the time. 我很遗憾地说我从来没有找到时间。

I found some explanations about TrayBandSiteService here: http://www.geoffchappell.com/viewer.htm?doc=studies/windows/shell/explorer/classes/traybandsiteservice.htm 我在这里找到了关于TrayBandSiteService的一些解释: http//www.geoffchappell.com/viewer.htm? doc = studies /windows / shells / explorer / classes / traybandsiteservice.htm

and if Geoff Chappell is right, it may be the same problem as on my machine: sometimes the explorer does a restart and for this moment(s) the TrayBandSiteService may not be accessible. 如果Geoff Chappell是对的,它可能与我的机器上的问题相同:有时,资源管理器会重新启动,而在这一刻,可能无法访问TrayBandSiteService。

Activator.CreateInstance(typeof(MyType)) Works fine when I try to run the code in a stand alone Windows Application. Activator.CreateInstance(typeof(MyType))当我尝试在独立的Windows应用程序中运行代码时工作正常。 Where as it fails when I try to run the same code in a class library under wcf services 当我尝试在wcf服务下的类库中运行相同的代码时失败的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM