synBoogaloo - BizTalk/.Net 3.0

Why is it drug addicts and computer afficionados are both called users?

  Home  |   Contact  |   Syndication    |   Login
  28 Posts | 0 Stories | 27 Comments | 81 Trackbacks

News

Archives

Post Categories

Image Galleries

Blog Role

Organisations

Photo Album

I wrote this little utility almost 2 years ago to speed up deployment and binding, enlisting, enabling and starting BizTalk components in a development environment and then to perform the uninstall. It works well on simple BizTalk projects. With more complex BizTalk projects I would recommend using Nant. My colleges and I now never use msi's for installs\uninstalls as they are harder to debug when a deployment fails and less configurable than Nant which is also open source. I know my posting of this is a little late as it's now easier to perform these functions in BizTalk 2006, sorry been busy.

The utility generates install and uninstall bat files for a BizTalk assembly applying a config file. The bat files call the btsdeploy utility and the vb scripts located in the BizTalk sdk directory (C:\Program Files\Microsoft BizTalk Server 2004\SDK\Samples\Admin\WMI\) to deploy, bind, enlist, enable and start up BizTalk orchestrations and the uninstall will do the reverse. Below is the usage message for this command line utility.

Generates an install bat file and an uninstall bat file for BizTalk Assemblies.
Syntax: BizBatInstall {Commands}
Commands:
        /b    Binding config file must be used with /a /i or /a /u
        /a    BizTalk assembly file must be used with /b /i or /b /u
        /i    Install bat file to generate must be used with /b /a
        /u    Uninstall bat file to generate must be used with /b /a
        /? or /help     Display this usage message

To create the utility:

1) In Visual Studio create a C# Console app call it BizBatInstall

2) Replace all the generated code in the class file with the following...

using System;
using System.IO;
using System.Configuration;
using System.Xml;

namespace Synergy.Samples
{
 ///


 /// Summary description for Class1.
 ///

 class BizBatInstall
 {
  ///
  ///The main entry point for the application.
  ///Gets the name of the assemblies and bat files
  ///Generates an install bat file and an uninstall bat file
  ///Install bat file contents:
  /// Rebuilds assembly
  /// Deploys assembly
  /// Imports the binding file
  /// Starts the send ports
  /// Enlists the orchestration
  /// Enables the recieve locations
  ///
  ///Uninstall bat file contents:
  /// Stops the orchestration
  /// Undeploys the biztalk assembly
  /// Removes the recieve ports
  /// Removes the send ports
  ///

  [STAThread]
  static void Main(string[] args)
  {
   try
   {
    //Check for help
    if (args.Length == 0)
    {
     ShowUsageMessage();
    }
    
    else if((args[0] == "/?") || (args[0] == "/help"))
    {
     ShowUsageMessage();
    }

    else
    {
     //Parse out command line arguements
     string bindingFileName = GetCommand("/b", args);
     string assembly = GetCommand("/a", args);
     string uninstallBatFileName = GetCommand("/u", args);
     string installBatFileName = GetCommand("/i", args);

     if (bindingFileName != string.Empty && assembly != string.Empty)
     {
      //Load the binding config file into an xml doc
      XmlDocument bindingDoc = new XmlDocument();
      bindingDoc.Load(bindingFileName);

      //Generate the bat files
      GenerateInstallBat(installBatFileName, bindingFileName, assembly, bindingDoc);
      GenerateUninstallBat(uninstallBatFileName,bindingFileName, assembly, bindingDoc);
     }

     else
     {
      Console.WriteLine("The binding file and or assembly file command line arguements were not set, no install or uninstall bat files generated!");
     }
    }
   }
   catch (System.Exception ex)
   {
    Console.Write(ex.ToString());
   }
  }

  private static string GetCommand(string commandToken, string[] args)
  {
   string commandValue = string.Empty;

   for (int i = 0; i < args.Length; i++)
   {
    if (args[i] == commandToken)
    {
     commandValue = args[i + 1];
     break;
    }
   }
   return commandValue;
  }

  private static void ShowUsageMessage()
  {
   Console.WriteLine("Generates an install bat file and an uninstall bat file for BizTalk Assemblies.");
   Console.WriteLine("Syntax: BizBatInstall ");
   Console.WriteLine("Commands:");
   Console.WriteLine("\t/b \tBinding config file must be used with /a /i or /a /u");
   Console.WriteLine("\t/a \tBizTalk assembly file must be used with /b /i or /b /u");
   Console.WriteLine("\t/i \tInstall bat file to generate must be used with /b /a");
   Console.WriteLine("\t/u \tUninstall bat file to generate must be used with /b /a");
   Console.WriteLine("\t/? or /help \tDisplay this usage message");
  }

