BizTalk 2006 – Manage application references

BizTalk 2006 – Manage application references

For some reason Microsoft has forgotten or missed to add the ability of managing references between BizTalk applications to BTSTask.

Therefore I have written a small command-line tool to overcome this issue.

You can

  • list the references of an application
  • list all applications referencing a certain application
  • delete references
  • and add references to applications

using this tool.

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Management;
using System.Reflection;
using System.Text;

using ExplorerOM = Microsoft.BizTalk.ExplorerOM;

namespace ManageAppReferences
{
    class Program
    {
        static int Main(string\[] args)
        {
            string lApplication = string.Empty;
            string lRefApplication = string.Empty;
            CManageAppReferences.RefOperation lOperation =
               CManageAppReferences.RefOperation.Unknown;

            if(!ParseArguments(
                  args,
                  ref lApplication,
                  ref lRefApplication,
                  ref lOperation))
            {
                Usage(false);
                return 1;
            }

            try
            {
                Usage(true);
                CManageAppReferences lMAR = new CManageAppReferences(lApplication);
                lMAR.LogInfo += delegate(string Info) { Console.WriteLine(Info); };

                Console.WriteLine();

                switch(lOperation)
                {
                    case CManageAppReferences.RefOperation.List:
                        lMAR.List();
                        break;
                    case CManageAppReferences.RefOperation.ListBack:
                        lMAR.ListBack();
                        break;
                    case CManageAppReferences.RefOperation.Add:
                        lMAR.Add(lRefApplication);
                        break;
                    case CManageAppReferences.RefOperation.Remove:
                        lMAR.Remove(lRefApplication);
                        break;
                    default:
                        lMAR.List();
                        break;
                }
            }
            catch(Exception ex)
            {
                ConsoleColor oldColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Error: " + ex.Message);
                Console.ForegroundColor = oldColor;
                Console.WriteLine("\nCommand failed.");
                return 1;
            }

            return 0;
        }

        static bool ParseArguments(
                     string\[] Arguments,
                     ref string Application,
                     ref string RefApplication,
                     ref CManageAppReferences.RefOperation Operation)

        {
            if (Arguments == null ||
                    Arguments.Length == 0 ||
                    Arguments[0].StartsWith("/?") ||
                    Arguments[0].StartsWith("-help"))
                return false;

            Application = Arguments[0].Trim();

            if (Arguments.Length > 1)
            {
                int lColon = Arguments[1].IndexOf(":");
                string lAction =
                     (lColon > -1) ?
                     Arguments[1].Substring(0, lColon).ToLower() :
                     Arguments[1].ToLower();

                switch (lAction)
                {
                    case "listback":
                        Operation = CManageAppReferences.RefOperation.ListBack;
                        break;
                    case "list":
                        Operation = CManageAppReferences.RefOperation.List;
                        break;
                    case "add":
                        Operation = CManageAppReferences.RefOperation.Add;
                        RefApplication =
                        (lColon > -1) ?
                        Arguments[1].Substring(lColon + 1) :
                        string.Empty;
                        break;
                    case "remove":
                        Operation = CManageAppReferences.RefOperation.Remove;
                        RefApplication =
                        (lColon > -1) ?
                        Arguments[1].Substring(lColon + 1) :
                        string.Empty;
                        break;
                    default:
                        Operation = CManageAppReferences.RefOperation.List;
                        break;
                }

                if(string.IsNullOrEmpty(RefApplication) &&
                    (Operation == CManageAppReferences.RefOperation.Add ||
                    Operation == CManageAppReferences.RefOperation.Remove)
                    )
                    return false;
            }

            return true;
        }

        static void Usage(bool Small)
        {
            Console.WriteLine("\nMarcel Tiews - www.tiews.info");
            Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name +
            ": Tool to Manage references between BTS-Applications.");

            if (Small)
                return;
            Console.WriteLine("\nUsage:\n   " +
            Assembly.GetExecutingAssembly().GetName().Name +
            "  ");

