.NET – Determine Whether an Assembly was compiled in Debug Mode

Finding out whether an assembly was compiled in Debug or Release mode is a task we must do from time to time.

I know two ways of accomplish this:

Either attributes are applied to Assemblies and can be found in the Assembly Manifest but there are a major difference between them:

  • AssemblyConfigurationAttribute must be added by the programmer but is human readable.
  • DebuggableAttribute is added automatically and is always present but is not human readable

You can easily get the Assembly Manifest by using the amazing ILDASM from your Visual Studio Studio Command Prompt:

ildasm_1

ildasm_2

And if you double click the MANIFEST item you will get all manifest data.

Looking carefully you will find the DebuggableAttribute:

ildasm_3

And perhaps the AssemblyConfigurationAttribute:

ildasm_4 

AssemblyConfigurationAttribute

Locate the AssemblyConfigurationAttribute – this attribute can be easily interpreted: its value can either be Debug or Release

ildasm_5

DebuggableAttribute

If AssemblyConfigurationAttribute is not present then we must use the DebuggableAttribute to get our goal.

Since we cannot understood the DebuggableAttribute value we have to open the assembly from another tool and read this attribute content. There’s no such tool available out-of-the-box but we can easily create a Command Line tool and use a method similar to:

private bool IsAssemblyDebugBuild(string filepath) {
    return IsAssemblyDebugBuild(Assembly.LoadFile(Path.GetFullPath(filepath)));
}
private bool IsAssemblyDebugBuild(Assembly assembly) {
    foreach (var attribute in assembly.GetCustomAttributes(false)) {
        var debuggableAttribute = attribute as DebuggableAttribute;
        if (debuggableAttribute != null) {
            return debuggableAttribute.IsJITTrackingEnabled;
		}

    return false;
}

or (if you prefer LINQ)

private bool IsAssemblyDebugBuild(Assembly assembly) {
    return assembly.GetCustomAttributes(false).Any(x => (x as DebuggableAttribute) != null ? (x as DebuggableAttribute).IsJITTrackingEnabled : false);
}

As you can see … it’s pretty simple.

Note:

Tipically I add a pre-build task to my build server so that the AssemblyConfigurationAttribute is added to the CommonAssemblyInfo file with the appropriated value: either “Debug” or “Release”. This way anyone can easily see, using only the ILDASM, which king of compilation was used to generate my assemblies.

ASP.NET – Converting C# String to JSON String

Most web applications display messages to users. Displaying messages is the most effective way to inform user about errors and warnings or to simply display info or success status.

I also believe that most of those web applications renders user messages using HTML elements such as Div, Span or Label.

The others, mainly because their complex layout or business rules, have to render those messages using a different approach: they render each display message as a Javascript function call where the message itself is a function parameter.  Such call could be simply like this:

string.Format("top.addMessage("{0}");", message)

Those of you that already faced this problem won’t learn nothing new but I hope to alert the others and avoid them to face a problem that usually is only detected in advanced develop stage or even production environment.

Problem

The problem turns visible when the display message contains some special characters that prevent the function call to execute and trigger javascript errors.

Typically the characters are “ and ‘ but there are more.

Well, that is not completely correct, only the character “ is problematic, since he is the JSON string delimiter character, but, javascript engines also accept the character ‘ as a delimiter. The best way to avoid problems with ‘ is to ensure that the correct delimiter is used.

By now, you should be thinking that your applications are immune to this problem. Tell me, do you use string Resources? Most of us use, it’s a best practice and a smart choice. Also tell me, do you manage production string Resources? Most of us don’t. In fact, the application owner or sponsor can change it completely outside of your control scope.

String Resources are one of the major vectors of this problem but you can get this problem simply by trying to display an exception message:

string.Format("top.addMessage("{0}");", exception.ToString())

Solution

What we are needing is an JScript.Encode method or similar that encode a C# string in such a way that it becomes a JSON encoded string.

Such method is not available on NetFX but, fortunately, Rick Strahl made a great post about this problem and made available a method that fits perfectly our needs.

Now that you know about it … use it.