Delete BizTalk Artifacts within C#


When you are developing and testing BizTalk projects, one of the most common tasks is to cleanup BizTalk server from suspended items, running service instances and so on.

A commonly used method achieve this is to use the following powershell script:

Import-module “C:\Program Files (x86)\Microsoft BizTalk Server 2013 R2\SDK\Utilities\PowerShell\BizTalkFactory.PowerShell.Extensions.dll”

Get-ChildItem –Path ‘BizTalk:\Health and Activity\Service Instances’ | % { $_.Terminate() }

This method is very convenient compared to the standard method of deleting all the artifacts in the BizTalk Server Administration Console.

In automated test scenarios it is often necessary to reset the test environment to its initial state. You can use the following code to create a test step to achieve this.

string scope = @"root\MicrosoftBizTalkServer";
/*
  ServiceClass possible values:
    Orchestration 1
    Tracking 2
    Messaging 4
    MSMQT 8
    Other 16
    Isolated adapter 32
    Routing failure report 64
*/
SelectQuery query = new SelectQuery("SELECT * FROM MSBTS_ServiceInstance Where ServiceClass = 1 or ServiceClass = 4 or ServiceClass = 64");

ManagementScope mScope = new ManagementScope(scope);

ManagementObjectSearcher searcher = new ManagementObjectSearcher(mScope, query);

ManagementObjectCollection btsArtifacts = searcher.Get();

foreach (ManagementObject mobj in btsArtifacts)
{

  mobj.InvokeMethod("Terminate", null);
}

Another very useful code snippet is the following. It bounces all BizTalk host instances on the local machine. We use this in our automated tests scenarios where it is necessary to restart all instances after a configuration change.

string scope = @"root\MicrosoftBizTalkServer";
/*
                ServiceState = 4 (= running)
*/

var query = new SelectQuery("SELECT * FROM MSBTS_HostInstance WHERE ServiceState = 4");
ManagementScope mScope = new ManagementScope(scope);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(mScope, query);
ManagementObjectCollection btsArtifacts = searcher.Get();
foreach (var mobj in btsArtifacts.Cast<ManagementObject>())
{
   mobj.InvokeMethod("Stop", null);
   mobj.InvokeMethod("Start", null);
}


Comments (1)
  1. martinism says:

    what are the other tasks when you are developing and testing BizTalk projects

Comments are closed.

Skip to main content