/BiztalkConfigLoader/BiztalkConfigLoader/BtsAdministration.cs

# · C# · 243 lines · 213 code · 20 blank · 10 comment · 28 complexity · ff3ab5ba2dfb6cb1cbf0fd9f70251e84 MD5 · raw file

  1. using System;
  2. using System.Management;
  3. namespace BiztalkConfigLoader
  4. {
  5. public class BtsAdministration
  6. {
  7. #region Private constants
  8. private const string bts_WMINameSpace = @"root\MicrosoftBizTalkServer";
  9. private const string bts_HostSettingNameSpace = "MSBTS_HostSetting";
  10. private const string bts_ServerAppTypeNameSpace = "MSBTS_ServerHost";
  11. private const string bts_HostInstanceNameSpace = "MSBTS_HostInstance";
  12. private const string bts_AdapterSettingNameSpace = "MSBTS_AdapterSetting";
  13. private const string bts_ReceiveHandlerNameSpace = "MSBTS_ReceiveHandler";
  14. private const string bts_SendHandlerNameSpace = "MSBTS_SendHandler2";
  15. #endregion
  16. #region Hosts
  17. public static void AddHost(string hostName, string ntGroupName, bool isDefault, bool hostTracking, bool authTrusted, int hostType, bool isHost32BitOnly)
  18. {
  19. PutOptions options = new PutOptions();
  20. options.Type = PutType.CreateOnly;
  21. ManagementObject bts_AdminObject;
  22. // Creating instance of BizTalk Host.
  23. ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_HostSettingNameSpace, new ObjectGetOptions());
  24. bts_AdminObject = bts_AdminObjClass.CreateInstance();
  25. ManagementObject appObject = bts_AdminObject;
  26. // Assigning Host Properties.
  27. appObject["Name"] = hostName;
  28. appObject["NTGroupName"] = ntGroupName;
  29. appObject["IsDefault"] = isDefault;
  30. appObject["HostTracking"] = hostTracking;
  31. appObject["AuthTrusted"] = authTrusted;
  32. appObject["HostType"] = hostType;
  33. appObject["IsHost32BitOnly"] = isHost32BitOnly;
  34. appObject.Put(options);
  35. }
  36. public static void RemoveHost(string hostName)
  37. {
  38. ManagementClass bts_AdminObjClass = new ManagementClass(bts_WMINameSpace, bts_HostSettingNameSpace, new ObjectGetOptions());
  39. if (ObjectExists(bts_AdminObjClass, "Name", hostName))
  40. {
  41. ManagementObject bts_AdminObject = bts_AdminObjClass.CreateInstance();
  42. ManagementObject appObject = bts_AdminObject;
  43. appObject["Name"] = hostName;
  44. appObject.Delete();
  45. }
  46. }
  47. #endregion
  48. #region Host instances
  49. public static void AddHostInstance(string servername, string hostname, string username, string password, bool startInstance)
  50. {
  51. //PutOptions options = new PutOptions();
  52. //options.Type = PutType.CreateOnly;
  53. ManagementObject bts_AdminObjectServerHost;
  54. ManagementObject bts_AdminObjectHostInstance;
  55. // Creating instance of BizTalk Host.
  56. ManagementClass bts_AdminObjClassServerHost = new ManagementClass(bts_WMINameSpace, bts_ServerAppTypeNameSpace, new ObjectGetOptions());
  57. bts_AdminObjectServerHost = bts_AdminObjClassServerHost.CreateInstance();
  58. bts_AdminObjectServerHost["ServerName"] = servername;
  59. bts_AdminObjectServerHost["HostName"] = hostname;
  60. bts_AdminObjectServerHost.InvokeMethod("Map", null);
  61. ManagementClass bts_AdminObjClassHostInstance = new ManagementClass(bts_WMINameSpace, bts_HostInstanceNameSpace, new ObjectGetOptions());
  62. bts_AdminObjectHostInstance = bts_AdminObjClassHostInstance.CreateInstance();
  63. bts_AdminObjectHostInstance["Name"] = "Microsoft BizTalk Server " + hostname + " " + servername;
  64. object[] objparams = new object[3];
  65. objparams[0] = username;
  66. objparams[1] = password;
  67. objparams[2] = true;
  68. bts_AdminObjectHostInstance.InvokeMethod("Install", objparams);
  69. if (startInstance)
  70. {
  71. bts_AdminObjectHostInstance.InvokeMethod("Start", null);
  72. }
  73. }
  74. public static void RemoveHostInstance(string servername, string hostname)
  75. {
  76. string hostInstanceName = "Microsoft BizTalk Server " + hostname + " " + servername;
  77. ManagementClass hostInstClass = new ManagementClass(bts_WMINameSpace, bts_HostInstanceNameSpace, new ObjectGetOptions());
  78. EnumerationOptions enumOptions = new EnumerationOptions();
  79. enumOptions.ReturnImmediately = false;
  80. ManagementObjectCollection hostInstCollection = hostInstClass.GetInstances(enumOptions);
  81. ManagementObject hostInstance = null;
  82. foreach (ManagementObject inst in hostInstCollection)
  83. {
  84. if (inst["Name"] != null)
  85. {
  86. if (inst["Name"].ToString().ToUpper() == hostInstanceName.ToUpper())
  87. {
  88. hostInstance = inst;
  89. }
  90. }
  91. }
  92. if (hostInstance == null)
  93. return;
  94. if (hostInstance["HostType"].ToString() != "2" && hostInstance["ServiceState"].ToString() == "4")
  95. {
  96. hostInstance.InvokeMethod("Stop", null);
  97. }
  98. //Now UnInstall the HostInstance
  99. hostInstance.InvokeMethod("UnInstall", null);
  100. //Create an instance of the ServerHost class using the System.Management namespace
  101. ManagementClass svrHostClass = new ManagementClass(bts_WMINameSpace, bts_ServerAppTypeNameSpace, new ObjectGetOptions());
  102. ManagementObject svrHostObject = svrHostClass.CreateInstance();
  103. //Set the properties of the ServerHost instance
  104. svrHostObject["ServerName"] = servername;
  105. svrHostObject["HostName"] = hostname;
  106. //Invoke the UnMap method of the ServerHost object
  107. svrHostObject.InvokeMethod("UnMap", null);
  108. }
  109. #endregion
  110. #region Adapters and handlers
  111. public static void AddAdapter(string name, string type, string comment)
  112. {
  113. ManagementObject objInstance = null;
  114. ManagementClass objClass = new ManagementClass(bts_WMINameSpace, bts_AdapterSettingNameSpace, new ObjectGetOptions());
  115. EnumerationOptions enumOptions = new EnumerationOptions();
  116. enumOptions.ReturnImmediately = false;
  117. ManagementObjectCollection hostInstCollection = objClass.GetInstances(enumOptions);
  118. foreach (ManagementObject inst in hostInstCollection)
  119. {
  120. if (inst["Name"] != null)
  121. {
  122. if (inst["Name"].ToString().ToUpper() == name.ToUpper())
  123. {
  124. objInstance = inst;
  125. }
  126. }
  127. }
  128. if (objInstance == null)
  129. {
  130. objInstance = objClass.CreateInstance();
  131. objInstance.SetPropertyValue("Name", name);
  132. objInstance.SetPropertyValue("MgmtCLSID", type);
  133. objInstance.SetPropertyValue("Comment", comment);
  134. }
  135. try
  136. {
  137. PutOptions options = new PutOptions() { Type = PutType.UpdateOrCreate };
  138. objInstance.Put(options);
  139. }
  140. catch (Exception)
  141. {
  142. return;
  143. }
  144. }
  145. public static void RemoveAdapter(string name)
  146. {
  147. ManagementObject objInstance = null;
  148. ManagementClass objClass = new ManagementClass(bts_WMINameSpace, bts_AdapterSettingNameSpace, new ObjectGetOptions());
  149. //Leave the adapter if there are other instances depending on it.
  150. if (objClass.GetInstances().Count > 0)
  151. return;
  152. objInstance = objClass.CreateInstance();
  153. objInstance.SetPropertyValue("Name", name);
  154. if (ObjectExists(objClass, "Name", name))
  155. {
  156. objInstance.Delete();
  157. }
  158. }
  159. public static void AddReceiveHandler(string adaptername, string hostname)
  160. {
  161. ManagementObject objInstance;
  162. ManagementClass objClass = new ManagementClass(bts_WMINameSpace, bts_ReceiveHandlerNameSpace, new ObjectGetOptions());
  163. objInstance = objClass.CreateInstance();
  164. objInstance.SetPropertyValue("AdapterName", adaptername);
  165. objInstance.SetPropertyValue("HostName", hostname);
  166. objInstance.Put();
  167. }
  168. public static void RemoveReceiveHandler(string adaptername, string hostname)
  169. {
  170. ManagementObject objInstance;
  171. ManagementClass objClass = new ManagementClass(bts_WMINameSpace, bts_ReceiveHandlerNameSpace, new ObjectGetOptions());
  172. if (ObjectExists(objClass, "HostName", hostname, "AdapterName", adaptername))
  173. {
  174. objInstance = objClass.CreateInstance();
  175. objInstance.SetPropertyValue("AdapterName", adaptername);
  176. objInstance.SetPropertyValue("HostName", hostname);
  177. objInstance.Delete();
  178. }
  179. }
  180. public static void AddSendHandler(string adaptername, string hostname)
  181. {
  182. ManagementObject objInstance;
  183. ManagementClass objClass = new ManagementClass(bts_WMINameSpace, bts_SendHandlerNameSpace, new ObjectGetOptions());
  184. objInstance = objClass.CreateInstance();
  185. objInstance.SetPropertyValue("AdapterName", adaptername);
  186. objInstance.SetPropertyValue("HostName", hostname);
  187. objInstance.Put();
  188. }
  189. public static void RemoveSendHandler(string adaptername, string hostname)
  190. {
  191. ManagementObject objInstance;
  192. ManagementClass objClass = new ManagementClass(bts_WMINameSpace, bts_SendHandlerNameSpace, new ObjectGetOptions());
  193. if (ObjectExists(objClass, "HostName", hostname, "AdapterName", adaptername))
  194. {
  195. objInstance = objClass.CreateInstance();
  196. objInstance.SetPropertyValue("AdapterName", adaptername);
  197. objInstance.SetPropertyValue("HostName", hostname);
  198. objInstance.Delete();
  199. }
  200. }
  201. #endregion
  202. #region ObjectExists
  203. private static bool ObjectExists(ManagementClass objClass, string key, string value)
  204. {
  205. foreach (ManagementObject obj in objClass.GetInstances())
  206. {
  207. if (obj[key].ToString() == value)
  208. return true;
  209. }
  210. return false;
  211. }
  212. private static bool ObjectExists(ManagementClass objClass, string key, string value, string key2, string value2)
  213. {
  214. foreach (ManagementObject obj in objClass.GetInstances())
  215. {
  216. if (obj[key].ToString() == value && obj[key2].ToString() == value2)
  217. return true;
  218. }
  219. return false;
  220. }
  221. #endregion
  222. }
  223. }