我可以限制IIS只运行释放代码吗?

我工作的公司有2台服务器,1个版本和1个testing/debugging服务器。 我想限制发布服务器,只有编译为发行版的代码(没有定义DEBUG常量)才能运行。

这是可能的,如果是这样,我怎么能实现这个? 我可以在IIS或web.config中设置它吗?

用一个标志与另一个标志编译时编译的程序集没有区别。 名称“Debug”和“Release”纯粹是传统的。 我假设你关心的设置是代码是否优化。 这也是不可检测的。

相反,正如@HABO所build议的那样,答案就是定义一个标志,存在于开发人员的机器上,这使得debugging代码可以被接受。 你可以使用如下简单的东西:

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(); } } }