  private static void GenerateUninstallBat(string uninstallBatFileName, string bindingFileName,
   string biztalkAssemblyFileName, XmlDocument bindingDoc)
  {
   if (uninstallBatFileName != string.Empty)
   {
    try
    {
     StreamWriter batStream = GetStreamWriter(uninstallBatFileName);
   
     //Rebuilds assembly
     batStream.WriteLine("rem  Stop and Unenlist Orchestrations ");

     foreach (XmlNode bindingDocNode in bindingDoc.SelectNodes("//ModuleRefCollection/ModuleRef/Services/Service"))
     {
      batStream.WriteLine("cscript.exe \"" +
       ConfigurationSettings.AppSettings.Get("stopOrchestrationScript") +
       "\" \"
" + bindingDocNode.SelectSingleNode("@Name").Value
+ "\" \"" +
       bindingDocNode.ParentNode.ParentNode.SelectSingleNode("@Name").Value +
       "\" Unenlist\r\n");
     }

     //Undeploy assembly
     batStream.WriteLine("rem  Undeploy Assembly ");
     batStream.WriteLine("btsdeploy remove assembly=\"" + biztalkAssemblyFileName + "\" Uninstall=TRUE\r\n");

     //Removes the recieve ports
     batStream.WriteLine("rem  Remove Receive Ports ");
     foreach (XmlNode bindingDocPortNode in bindingDoc.SelectNodes("//ReceivePortCollection/ReceivePort"))
     {
      batStream.WriteLine("cscript.exe \"" +
       ConfigurationSettings.AppSettings.Get("removeReceivePortScript") + "\" \"" +
       bindingDocPortNode.SelectSingleNode("@Name").Value + "\"\r\n");
     }

     //Removes the send ports
     batStream.WriteLine("rem  Remove Send Ports ");
     foreach (XmlNode bindingDocNode in bindingDoc.SelectNodes("//SendPortCollection/SendPort"))
     {
      batStream.WriteLine("cscript.exe \"" +
       ConfigurationSettings.AppSettings.Get("removeSendPortScript") + "\" \"" +
       bindingDocNode.SelectSingleNode("@Name").Value + "\"\r\n");
     }

     //Close and flush the file
     batStream.Close();
    }
    catch (System.Exception ex)
    {
     Console.Write(ex.ToString());
    }
   }
   else
   {
    Console.WriteLine("The uninstall bat file command line arguement was not set, no uninstall bat file generated.");
   }
  }

  private static void GenerateInstallBat(string installBatFileName, string bindingFileName,
   string biztalkAssemblyFileName, XmlDocument bindingDoc)
  {
   if (installBatFileName != string.Empty)
   {
    try
    {
     StreamWriter batStream = GetStreamWriter(installBatFileName);
   
     //Rebuilds assembly
     batStream.WriteLine("rem  Rebuild Assembly ");

     //Deploys assembly
     batStream.WriteLine("rem  Deploy Assembly ");
     batStream.WriteLine("btsdeploy deploy assembly=\"" + biztalkAssemblyFileName + "\" Install=TRUE\r\n");

     //Imports the binding file
     batStream.WriteLine("rem  Import Assembly ");
     batStream.WriteLine("btsdeploy import binding=\"" + bindingFileName + "\"\r\n");

     //Starts the send ports
     batStream.WriteLine("rem  Start Send Ports ");
     foreach (XmlNode bindingDocNode in bindingDoc.SelectNodes("//SendPortCollection/SendPort"))
     {
      batStream.WriteLine("cscript.exe \"" +
       ConfigurationSettings.AppSettings.Get("startSendPortScript") + "\" \"" +
       bindingDocNode.SelectSingleNode("@Name").Value + "\"\r\n");
     }

     //Enlists the orchestration
     batStream.WriteLine("rem  Enlist Orchestrations ");
     foreach (XmlNode bindingDocNode in bindingDoc.SelectNodes("//ModuleRefCollection/ModuleRef/Services/Service"))
     {
      batStream.WriteLine("cscript.exe \"" +
       ConfigurationSettings.AppSettings.Get("enlistOrchestrationScript") +
       "\" \"
" + bindingDocNode.SelectSingleNode("@Name").Value
+ "\" \"" +
       bindingDocNode.ParentNode.ParentNode.SelectSingleNode("@Name").Value +
       "\" Start\r\n");
     }

     //Enables the recieve locations
     batStream.WriteLine("rem  Enable Receive Ports and Locations ");
     foreach (XmlNode bindingDocPortNode in bindingDoc.SelectNodes("//ReceivePortCollection/ReceivePort"))
     {
      foreach (XmlNode bindingDocLocNode in bindingDocPortNode.SelectNodes("./ReceiveLocations/ReceiveLocation"))
      {
       batStream.WriteLine("cscript.exe \"" +
        ConfigurationSettings.AppSettings.Get("enableReceiveLocationScript") + "\" \"" +
        bindingDocPortNode.SelectSingleNode("@Name").Value + "\" \"" +
        bindingDocLocNode.SelectSingleNode("@Name").Value + "\"\r\n");
      }
     }

     //Close and flush the file
     batStream.Close();
    }
    catch (System.Exception ex)
    {
     Console.Write(ex.ToString());
    }
   }
   else
   {
    Console.WriteLine("The install bat file command line arguement was not set, no install bat file generated.");
   }
  }

