Fixing "Instance names used for writing to custom counters must be 127 characters or less."

by timvasil 11/11/2008 12:39:00 PM

If you've run a unit test in Visual Studio 2008 that utilizes database connectivity, you may have seen this error when creating a connection object:

Instance names used for writing to custom counters must be 127 characters or less.

The problem crops up when you're running the test within a directory structure with a fairly long name, i.e. you're organizing your code well and giving your projects descriptive names.  Well, this error is punishment for being organized.

There are several Microsoft bug reports on the issue.  One has been ignored, the other closed.

There are posts on the web with some workarounds, but none worked for me, perhaps because I'm using Visual Studio 2008?  No matter what I tweaked in .testrunconfig I was still getting the error.  The other approach--shortening the pathname--was really a non-starter.  I wasn't going to compromize well-thought-out organization to get around a Microsoft bug.

So I took a peek at the source of the problem as hinted by the call stack when the error appeared.  It's a method in the internal DbConnectionPoolCounters class in the System.Data.ProviderBase assembly.  Here's the code:

private string GetInstanceName()
{
    string assemblyName = this.GetAssemblyName();
    if (ADP.IsEmpty(assemblyName))
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        if (currentDomain != null)
        {
            assemblyName = currentDomain.FriendlyName;
        }
    }
    int currentProcessId = SafeNativeMethods.GetCurrentProcessId();
    return string.Format(null, "{0}[{1}]", new object[] { assemblyName, currentProcessId }).Replace('(', '[').Replace(')', ']').Replace('#', '_').Replace('/', '_').Replace('\\', '_');
}

The instance name of the performance counter is being built, in part, from the name of the AppDomain.  When running tests in Visual Studio, this name can be very, very long, especially if your project's home has a long pathname.

The workaround I pursued, then, is to shorten the name of the app domain.  While AppDomain.FriendlyName is a read-only property, and there's no private field of the AppDomain class to change it, there is a private extern method of AppDomain called nSetupFriendlyName.  And, like magic, if you invoke this method and give the app domain a shorter name, problem solved!

Here's the workaround, utilizing a little reflection magic:

[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
    typeof(AppDomain).GetMethod("nSetupFriendlyName", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(AppDomain.CurrentDomain, new object[] { "Test" });
}

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Visual Studio | .NET Framework | C# | ADO.NET | MSTest

Converting a Class Library to an MS Test Library

by timvasil 6/10/2008 5:15:00 PM

If you add the [TestMethod] attribute to methods in a regular class library, they won't show up in the test view or test editor.  You may see a message "no tests are loaded."  This happens because Visual Studio doesn't look for the attribute in projects that don't have special "I am a test" markings.  If you create a test project from scratch and add the code the problem will go away, but if you've spent time customizing a project, its references, etc., fear not; you don't need to start over.  Edit the .csproj (or .vbproj) file manually and add the following XML as a child of the first <PropertyGroup> element that appears:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Visual Studio | MSTest

Fix for "'asp' is an unrecognized tag prefix"

by timvasil 11/15/2007 1:14:00 PM

Recently, while using the beta 2 version of Visual Studio 2008, IntelliSense stopped working when editing .aspx pages and master pages.  Well, it was more than just IntelliSense not working.  I was getting horrible validation errors, such as 'asp' is an unrecocognized tag prefix or device filter.  Considering 'asp' is one of those tag prefixes that should always be defined (thank you, machine.config), I knew something was horribly wrong.  Sources on the web had me try all sorts of things:

  • Make sure the MasterPage is open,
  • Close VS and delete all files in C:\Documents and Settings\{username}\Application Data\Microsoft\VisualStudio\9.0\ReflectedSchemas, and
  • Ensure the file is rooted in an <html> or <body> tag.

No, no, no.  None of that worked.

So then I tried closing the solution, deleting its .suo file (just a work file that can be regenerated), and deleting all files in the obj and bin directories.  And then, like magic, IntelliSense and validation started working again!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET | Visual Studio

Changing the case of linked embedded resources

by timvasil 11/2/2007 1:40:00 PM

If you embed a resource in Visual Studio by linking to a file and then want to adjust the case of that link, you have to do it in two places:

Open up the project file and find the link:

<EmbeddedResource Include="..\..\..\..\Yui.zip">
   <
Link>Yui.zip</Link>
</
EmbeddedResource>

Ensure that both instances of the name (in this case "Yui.zip") have the same case.  The text you specify in the "Include" attribute is what shows up in Visual Studio, however the text you specify in the <Link> element becomes the actual name of the embedded resource.  If these two aren't consistent, it'll definitely lead to confusion. Assembly.GetManifestResourceStream is case-sensitive and won't return what you expect unless you get the case right!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET Framework | Visual Studio

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments