Determine whether the BizTalk assembly is Debug or Release build at runtime.

Posted at: 6/2/2007 at 7:56 AM by saravana

Recently someone raised this question in the newsgroup, they wanted to branch inside the orchestration based on the build of the assembly itself. Whenever an assembly is build with "Debug" mode, some System.Diagnostics.DebuggableAttributes are added to the assembly. One such attribute is "IsJITTrackingEnabled", which will track information during code generation (MSIL) for the debugger.

So, we can use the simple technique of reflection to determine whether the assembly is build against "Debug" or "Release" mode by the presence or absence of IsJITTrackingEnabled attribute. The below method does exactly the same:

public static bool IsDebugBuild(System.Reflection.Assembly assembly)
{
object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), false);

if (attributes.Length == 0)
return false;//No debug attibutes, so release build

foreach (Attribute attr in attributes)
{
if (attr is DebuggableAttribute)
{
DebuggableAttribute d = attr as DebuggableAttribute;
if (d.IsJITTrackingEnabled == true) //this flag is set only for debug builds
return true; //Debug
else
return false; //Release
}
}
throw new Exception("Cannot determine the build type");
}

Place the above code in an utility class (external assembly), build and GAC it. Inside your Orchestration, reference the assembly, define a variable (ex: assm) of type "System.Reflection.Assembly" and call the method IsDebugBuild as shown below inside your Expression shape

assm = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.Debug.WriteLine(Utility.DetermineDebugOrRelease.IsDebugBuild(assm));

Place the above code in an utility class (external assembly), build and GAC it. Inside your Orchestration, reference the assembly, define a variable (ex: assm) of type "System.Reflection.Assembly" and call the method IsDebugBuild as shown below inside your Expression shape

assm = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.Debug.WriteLine(Utility.DetermineDebugOrRelease.IsDebugBuild(assm));

NOTE: This example is just to address someone's question. You should try NOT to use this type of code in production. Because reflection is an expensive process and it doesn't really suits well for high throughput BizTalk applications.

Nandri!
Saravana

Tags:  Categories: .NET | BizTalk General
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(11) | Comments RSS

Comments

Thursday, June 07, 2007 12:11 PM
Anonymous
Anonymous
Can't compiler flags be used?

#if DEBUG
  this.DoSomething();
#endif
Thursday, July 02, 2009 10:39 AM
irfan India
irfan
tats interseting to know tat but i was unable to figure out where and which senario will be used!
Thursday, July 02, 2009 11:19 AM
saravana
As I mentioned in the bottom of the post, I won't recommend using this approach on a BizTalk solution for performance reason. One reason I can think of where it might have helped is: Say if you want to write lot of debug trace/warning information if the assembly is in debug version and you don't want to do that on a proper release/production version.

it will come down to what type of operation you are doing inside the debug block.
Monday, August 02, 2010 1:02 AM
lenen
Lenen Zonder BKR Toetsing Lenen zonder BKR toetsing stijgt in populariteit op het Internet. Veel mensen met een zogeheten BKR notatie, die toch geld willen lenen zijn op zoek naar ...
Monday, August 02, 2010 4:58 AM
lenen
BKR problemen? Nu Geld lenen zonder BKR toetsing? Op zoek naar betrouwbare aanbieders? Wij vergelijken banken die u toch kunnen helpen aan een betrouwbare
Thursday, August 05, 2010 1:44 AM
Seattle Limos
Good read. You ok if i add this info to my blog?
Saturday, August 07, 2010 3:52 PM
Sex Cam
I will post a link to this page on my blog. I am sure my visitors will find that very useful.
Saturday, August 07, 2010 4:19 PM
Sex Chat
I love viewing this blogs articles, keep up the amazing work!
Tuesday, August 10, 2010 12:24 AM
hypotheek
Hoeveel kan ik lenen? (hypotheek). Wat worden mijn maandlasten? (hypotheek) ... Hoeveel hypotheek heb ik nodig? Hoe hoog is de boete die ik nu zou moeten
Tuesday, August 10, 2010 7:57 AM
hypotheek
Hypotheek informatie, hypotheek aanvragen of afsluiten? Hypotheekrentes bekijken. Hypotheek aanbieders vergelijken, hypotheek vormen, bijkomende kosten,
Thursday, August 12, 2010 12:58 PM
quick student loan
There is no greatness without a passion to be great, whether it's the aspiration of an athlete or an artist, a scientist, a parent, or a businessperson.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading