BizTalk - Creating a message instance using the deployed schema

I’m always in a situation where I need to create an empty message instance on the fly in an orchestration and I have found this method to be the sleekest of them all. Ive found guidance on this issue from my colleague Yossi on one of his previous blogs but thought I’d reiterate it with a few modifications as it always saves me a bit of trouble when generatng message instances:
With Microsoft.BizTalk.Component.Interop.DocumentSpec we can easily create an instance of a message on the fly by simply pointing to a deployed schema. 
var doc = new XmlDocument();
var sw = new StringWriter(new StringBuilder())
var catalog = new BtsCatalogExplorer { BTSDBConnectionString };
var schema = catalog.Schemas[schemaName
var docSpec = new DocumentSpec(string.Format("{0}+{1}", schemaName, rootNode), schema.BtsAssembly.DisplayName);
doc.Load(docSpec.CreateXmlInstance(sw));
  
And if you’d prefer to not hold another configuration value for your Biztalk Connection String, then with a little help from http://blogdoc.biztalk247.com/article.aspx?page=4eb96b13-b5a4-4c2e-96c2-c9a7d07f36f8 you can just make use of either WMI:
using (var mgmtSearcher = new ManagementObjectSearcher())
  {
     mgmtSearcher.Scope = new ManagementScope(@"root\MicrosoftBizTalkServer");
     mgmtSearcher.Query = new SelectQuery("select * from MSBTS_GroupSetting");
     using (var enumGroupSetting = mgmtSearcher.Get().GetEnumerator())
          {
while (enumGroupSetting.MoveNext())
             {
                var groupSetting = (ManagementObject) enumGroupSetting.Current;
                _server = groupSetting["MgmtDbServerName"] as string;
                 _database = groupSetting["MgmtDbName"] as string
              }
          }
  }
Or accessing the registry:
using (var key =  Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\BizTalk Server\3.0\Administration"))
 {
      _server = keyBts.GetValue("MgmtDBServer") as string
      _database = keyBts.GetValue("MgmtDBName") as string;
  }

To build the connection string for your Management Database:

var connectionString = new SqlConnectionStringBuilder
{  
    DataSource = _server,
    InitialCatalog = _database                
}

Easy…

 

 

Written by Nigel Sheldon at 00:00

Categories :

0 Comments :

Comment

Comments closed