Tallan Blog

Tallan’s Experts Share Their Knowledge on Technology, Trends and Solutions to Business Challenges

Saving BizTalk Suspended Messages in Bulk using WMI

BizTalk is great in its ability to store messages within the MessageBox even after processing failure, however its not always so easy to get those messages back out!   I recently had a need to retrieve all messages that were currently suspended due to validation failures while attempting to generate X12 EDI messages using the Microsoft.Practices.ESB.

The solution is to use Windows Management Instrumentation (WMI) which can be used within PowerShell or C# to interface directly with BizTalk and easily perform administrative tasks programmatically.  For this scenario we will use C#.

The first step is to create a new console application and open up Server Explorer.

Next, right click on ‘Management Classes’ and select ‘Add Classes…’


Next add MSBTS_HostInstanceSetting, and MSBTS_MessageInstance


After the two WMI classes are added, we have to select them within Server Explorer\Servers\Management Classes\ and select ‘Generate Managed Class’ for both HostInstance and MessageInstance


This will create both classes within your solution


Below is my sample code that will first retrieve all suspended instances filtered on a specific message status.

ROOT.MICROSOFTBIZTALKSERVER.ServiceInstance.ServiceInstanceCollection instanceCollection =
ROOT.MICROSOFTBIZTALKSERVER.ServiceInstance.GetInstances(String.Format(“ServiceStatus='{0}'”, 4));

The message statuses possible are

  • 1 – Ready to run
  • 2 – Active
  • 4 – Suspended (resumable)
  • 8 – Dehydrated
  • 16 = Zombies (completed with discarded messages)
  • 32 – Suspended (not resumable)
  • 64 – In Breakpoint

Next, we will start to iterate through each suspended instance in the collection

For each host instance, we will then extract and iterate over each instance message

ROOT.MICROSOFTBIZTALKSERVER.MessageInstance.MessageInstanceCollection messageCollection =
String.Format(“ServiceInstanceID='{0}'”, instance.InstanceID));

Lastly, we will save each message to the file system.


Below is the full codebase:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ExtractSuspendedMessages




// sample arguments: ExtractSuspendedMessages “C:\temp\suspendedmsessages” false

staticvoid Main(string[] args)


if (args.Length != 2)

Console.WriteLine(“Require 2 inputs.”);


ExtractAllMessages(args[0], Convert.ToBoolean(args[1]));


privatestaticvoid ExtractAllMessages(string folderLoc, bool terminateInstancesAfterSaving = false)



// Use integer to filter for specific suspended service statuses

//1 – Ready to run

//2 – Active

//4 – Suspended (resumable)

//8 – Dehydrated

//16 = Zombies (completed with discarded messages)

//32 – Suspended  (not resumable)

//64 – In Breakpoint

ROOT.MICROSOFTBIZTALKSERVER.ServiceInstance.ServiceInstanceCollection instanceCollection =

ROOT.MICROSOFTBIZTALKSERVER.ServiceInstance.GetInstances(String.Format(“ServiceStatus='{0}'”, 4));

//Loop through each service instance

foreach (ROOT.MICROSOFTBIZTALKSERVER.ServiceInstance instance in instanceCollection)


Console.WriteLine(String.Format(“Saving suspended messages for GUID: {0} with error: {1} to folder {2}”,

instance.InstanceID, instance.ErrorDescription, folderLoc));

ROOT.MICROSOFTBIZTALKSERVER.MessageInstance.MessageInstanceCollection messageCollection =


String.Format(“ServiceInstanceID='{0}'”, instance.InstanceID));

//Loop through each message

foreach (ROOT.MICROSOFTBIZTALKSERVER.MessageInstance message in messageCollection)


Console.WriteLine(String.Format(“Saving Message with ID: {0}”, message.MessageInstanceID));





catch (Exception) { }


if (terminateInstancesAfterSaving)






Running the console application with input variables “C:\temp\messages” and false









Learn more about Tallan or see us in person at one of our many Events!

Share this post:

2 Comments. Leave new

Thanks a lot for this post, it was very helpful! One remark though, one needs to generate the managed class for serviceInstances instead of hostInstances. Which is done correctly in the code and image, but the text mentions something different.

Hi, Thank you soo much for the article. Its very useful. Can we publish this messages back to BizTalk message box without saving to a file since my orchestration needs to subscribe to these messages

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>