  private static StreamWriter GetStreamWriter(string batFileName) 
  {
   if (File.Exists(batFileName))
   {
    File.Delete(batFileName);
   }
   StreamWriter batStream = File.CreateText(batFileName);
   return batStream;
  }
 }
}

3) Add an app.config to the project and copy the following xml configuration into it this just points to the different vb scripts in the sdk

 <appSettings>
  <add key="enlistOrchestrationScript" value="C:\Program Files\Microsoft BizTalk Server 2004\SDK\Samples\Admin\WMI\Enlist Orchestration\VBScript\EnlistOrch.vbs"/>
  <add key="stopOrchestrationScript" value="C:\Program Files\Microsoft BizTalk Server 2004\SDK\Samples\Admin\WMI\Stop Orchestration\VBScript\StopOrch.vbs"/>
  <add key="enableReceiveLocationScript" value="C:\Program Files\Microsoft BizTalk Server 2004\SDK\Samples\Admin\WMI\Enable Receive Location\VBScript\EnableRecLoc.vbs"/>
  <add key="removeReceivePortScript" value="C:\Program Files\Microsoft BizTalk Server 2004\SDK\Samples\Admin\WMI\Remove Receive Port\VBScript\RemoveReceivePort.vbs"/>
  <add key="startSendPortScript" value="C:\Program Files\Microsoft BizTalk Server 2004\SDK\Samples\Admin\WMI\Start Send Port\VBScript\StartSendPort.vbs"/>
  <add key="removeSendPortScript" value="C:\Program Files\Microsoft BizTalk Server 2004\SDK\Samples\Admin\WMI\Remove Send Port\VBScript\RemoveSendPort.vbs"/>
 </appSettings>

4) Build and call (see usage message above)

Note

This is a very simple app, feel free to modify it, it may also help to create more vb script files and call those in the bat for further functionality. For projects with more than one assembly, to install and uninstall the lot just create a bat file which uses the CALL function to execute the other bat files in order of dependance.

R. Addis

posted on Tuesday, March 21, 2006 10:44 PM

Feedback

# re: BizTalk install uninstall bat file generator 7/16/2006 1:24 PM Awais Bin Khalid
can you please post an example of calling this application, i am a bit confused about on how to call it. any binding files would be tremendously helpfull.

Thanks
Awais

# re: BizTalk install uninstall bat file generator 7/16/2006 7:46 PM Rob Addis
Hey Awais,
This is the second question I have had about binding files. See this link on how to create a binding file in BizTalk 2004:
http://www.eggheadcafe.com/ng/microsoft.public.biztalk.newuser/post20910530.asp
And to do the same in BizTalk 2006 see this post:
http://www.topxml.com/BizTalk-2006/re-21587_How-To-Package-and-Deploy-BizTalk-2006-Applications.aspx
Ok to call the install uninstall bat file generator to create an install bat file would look something like
1)Go to run and type cmd to get the command window
2)Syntax to call it is:
c:\path to BizBatInstall bin directory\BizBatInstall /b"c:\path to exported binding file\bindingfile.xml" /a"c:\path to assembly\BizTalkAssembly.dll" /i"c:\path to install bat file to generate\InstallBatFile.bat"

Hope this helps I think you should do some more research on binding files in BizTalk and learn how to read a binding file (it's not hard), this is a very fundamental concept in BizTalk’s loosely coupled architecture.
Rob Addis


# re: BizTalk install uninstall bat file generator 4/30/2007 7:45 PM gagandeepgupta16
hi, please tell me how to send and recieve an xmlstream when sender and reciever are both webservice. this is to be done without adding any references to any of the webservices???


thanx in advance

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 3 and 4 and type the answer here: