我创建了一个程序供我们在办公室计算机上使用。当他们尝试使用该程序时,它会从SqlConnection构造函数中抛出一个异常,说明“不允许注册表访问”。
我们的网络管理员限制用户访问注册表,以防止人们安装个人软件。另外,我没有必要写入事件日志。是否有一些原因要告诉SqlConnection不要尝试访问事件日志?
源代码:
string _ConnectionString = "server=our_server_name; initial catalog=our_db_name; integrated security=true;"
...
using (var connection = new SqlConnection(_ConnectionString)) <-- exception thrown here
{
connection.Open();
...
}
异常
System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception.
---> System.Security.SecurityException: Requested registry access is not allowed.
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at System.Diagnostics.PerformanceCounterLib.FindCustomCategory(String category, PerformanceCounterCategoryType& categoryType)
at System.Diagnostics.PerformanceCounterLib.IsCustomCategory(String machine, String category)
at System.Diagnostics.PerformanceCounter.InitializeImpl()
at System.Diagnostics.PerformanceCounter.set_RawValue(Int64 value)
at System.Data.ProviderBase.DbConnectionPoolCounters.Counter..ctor(String categoryName, String instanceName, String counterName, PerformanceCounterType counterType)
at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
at System.Data.SqlClient.SqlPerformanceCounters..ctor()
at System.Data.SqlClient.SqlPerformanceCounters..cctor()
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlConnectionFactory..cctor()
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlConnection..cctor()
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlConnection..ctor()
at System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential credential)
at OurProgram.Form1.UpdateStuff() in C:\path_to_project\Form1.cs:line 111
由于
答案 0 :(得分:0)
您应该尝试升级到.net 4.6,因为自上次升级到最新的.net框架以来,特定代码路径似乎已添加了try catch。
if (baseKey != null) {
try {
key = baseKey.OpenSubKey(keyPath);
} catch (SecurityException) {
// we may not have permission to read the registry key on the remote machine. The security exception
// is thrown when RegOpenKeyEx returns ERROR_ACCESS_DENIED or ERROR_BAD_IMPERSONATION_LEVEL
//
// In this case we return an 'Unknown' category type and 'false' to indicate the category is *not* custom.
//
categoryType = PerformanceCounterCategoryType.Unknown;
this.customCategoryTable[category] = categoryType;
return false;
}
}
此外,如果您的网络管理员为您提供任何不支持升级的信息。然后告诉他,微软将在2016年初放弃对.net 4.0的支持。所以它只是时间问题:D
.NET Framework 4:.NET Framework 4未在任何操作中发布 系统。 Windows Server 2003 SP2支持.NET Framework 4,直到 2015年7月14日,Windows Vista SP2,Windows 7 SP1,Windows Server 2008 SP2,Windows Server 2008 R2 SP1,Windows 8.1更新,Windows Server 2012和Windows Server 2012 R2,直到2016年1月12日。