53

I'm starting deployment of my web application and I need to guarantee that all the assemblies that are going to be deployed were built using Release configuration. Our system was developed using C#/.Net 3.5.

Is there any way to achieve this?

ps2goat
  • 8,067
  • 1
  • 35
  • 68
born to hula
  • 1,274
  • 5
  • 18
  • 36

6 Answers6

50

Check this. The idea is that you get the list of assembly attributes using Assembly.GetCustomAttributes() and search for DebuggableAttribute and then find if such attribute has IsJITTrackingEnabled property set.

    public bool IsAssemblyDebugBuild(Assembly assembly)
    {
        return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Any(da => da.IsJITTrackingEnabled);
    }
CubanX
  • 5,176
  • 2
  • 29
  • 44
David
  • 3,519
  • 1
  • 24
  • 30
  • 15
    The sarcasm isn't really needed. More often than not, the hardest part involved in searching the internet is knowing what to ask. – doogle May 01 '13 at 17:45
  • Read the article but go to the comments for better solutions: var result = assembly.GetCustomAttributes(false).OfType().Select(da => da.IsJITTrackingEnabled).FirstOrDefault(); – stevieg Nov 30 '15 at 05:51
30

I loved that David suggestion, but you could also go this way (AssemblyInfo.cs):

#if DEBUG
[assembly: AssemblyDescription("Your application assembly (DEBUG version)")]
#else if RELEASE
[assembly: AssemblyDescription("Your application assembly (RELEASE version)")]
#endif

This is more human friendly, as anyone can right-click that assembly, to select Properties and go to Details tab.

Community
  • 1
  • 1
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 2
    +1: this is actually the purpose behind this assembly directive. – Mike Post Mar 24 '12 at 23:40
  • 8
    I would recommend using AssemblyConfiguration rather than AssemblyDescription. AssemblyConfiguration is documented as "Specifies the build configuration, such as retail or debug, for an assembly." – R. Schreurs Jan 11 '13 at 16:20
  • This works, but it's not as nice as being able to detect things in code without having #IF everywhere. – ps2goat Oct 22 '13 at 19:40
  • @R.Schreurs AssemblyConfiguration doesn't show up in the DLL properties, or in ildasm, as far as I can see (windows 7). Windows 10 might show additional info, I don't know. – NibblyPig Dec 04 '19 at 08:40
11

If it is your assembly I believe using the AssemblyConfiguration attribute is the best approach. It is documented as "Specifies the build configuration, such as retail or debug, for an assembly."

Depending on your build configurations you might have code like this:

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

Then check the assembly attribute:

public static bool IsAssemblyConfiguration(Assembly assembly, string configuration)
{
    var attributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
    if (attributes.Length == 1)
    {
        var assemblyConfiguration = attributes[0] as AssemblyConfigurationAttribute;
        if (assemblyConfiguration != null)
        {
            return assemblyConfiguration.Configuration.Equals(configuration, StringComparison.InvariantCultureIgnoreCase);
        }
    }
    return true;
}

(I know R. Schreurs comment at Rubens Farias says the same, but I've find this information somewhere else before seeing the comment so I believe this requires a more important entry like a full response instead of a comment)

CubanX
  • 5,176
  • 2
  • 29
  • 44
Guillermo Ruffino
  • 2,940
  • 1
  • 26
  • 23
2

Assuming only Debug and Release configuration, DEBUG symbol is by default defined with Debug configuration, so the code below in AssemblyInfo.cs (under Properties folder).

#if DEBUG
[assembly: AssemblyTitle("Debug")]
#else
[assembly: AssemblyTitle("Release")]
#endif

I use AssemblyTitle over AssemblyDescription as it will show up on my Windows 7 file explorer properties:

DLL File properties

For those who like David and stevieg's answer, here is a LINQPad script written in C#. To use the script, you need to download LINQPad 5 and make sure C# Program is selected as shown in screenshot below.

Simply replace DLL_FOLDER_PATH to point to folder containing the DLLs to be inspected.

// TODO - Specify your folder containing DLLs to inspect
static string DLL_FOLDER_PATH = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0";
void Main()
{
    (from dllPath in Directory.GetFiles(DLL_FOLDER_PATH, "*.dll")
    let assembly = dllPath.SafeLoad()
    let build = assembly == null ? "Error" : (dllPath.SafeLoad().IsAssemblyDebugBuild() ? "Debug" : "Release")
    select new {
        Assembly_Path = dllPath,
        Build = build,
    }).Dump();
}
static class Extensions {
    public static bool IsAssemblyDebugBuild(this Assembly assembly)
    {
        return assembly.GetCustomAttributes(false).OfType<DebuggableAttribute>().Select(da => da.IsJITTrackingEnabled).FirstOrDefault();
    }
    public static Assembly SafeLoad(this string path){
        try{
            return Assembly.LoadFrom(path);
        }
        catch {
            return null;
        }
    }
}

Checking release or debug build using LINQPad5

LINQPAD 5 can be downloaded here.

Chris Voon
  • 1,974
  • 2
  • 18
  • 11
2

If you have Reflector installed you can also click on the assembly and look for the debuggable attribute ([assembly: Debuggable()]) in the Disassembler pane.

xr280xr
  • 12,621
  • 7
  • 81
  • 125
0

Don't deploy to production via Visual Studio. Look into Continuous Integration and scripted builds (such as with NAnt, or perhaps something more legible like FAKE).

The F5 Key Is Not a Build Process

To detractors who believe that this does not answer the question, the OP wrote:

...I need to guarantee that all the assemblies that are going to be deployed were built using Release configuration.

To guarantee that, use a build server such as TeamCity and possibly a release management tool like Octopus Deploy. Lock down your production systems so that developers must go through the official build process.

TrueWill
  • 25,132
  • 10
  • 101
  • 150
  • 2
    This does not answer the question in any way. It is a good idea to have a build process, this is not an answer. – jasonmw Oct 21 '16 at 17:51