Creating BizTalk HostInstance through code
Posted
Friday, February 29, 2008 7:18 AM
by
rudolf
Following on yesterdays post about creating a host the next step is to create a host instance. This sample create the instance and start it as well.
Here is the code:
---------------------------------------------------------------
using System;
using System.Management;
namespace CreateStartHostInstance
{
class AddStartHostInstance
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string bts_WMINameSpace;
string bts_ServerAppTypeNameSpace;
string bts_HostInstanceNameSpace;
string user;
string pwd;
string hostName;
string serverName;
bts_ServerAppTypeNameSpace = "MSBTS_ServerHost";
bts_HostInstanceNameSpace = "MSBTS_HostInstance";
// WMI NameSpace for BizTalk Server
bts_WMINameSpace = @"root\MicrosoftBizTalkServer";
try
{
if (args.Length < 4)
{
ShowHelp();
}
else if (args[0] == "-?" || args[0] == "/?" || args[0] == "/h" || args[0] == "/h")
{
ShowHelp();
}
else
{
hostName = args[0];
serverName = args[1];
user = args[2];
pwd = args[3];
PutOptions options = new PutOptions();
options.Type = PutType.CreateOnly;
ManagementObject bts_AdminObjectServerHost = null;
ManagementObject bts_AdminObjectHostInstance = null;
System.Management.ObjectGetOptions bts_objOptions = new ObjectGetOptions();
// Creating instance of BizTalk Host.
ManagementClass bts_AdminObjClassServerHost = new ManagementClass(bts_WMINameSpace, bts_ServerAppTypeNameSpace, bts_objOptions);
bts_AdminObjectServerHost = bts_AdminObjClassServerHost.CreateInstance();
// Make sure to put correct Server Name,username and // password
bts_AdminObjectServerHost["ServerName"] = serverName;
bts_AdminObjectServerHost["HostName"] = hostName;
bts_AdminObjectServerHost.InvokeMethod("Map", null);
ManagementClass bts_AdminObjClassHostInstance = new ManagementClass(bts_WMINameSpace, bts_HostInstanceNameSpace, bts_objOptions);
bts_AdminObjectHostInstance = bts_AdminObjClassHostInstance.CreateInstance();
// Make Sure you correct HostName and MachineName for HostInstance name,
bts_AdminObjectHostInstance["Name"] = "Microsoft BizTalk Server " + hostName + " " + serverName;
object[] objparams = new object[3];
objparams[0] = user;
objparams[1] = pwd;
objparams[2] = true;
bts_AdminObjectHostInstance.InvokeMethod("Install", objparams);
Console.WriteLine("Host instance created successfully!");
bts_AdminObjectHostInstance.InvokeMethod("Start", null);
Console.WriteLine("Host instance started successfully!");
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
Console.ForegroundColor = ConsoleColor.White;
}
}
private static void ShowHelp()
{
Console.WriteLine("Usage: CreateStartHostInstance.exe <HostName> <ServerName> <UserName> <Password>");
}
}
}
---------------------------------------------------------------