[EntLibFAQ]“不允许所请求的注册表访问权”的解释[0508Update]

news/2024/5/19 1:42:57

[EntLibFAQ]“不允许所请求的注册表访问权”的解释

Version

Date

Creator

Description

1.0.0.1

2006-5-2

郑昀@Ultrapower

草稿

 

继续阅读之前,我们假设您熟悉以下知识:

n         Microsoft Enterprise Library June 2005

n         EventLog和注册表的关系

 [现象]

首先,这种情况发生在第一次使用Microsoft Enterprise Library June 2005的库(不限定是哪一个库,可以是Caching,或者Security)时,并且你的应用是Web应用:

Windows2000上,你也许会看到错误“拒绝对注册表项的访问”;

Windows2003上,你将看到System.Security.SecurityException: 不允许所请求的注册表访问权。

此种情形,蝈蝈俊在他的《使用企业库在某些站点会报试图执行安全策略不允许的操作异常的解决方法。》中已经谈及了。我下面将试图详细说明原因。

[分析]

比如我的ASP.NET构建的Web站点中由于用到了EnterpriseLibraryCaching库,所以在客户生产环境中部署的时候,当部署完毕,第一次浏览站点时,我们遭遇了这个错误。

 

也许你熟悉ASP.NET的这种报错,知道应该用微软KB329291来解释(http://support.microsoft.com/?scid=kb;zh-cn;329291&spid=810&sid=58 )

KB信息

症状

使用 ASP.NET 在事件日志中创建一个新的事件源时,您可能会收到下面的错误信息:

System.Security.SecurityException:Requested registry access is not allowed.

原因

默认情况下,ASP.NET 工作进程的用户令牌是 ASPNET(或者,对于 Internet 信息服务 [IIS] 6.0 上运行的应用程序是 NetworkService)。由于您的帐户不具有创建事件源的正确用户权限,会出现症状部分中的问题。

 

那么什么情况下,会导致我们的Enterprise Library会不顾及ASP.NET调用者的感受,而强行创建新的事件源呢?

实际上也是因为一些无足轻重的原因。

Enterprise Library会主动创建一些新的性能计数器。调用链是这样的:

1:调用Common. InstrumentedEventInitialize方法:

AddPerformanceCounter(counterCategory, counterNames, createNewInstance);

2:而这个AddPerformanceCounter函数中用到了

PerformanceCounterInstances instances = new PerformanceCounterInstances(category, counterNames[i], createNewInstance);

3PerformanceCounterInstances呢,又是利用

new PerformanceCounter(this.categoryName, this.counterName, CurrentInstanceName, false)

来创建计数器。PerformanceCounter初始化 System.Diagnostics.PerformanceCounter 类的新的只读实例或读/写实例,并将其与本地计算机上指定的系统性能计数器或自定义性能计数器及类别实例关联。

但是创建性能计数器的时候,出了点错误,错误信息如下所示:

错误信息

Failed to create instances of performance counter 'Total Cache Entries' - 请求的性能计数器不是自定义计数器,它必须初始化为只读。

 

所以Enterprise library认为它需要通知你,通过以下代码:

EventLog eventlog = new EventLog(SR.LogName, SR.MachineName, SR.FailureLogSource);

eventlog.WriteEntry(message_, EventLogger.FailureLogType);

写一个Windows应用程序事件日志。

EventLog构造函数的第三个参数SR.FailureLogSource,其对应的字符串就是“Enterprise Library Instrumentation”。

 

这时候,它本希望能够增加“Enterprise Library Instrumentation”为一个新的事件源,就是在注册表

HKEY_LOCAL_MACHINE/

SYSTEM/

CurrentControlSet/

Services/

Eventlog/Application

下新建一个项叫做“Enterprise Library Instrumentation”的。但是,ASP.NET的用户身份限制了创建工作。于是,异常被引发了。并且,一路被抛了出来,导致所有的工作都无法进行下去。

这是一个不应该被如此重视的异常错误,完全可以忽略。

上面的阐述,是我个人的理解,可能与事实之间存在偏差。

[解决]

临时救急的办法:

我们有一个神奇的解决办法,是houxy找到的“IIS虚拟目录的匿名访问选择框点掉再选中”大法:

首先,不让该站点虚拟目录启用匿名访问,这样在本机管理员身份下访问你的web应用,因为你当前的身份应该拥有对注册表的权限,所以肯定能做Enterprise Library要做的事情。然后,再让虚拟目录启用匿名访问,再次访问Web应用,一切恢复正常,不再报告“System.Security.SecurityException: 不允许所请求的注册表访问权。”了。

这时候,你从Windows应用程序日志中可以看到这么两个错误日志:

Windows应用程序日志1

事件类型:      错误

事件来源:      Enterprise Library Instrumentation

事件种类:     

事件 ID: 0

日期:             2006-4-30

事件:             15:06:00

描述:

Failed to create instances of performance counter 'Total Cache Entries' - 请求的性能计数器不是自定义计数器,它必须初始化为只读。.

 

 

事件类型:      错误

事件来源:      Enterprise Library Instrumentation

事件种类:     

事件 ID: 0

日期:             2006-4-30

事件:             15:06:00

描述:

Failed to create instances of performance counter 'Cache Total Turnover Rate' - 请求的性能计数器不是自定义计数器,它必须初始化为只读。.

 

 

重新编译的办法:

蝈蝈俊在他的《使用企业库在某些站点会报试图执行安全策略不允许的操作异常的解决方法。》中已经谈及了。不再阐述了。不过我没有在客户现场试验成功。

 

 

主动Installutil的办法:

"在服务器端执行一下:InstallUtil Microsoft.Practices.EnterpriseLibrary.Caching.dll 就好了,其他模块也是这么做。 "-----liweiguang said.

 

在部署之前主动InstallService的办法:

在新Windows2003环境下,首先安装Enterprise Library

之后运行Microsoft Enterprise Library安装目录下的src子文件夹下的

InstallServices.bat。即可彻底解决这个问题。

(注:不安装Enterprise Library也可以。在海油新大楼环境中,我就没有安装Enterprise Library。只不过由于没有进行InstallServices.bat的预处理,所以遇到了前面说的这个问题,实际上如果运行了这个bat文件中的

if Exist Microsoft.Practices.EnterpriseLibrary.Caching.dll installutil %action% Microsoft.Practices.EnterpriseLibrary.Caching.dll

语句命令,这样,我们通过

installutil Microsoft.Practices.EnterpriseLibrary.Caching.dll

就可以预先将Caching库所需要的性能计数器等资源准备好。)

 





http://www.niftyadmin.cn/n/3649758.html

相关文章

mac node repl_如何使用Node.js REPL

mac node replThe author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program. 作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。 介绍 (Introduction) The Node…

[Remoting FAQ]传递Remoting参数时遇到的两种常见错误

[Remoting FAQ]传递Remoting参数时遇到的两种常见错误VersionDateCreatorDescription1.0.0.12006-4-25郑昀Ultrapower草稿继续阅读之前,我们假设您熟悉以下知识:n Remoting[现象1]我们先来描述一个简单的错误。当你激活远端Remoting Objects时&a…

greensock下载_使用GreenSock绘制Alligator.io SVG徽标

greensock下载To get the most out of this article it is important that you have a solid understanding of JavaScript. We will be solely focusing on understanding GreenSock in this article, so if you haven’t used JavaScript before then get learning and come …

[收藏]不仅仅跟随更能够提出震撼性的技术框架/技术特点出来

“很久以前Michael Chen写过一篇blog,表达了Michael Chen对目前国内技术现状的一种思考:希望有更多的人能够不仅仅跟随,更能够提出震撼性的技术框架/技术特点出来。然而,那个时候许多人包括干脆放弃了希望安心的做起了传教士&…

[J2ME QA]MMAPICannot parse this type of AMR异常之讨论

[J2ME] MMAPI的Cannot parse this type of AMR异常之讨论郑昀 草拟 20060417[现象]首先,我们假设遇到这种错误的人们了解如何使用MMAPI,从而排除代码使用不当问题。那么在播放3gp媒体文件时遇到“java.lang.Exception: Cannot parse this type of AMR”的…

flutter布局中的单位_在Flutter中创建基本布局

flutter布局中的单位As Flutter quickly becomes one of the leading technologies in app development, it is important for even someone in the web dev environment to check out. Coming from a background of HTML, CSS and JavaScript, the way Flutter handled everyt…

Signing a midlet suite的讨论稿[J2ME]

[J2ME] Signing a midlet suite的讨论稿发起者:郑昀(zhengyun_ustc)[问题]如何对一个MIDlet jar签名呢?很多人都想知道怎么让自己的MIDlet访问网络啦、发送短信啦都能够不弹出烦人的警告框。单纯回答说“你必须去Versign购买一个证书,或者找厂…

在JavaScript中使用Trim String方法

It’s always helpful to have an easy method method on strings to remove trailing or leading whitespace and the trim() method available on JavaScript strings is here to help us with doing exactly that. 在字符串上使用简单的方法方法来删除尾部或前导空格总是有帮…