Start and Stop Biztalk 2006 Applications
Recently I switched roles where I work and we have started to use BizTalk quite extensively for our current project. We have also made the move to Team Foundation Server and VSTS and I got the great responsibility of getting our build system up and running. Now out of the box, Team Foundation Server has a great build story, but we had the requirement of being able to deploy our BizTalk application to a remote server and start the application as well. BTSTask.exe took care of 80% of that functionality by being able to add and remove an application and the resources for the application, as well as import bindings that exist within an XML file, but one thing I couldn't seem to find was how to start and stop a BizTalk application from the command line. Luckily BTSTask itself was a .NET application and since I am not afraid of ILDASM, I decided to take a look and see how BTSTask worked under the covers. One thing to warn you about is that all the documentation for the classes that are used within BTSTask (and also in the source code below) have the claim at the top stating the classes should not be used externally and are purely for internal BizTalk use. Overall, the program was pretty simple. I know there is not really any error handling or anything like that and the command line parsing probably isn't the most robust, but the command line syntax is very similar to what already exists for BTSTask and since the code is pasted below, you are more than welcome to modify it any way you would like.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;
namespace BizTalkApplicationManagement
{
class Program
{
private static string DatabaseName = "BizTalkMgmtDb";
private static string DatabaseServer = ".";
private static string ApplicationName;
private static bool Start = false;
static void Main(string[] args)
{
ParseCommandLine(args);
if (ApplicationName == null)
{
throw new ApplicationException("ApplicationName must be passed in");
}
Microsoft.BizTalk.ApplicationDeployment.Group myGroup = new Microsoft.BizTalk.ApplicationDeployment.Group();
Application application;
myGroup.DBName = DatabaseName;
myGroup.DBServer = DatabaseServer;
BtsCatalogExplorer explorer = (BtsCatalogExplorer)myGroup.CreateInstance(typeof(BtsCatalogExplorer));
ApplicationCollection applications = explorer.Applications;
application = applications[ApplicationName];
if (application != null)
{
if (Start)
{
application.Start(ApplicationStartOption.StartAll);
}
else
{
application.Stop(Microsoft.BizTalk.ExplorerOM.ApplicationStopOption.StopAll);
}
explorer.SaveChanges();
explorer.Refresh();
}
else
{
Console.WriteLine("{0} does not exist in database [{1}] on server [{2}]", ApplicationName, DatabaseName, DatabaseServer);
}
}
static void ParseCommandLine(string[] args)
{
foreach (string currentParam in args)
{
if (currentParam.ToLower() == "start")
{
Start = true;
}
else if (currentParam.ToLower() == "stop")
{
Start = false;
}
else if (currentParam.ToLower().Contains("applicationname"))
{
ApplicationName = currentParam.Split(':')[1];
}
else if (currentParam.ToLower().Contains("server"))
{
DatabaseServer = currentParam.Split(':')[1];
}
else if (currentParam.ToLower().Contains("database"))
{
DatabaseName = currentParam.Split(':')[1];
}
}
}
}
}
You will need to add references to Microsoft.BizTalk.Admin.dll, Microsoft.BizTalk.ApplicationDeployment.Engine.dll, Microsoft.BizTalk.ExplorerOM.dll, and Microsoft.EnterpriseServices.dll.
From the command line, you would then start an application like so:
BizTalkApplicationManagement Start -ApplicationName:<NameOfBizTalkApplication> -Server:<Server> -Database:<BizTalkMgmtDb>