c# - ASP.NET MVC : Requested registry access denied when creating performance counters

标签 c# asp.net asp.net-mvc security asp.net-mvc-4

我尝试为我的 MVC 操作创建性能计数器,但收到一个 SecurityException,指出注册表访问被拒绝。我已向 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" />

最佳答案

这不是中等信任/完全信任问题。您需要以管理员或其他特权用户身份运行才能在计算机上创建性能计数器。见http://msdn.microsoft.com/en-us/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.

基本上,如果您的应用程序有安装程序,您将在安装时调用 PerformanceCounterCategory.Create。如果您的应用程序没有安装程序,您可以使用此代码编写 .exe 并从提升的命令提示符运行它。

然后,作为 MVC 应用程序正常执行的一部分,您可以利用该性能计数器已经存在的事实,通过使用标准 PerformanceCounter 类打开计数器并向其中写入值。

关于c# - ASP.NET MVC : Requested registry access denied when creating performance counters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22616361/

相关文章:

c# - 我添加了一个用户并将他分配给一个角色,当我使用 Membership.GetAllUsers() 时它没有返回

c# - 是否可以在 c# 中使用 "inherited partial structs"?

c# - 未找到 OpenID 终结点。 - .Net OpenID

c# - ASP.NET MVC LIST 和 Create 在同一 View 中

c# - Raspberry Pi+MonoDevelop如何从串口读取数据?

c# - WndProc 将 lParam 挂接到 xy 线?

javascript - Visual Studio 2015 中的 Grunt watch 任务绑定(bind)

asp.net-mvc - "Unable to evaluate expression"和异步 CTP

c# - 如何在 azure 上以编程方式托管 wcf 服务

c# - MonoDevelop 和/或 MonoTouch 错误?