            Console.WriteLine("\n   Commands:");
            Console.WriteLine(
               "      List                  - lists referenced applications (default)");
            Console.WriteLine(
               "      ListBack              - lists back referenced applications");
            Console.WriteLine(
               "      Add:     - adds reference to this application");
            Console.WriteLine(
               "      Remove:  - removes reference to this application");
            Console.WriteLine("\n");
        }
                      
        public class CManageAppReferences
        {
            public enum RefOperation { Unknown, List, ListBack, Add, Remove }
            public delegate void LogInfoHandler(string Info);
            public event LogInfoHandler LogInfo;

            private ExplorerOM.Application _Application = null;
            private ExplorerOM.BtsCatalogExplorer _Explorer = null;
            private string _DBConnString = string.Empty;

            public CManageAppReferences(string ApplicationName)
            {
                SetConnectionStringFromWMI();
                _Explorer = new Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer();
                _Explorer.ConnectionString = _DBConnString;

                _Application = GetApplication(ApplicationName);
            }

            public bool List()
            {
                OnLogInfo("References of " + _Application.Name);

                foreach (ExplorerOM.Application lApp in _Application.References)
                {
                    OnLogInfo("   " + lApp.Name);
                }
                return true;
            }

            public bool ListBack()
            {
                OnLogInfo("Back references of " + _Application.Name);

                foreach (ExplorerOM.Application lApp in _Application.BackReferences)
                {
                    OnLogInfo("   " + lApp.Name);
                }
                return true;
            }

            public bool Add(string AddApplication)
            {
                OnLogInfo("Add reference to " + AddApplication);
                ExplorerOM.Application lAddApp = GetApplication(AddApplication);
                _Application.AddReference(lAddApp);
                _Explorer.SaveChanges();
                OnLogInfo("Reference added");
                return true;
            }

            public bool Remove(string RemoveApplication)
            {
                OnLogInfo("Remove reference to " + RemoveApplication);
                ExplorerOM.Application lRemApp = GetApplication(RemoveApplication);
                _Application.RemoveReference(lRemApp);
                _Explorer.SaveChanges();
                OnLogInfo("Reference removed");
                return true;
            }

            private ExplorerOM.Application GetApplication(string ApplicationName)
            {
                ExplorerOM.Application lApp = _Explorer.Applications[ApplicationName];
                if (lApp == null)
                    throw new ApplicationException("Application \"" +
                        ApplicationName + "\" not found.");
                return lApp;
            }

            private bool SetConnectionStringFromWMI()
            {
                string lServer = string.Empty;
                string lDatabase = string.Empty;
                bool lFound = false;

                using (ManagementObjectSearcher lMOSearcher =
                     new ManagementObjectSearcher())
                {
                    lMOSearcher.Scope =
                     new ManagementScope(@"root\MicrosoftBizTalkServer");
                    lMOSearcher.Query =
                     new SelectQuery("select * from MSBTS_GroupSetting");

                    foreach (ManagementObject lMOGroupSetting in lMOSearcher.Get())
                    {
                        lServer = lMOGroupSetting["MgmtDbServerName"] as string;
                        lDatabase = lMOGroupSetting["MgmtDbName"] as string;
                        lFound = true;
                        break;
                    }
                }

                if (lFound)
                {
                    SqlConnectionStringBuilder lSB = new SqlConnectionStringBuilder();
                    lSB.DataSource = lServer;
                    lSB.InitialCatalog = lDatabase;
                    lSB.IntegratedSecurity = true;
                    _DBConnString = lSB.ToString();
                }

                return lFound;
            }

            private void OnLogInfo(string Info)
            {
                if (LogInfo != null)
                    LogInfo(Info);
            }
        }
    }
}

3 thoughts on “BizTalk 2006 – Manage application references

Leave a Reply

Your email address will not be published. Required fields are marked *