1

The company I work for has 2 servers, 1 release and 1 test/debug server. I want to restrict the release server that only code that is compiled as release (not having the DEBUG constant defined) can be run.

Is this possible and if so, how can I achive this? Can I set this in IIS or in the web.config?

  • Would it be acceptable to do the perverse, i.e. add a value to the machine configuration on the release server that your applications check on startup? If they detect a conflict between the server, their build status and any other business rules you care to define, they throw an exception and exit. – HABO Aug 17 '13 at 14:15
  • I could work with that... thanks. Ill think about it. – SynerCoder Aug 17 '13 at 14:25
2

There is no difference in the compiled assemblies when you compile with one flag versus another. The names "Debug" and "Release" are purely conventional. The setting I assume you are concerned about is whether the code is optimized or not. That too is not detectable.

Instead, as @HABO suggested, the answer would be to define a flag, present on developer's machines, which makes debug code acceptable. You could use something as simple as the following:

static class AssertProductionOptimized
{
    private static bool checkCompleted = false;
    private static bool isDeveloper = false;

    private const string regPath = @"Software\My Awesome Software, Inc";
    private const string regValue = "IsDeveloper";

    [Conditional("DEBUG")]
    public static void AssertOptimized()
    {
        if (checkCompleted)
        {
            isDeveloper = checkIfDeveloper();
            checkCompleted = true;
        }

        if (!isDeveloper)
        {
            throw new InvalidOperationException(string.Format("Debug code running "
                + "on non-developer machine.  Either build without DEBUG flag, or "
                + "add a DWORD named {1} with a value of 1 to HKLM\\{0}", 
                regPath, regValue));
        }
    }

    private static void checkIfDeveloper()
    {
        RegistryKey hkKey = null;
        try 
        {
            hkKey = Registry.LocalMachine.OpenSubKey(regPath);

            // if the key does not exist, we are not a developer
            if (hkKey == null)
                return false;

            var hkValueObj = hkKey.GetValue(regValue);

            return object.Equals(hkValueObj, 1);
        }
        catch (Exception ex)
        { 
            throw new Exception("Exception occurred while checking developer status", ex);
        }
        finally
        {
            if (hkKey != null)
                hkKey.Dispose();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.