IISReset for BizTalk
admin This small tool works like the IISReset for IIS. You can start/stop the BizTalk hosts from the command prompt.
The code is simple an easy customizable.
using System;
using System.Reflection;
using System.Collections;
using System.Management;
namespace BizTalk.Tools.Reset
{
class CProgram
{
[STAThread]
static int Main(string[ ] args)
{
CReset.ResetAction lAction=0;
if(args.Length!=1) { Usage(); return 0; }
else if(args[0].ToLower().CompareTo(”start”)==0)
{ lAction=CReset.ResetAction.Start; }
else if(args[0].ToLower().CompareTo(”stop”)==0)
{ lAction=CReset.ResetAction.Stop; }
else if(args[0].ToLower().CompareTo(”restart”)==0)
{ lAction=CReset.ResetAction.Restart; }
else { Usage(); return 0; }
CReset lReset = new CReset();
lReset.LogInfo += new CReset.LogInfoHandler(WriteInfo);
try
{
lReset.Reset(string.Empty,lAction);
}
catch(Exception ex)
{
Console.WriteLine(”\n!!! Error occured: {0} !!!\n”,ex.Message );
return 1;
}
return 0;
}
static void Usage()
{
Console.WriteLine(
“\nTool to Stop/Start/Restart BizTalk-HostInstances.”);
Console.WriteLine(
“\nusage:\n ” + Assembly.GetExecutingAssembly().GetName().Name +
” [START|STOP|RESTART]“);
}
static void WriteInfo(string Info)
{
Console.WriteLine(Info);
}
}
class CReset
{
public delegate void LogInfoHandler(string Info);
public event LogInfoHandler LogInfo;
[Flags]
public enum ResetAction { Start=1, Stop=2, Restart=Start|Stop }
private enum HostState
{ Stopped=1, StartPending=2, StopPending=3, Running=4, Unknown=8 }
public CReset() {}
public void Reset(string Server, ResetAction Action )
{
OnLogInfo(”Action:” + Action.ToString());
ManagementScope lMScope;
if(Server.Length>0)
{
lMScope = new ManagementScope(
“\\\\” + Server + “\\root\\MicrosoftBizTalkServer”);
}
else
{
lMScope = new ManagementScope(@”root\MicrosoftBizTalkServer”);
}
// HostTypes: In-proc=1 / Isolated=2
ObjectQuery lQuery = new ObjectQuery(
“select * from MSBTS_HostInstance where HostType=1″);
using(ManagementObjectSearcher
lSearcher = new ManagementObjectSearcher(lMScope,lQuery))
{
ManagementObjectCollection ReturnCollection = lSearcher.Get();
foreach (ManagementObject lHostInstance in ReturnCollection)
{
// States: Stopped=1 / Start pending=2 /
// Stop pending=3 / Running=4 / Unknown=8
HostState State=HostState.Unknown;
switch ((uint)lHostInstance["ServiceState"])
{
case 1: State=HostState.Stopped; break;
case 2: State=HostState.StartPending; break;
case 3: State=HostState.StopPending; break;
case 4: State=HostState.Running;
break; default: State=HostState.Unknown; break;
}
OnLogInfo(
String.Format(”HostInstance <{0}> on Server <{1}> -> {2}”,
lHostInstance.Properties["HostName"].Value,
lHostInstance.Properties["RunningServer"].Value,State));
if(State!=HostState.Stopped &&
(Action & ResetAction.Stop)!=0)
{
OnLogInfo(” Stopping…”);
lHostInstance.InvokeMethod(”Stop”,null);
OnLogInfo(” Stopped.”);
State=HostState.Stopped;
}
if(State!=HostState.Running &&
(Action & ResetAction.Start)!=0)
{
OnLogInfo(” Starting…”);
lHostInstance.InvokeMethod(”Start”,null);
OnLogInfo(” Started.”);
}
}
}
}
private void OnLogInfo(string Info)
{
if(LogInfo!=null)
LogInfo(Info);
}
}
}
Posted in Biztalk, Development |