BizTalk Trick: Display Debug Messages in an Orchestration (MessageBox)
I agree: the Health and Activity Tracker tool of BizTalk 2004 is the source of information if something goes wrong in BizTalk. But sometimes (for example during development) it's quite a hassle to fire up the Health and Activity Tracker, find your message and navigate to the Orchestration Debugger, especially since this tool is not (yet?) integrated in Visual Studio.NET.
So what does a developer typically do if he/she needs to get some debug information from a Windows.Forms application while the application is running? Will he/she set a breakpoint, fire up the application and enter an expression in the Command Window? Maybe... Probably he/she will quickly add a MessageBox.Show(something) in the code and fire up the application, right? It would be nice if we could do the same in a BizTalk Orchestration! But there is one small problem: the BizTalk processes aren't running with the identity of the currently logged on user; so the MessageBox won't be visible for that interactive user by default. Luckily you can change this behaviour by using MessageBoxOptions.ServiceNotifcation option when invoking the MessageBox.Show method. From the MSDN Library:
ServiceNotification: The message box is displayed on the active desktop. The caller is a service notifying the user of an event. The function displays a message box on the current active desktop, even if there is no user logged on to the computer.
I'm using following class which I've put in a class library that's deployed in the GAC, so the class can be called very easy from an Expression shape in an Orchestration once you've added a reference to the assembly. The expression could be something like this (you don't need an instance of the MessageBox class, the Show method is declared static):
Ordina.BizTalk.Debug.MessageBox.Show("OK!");
Here's the complete code. Oh, by the way, please do not forget these expressions when you put your project in a production environment! :-)
using System;
using System.Windows.Forms;
namespace Ordina.BizTalk.Debug
{
/// <summary>
/// Helper class to display debug information in BizTalk orchestrations.
/// </summary>
public class MessageBox
{
private MessageBox()
{ /* Can not create instance! */ }
static public void Show(string text, string title,
MessageBoxButtons buttons, MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton)
{
System.Windows.Forms.MessageBox.Show(
text,
title,
buttons,
icon,
defaultButton,
MessageBoxOptions.ServiceNotification);
}
static void Show(string text, string title)
{
Show(
text,
title,
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
}
static public void Show(string text)
{
Show(text, "Ordina.BizTalk.Debug Information");
}
}
}