简体   繁体   中英

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. I've added full-trust to my web.config file, but this does not solve the problem.

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 :

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. If your application doesn't have an installer, you can write an .exe with this code and run it from an elevated command prompt.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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