调用WCF函数报Performance Counter需初始化为ReadOnly错误 修复无效
WCF调用时Enterprise Library 4.1性能计数器报错排查方案
错误信息
调用WCF接口时抛出以下异常:
The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly at System.Diagnostics.PerformanceCounter.InitializeImpl() at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly) at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.EnterpriseLibraryPerformanceCounterFactory.CreateCounter(String categoryName, String counterName, String[] instanceNames) at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) at Example.SoaPlatform.LoggingService.Client.LogTraceCallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) at Microsoft.Practices.Unity.InterceptionExtension.HandlerPipeline.Invoke(IMethodInvocation input, InvokeHandlerDelegate target) at Microsoft.Practices.Unity.InterceptionExtension.InterceptingRealProxy.Invoke(IMessage msg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Example.Mobiliti.MobilitiAlertService.ContractTypes.IMobilitiAlertService.SendSMSAlert(SendSmsAlertRequest request) at SyncInvokeSendSMSAlert(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
已执行以下修复命令但问题未解决:
lodctr /R cd C:\Windows\Inf\.NETFramework lodctr corperfmonsymbols.ini
当前环境确认:使用的Enterprise Library版本为4.1.0。
排查解决步骤
- 检查服务运行账号权限:性能计数器初始化需要对应注册表和系统目录的访问权限,承载WCF的进程账号(IIS应用池标识、Windows服务运行账号)必须加入本地
Performance Monitor Users组或者本地管理员组,普通服务账号默认无权限访问HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib注册表路径,会导致计数器初始化失败。调整权限后重启进程验证。 - 修复架构不匹配导致的计数器重置无效问题:64位系统下如果WCF服务编译为32位(x86)平台目标,之前执行的默认
lodctr /R调用的是64位版本的计数器修复工具,仅对64位程序生效。需要执行32位版本的工具重置计数器:
执行完成后再重新注册.NET性能计数器即可。C:\Windows\SysWOW64\lodctr.exe /R - 手动注册Enterprise Library 4.1自定义性能计数器:之前执行的命令仅修复系统自带的.NET性能计数器,未包含EntLib自定义的计数器类别。打开对应架构的VS开发人员命令提示符,进入Enterprise Library 4.1的安装目录,执行目录下的
InstallUtil.bat(部分版本为InstallServices.bat),脚本会自动完成EntLib所有自定义性能计数器的注册,执行完成后重启WCF服务验证。 - 检查计数器注册表损坏:打开注册表编辑器,定位到
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services路径,查找所有名称以Enterprise Library 4.1开头的子项,确认子项下的Performance节点存在,且节点内的Counter Names、Counter Help字符串值内容不为空。如果存在键值缺失,先备份当前注册表,再从同版本EntLib正常运行的服务器导出对应注册表项导入故障机,重启后验证。 - 无性能计数需求时直接关闭EntLib埋点功能:如果业务不需要EntLib的性能计数监控,可直接修改WCF服务的配置文件(web.config/app.config),添加以下配置关闭性能计数器初始化逻辑,从根源避免报错:
保存配置后重启服务即可生效。<configuration> <configSections> <section name="instrumentationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration.InstrumentationConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </configSections> <instrumentationConfiguration performanceCountersEnabled="false" eventLoggingEnabled="true" wmiEnabled="false" /> </configuration>
内容的提问来源于stack exchange,提问作者Nest
相关产品推荐
相关产品推荐

