You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create a Windows Forms application.
The application connects to a Microsoft SQL Server database.
You need to find out whether the application is explicitly closing or disposing SQL connections. Which code segment should you use?
A.
string instanceName = Assembly.GetEntryAssembly().FullName;
PerformanceCounter perf = new PerformanceCounter( “.NET Data Provider for SqlServer”,
“NumberOfReclaimedConnections”, instanceName, true);
int leakedConnections = (int)perf.NextValue();
B.
string instanceName = Assembly.GetEntryAssembly().GetName().Name;
PerformanceCounter perf = new PerformanceCounter( “.NET Data Provider for SqlServer”,
“NumberOfReclaimedConnections”, instanceName, true);
int leakedConnections = (int)perf.NextValue();
C.
string instanceName = Assembly.GetEntryAssembly().FullName;
PerformanceCounter perf = new PerformanceCounter( “.NET Data Provider for SqlServer”,
“NumberOfNonPooledConnections”, instanceName, true);
int leakedConnections = (int)perf.NextValue();
D.
string instanceName = Assembly.GetEntryAssembly().GetName().Name;
PerformanceCounter perf = new PerformanceCounter( “.NET Data Provider for SqlServer”,
“NumberOfNonPooledConnections”, instanceName, true);
int leakedConnections = (int)perf.NextValue();
Explanation:
NumberOfNonPooledConnections The number of active connections that are not pooled.
NumberOfReclaimedConnections The number of connections that have been reclaimed through garbage collection where Close or Dispose was not called by the application.
Not explicitly closing or disposing connections hurts performance.Use of ADO.NET performance counters
(http://msdn.microsoft.com/en-us/library/ms254503(v=vs.80).aspx)Assembly Class
(http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx)
I think the correct answer is B. It’s even in the link posted in the Explanation:
http://msdn.microsoft.com/en-us/library/ms254503%28v=vs.110%29.aspx
Look at the code:
private string GetInstanceName()
{
//This works for Winforms apps.
string instanceName =
System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
}
Assembly.GetEntryAssembly().GetName().Name will return a simple name of the exe without the extension, which is what you want:
ExampleAssembly
On the other hand Assembly.GetEntryAssembly().FullName will return this crazy looking string:
ExampleAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
You are right the correct answer is B, I have checked it in Visual Studio. A bit more information of the PerformanceCounter() constructor:
Signature: public PerformanceCounter(string categoryName, string counterName, string instanceName, bool readOnly);
//
// Summary:
// Initializes a new, read-only or read/write instance of the System.Diagnostics.PerformanceCounter
// class and associates it with the specified system or custom performance counter
// and category instance on the local computer.
//
// Parameters:
// categoryName:
// The name of the performance counter category (performance object) with which
// this performance counter is associated.
//
// counterName:
// The name of the performance counter.
//
// instanceName:
// The name of the performance counter category instance, or an empty string
// (“”), if the category contains a single instance.
//
// readOnly:
// true to access a counter in read-only mode; false to access a counter in
// read/write mode.
//
// Exceptions:
// System.InvalidOperationException:
// categoryName is an empty string (“”).-or- counterName is an empty string
// (“”).-or- The read/write permission setting requested is invalid for this
// counter.-or- The category specified does not exist (if readOnly is true).
// -or- The category specified is not a .NET Framework custom category (if readOnly
// is false). -or-The category specified is marked as multi-instance and requires
// the performance counter to be created with an instance name.-or-instanceName
// is longer than 127 characters.-or-categoryName and counterName have been
// localized into different languages.
//
// System.ArgumentNullException:
// categoryName or counterName is null.
//
// System.ComponentModel.Win32Exception:
// An error occurred when accessing a system API.
//
// System.PlatformNotSupportedException:
// The platform is Windows 98 or Windows Millennium Edition (Me), which does
// not support performance counters.
//
// System.UnauthorizedAccessException:
// Code that is executing without administrative privileges attempted to read
// a performance counter.
Checked it for both Winform and Console applications, only worked with the simple instanceName. With full name they throw InvalidOperationException (my Project name was: An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.dll
Additional information: Instance ‘PerformanceCounterTestConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ does not exist in the specified Category.):
An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.dll
Additional information: Instance ‘PerformanceCounterTestConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ does not exist in the specified Category.