简体   繁体   English

ASP.NET MVC:创建性能计数器时拒绝请求的注册表访问

[英]ASP.NET MVC: Requested registry access denied when creating performance counters

I'm attempting to create performance counters for my MVC actions and receive a SecurityException saying that registry access is denied. 我试图为我的MVC操作创建性能计数器,并收到一个SecurityException,说注册表访问被拒绝。 I've added full-trust to my web.config file, but this does not solve the problem. 我已将完全信任添加到我的web.config文件中,但这不能解决问题。

PerformanceCounterCategory performancecountercategory = null;
if (!PerformanceCounterCategory.Exists(categoryname))
{
    performancecountercategory = PerformanceCounterCategory.Create(categoryname, "Latency counters for the Media Gateway", PerformanceCounterCategoryType.SingleInstance, countercreationdata);
}
else
{
    performancecountercategory = new PerformanceCounterCategory(Constants.PerformanceCounterCategory);
}


  <system.web>
    <trust level="Full"></trust>
    <compilation targetFramework="4.5" debug="true" />
    <httpRuntime targetFramework="4.5" />

This isn't a Medium Trust / Full Trust issue. 这不是中等信任/完全信任问题。 You need to be running as an administrator or other privileged user to create performance counters on the machine. 您需要以管理员或其他特权用户身份运行才能在计算机上创建性能计数器。 See the remarks section of http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountercategory(v=vs.110).aspx : 请参阅http://msdn.microsoft.com/zh-cn/library/system.diagnostics.performancecountercategory(v=vs.110).aspx的备注部分:

It is strongly recommended that new performance counter categories be created during the installation of the application, not during the execution of the application. 强烈建议在应用程序安装期间而不是在应用程序执行期间创建新的性能计数器类别。

Basically, if your application has an installer, you'd call PerformanceCounterCategory.Create at installation time. 基本上,如果您的应用程序具有安装程序,则在安装时调用PerformanceCounterCategory.Create。 If your application doesn't have an installer, you can write an .exe with this code and run it from an elevated command prompt. 如果您的应用程序没有安装程序,则可以使用此代码编写.exe并从提升的命令提示符下运行它。

Then, as part of your MVC application's normal execution, you can take advantage of the fact that this performance counter already exists by using the standard PerformanceCounter class to open the counter and write values to it. 然后,作为MVC应用程序正常执行的一部分,您可以利用标准PerformanceCounter类打开该计数器并向其中写入值,从而利用该性能计数器已经存在的事实。

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

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