BizTalk Server: Application Configuration options
Introduction
There are a couple of options a developer can have when comes to using application configurable values. These options can be divided into three categories:
- File: BTSNTSvc.exe.config, Custom config file, Enterprise Library
- Database: Business Rule Engine, SSO, or Custom database
- Registry
This article will provide guidance on each of possible options.
BTSNTSvc.exe.config file
The BizTalk orchestration engine uses an XML file called BTSNTSvc.exe.config to determine certain behaviors. This file attaches to the main executable BTSNTSvc.exe. Any changes placed in BTSNTSvc.exe.config apply to all host instance regardless of their names. This file is always located in the same directory as the BTSNTSvc.exe file, which is usually C:\Program Files\Microsoft BizTalk Server 2010. Any changes in this file will result in a restart of BizTalk Host Instance. Values can be obtained through code in expression shape in an orchestration.
//Get Configuration parameters from BTSNTsvc.exe.config
BTSNTsvcConfigValue1=System.Configuration.ConfigurationSettings.AppSettings.Get("Value1");
BTSNTsvcConfigValue2=System.Configuration.ConfigurationSettings.AppSettings.Get("Value2");
Blogs
Custom Config File
A custom configuration file (.config) can be option to store application configuration data that you can use within BizTalk orchestration for instance. A custom config can be easier to maintain than BTSNTSvc.exe.config file. You can choose whether or not to use Enterprise Library or custom code. Enterprise Library may be too much overhead to just access a custom file. Accessing a .config file can be done using simple .NET code, using the System.Configuration namespace.
/// <summary>
/// Return configuration value
/// </summary>
/// <param name="key">Key in configuration</param>
/// <param name="nameConfigFile">Name of configuration file</param>
/// <returns></returns>
public static string GetConfigValue(string key, string nameConfigFile)
{
// Get the application configuration file path.
// Combining location of config file with name of config file
string exeFilePath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("BizTalkConfigOptions", EnvironmentVariableTarget.Machine), nameConfigFile);
// Map to the application configuration file.
// Instantiate ExeConfigurationFileMap
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
// Bind filepath to configFile object
configFile.ExeConfigFilename = exeFilePath;
// Instantiate Configuration object and assign configFile
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
// Return desired value from configuration file
return config.AppSettings.Settings[key].Value.ToString();
}
When using custom config file in a multi-machine environment you need to maintain config on each machine. Another back draw is I/O with reading from file. You might need to implement some caching to avoid delays in reading from physical file.
Blogs
There are some blogs that discuss this option:
- BizTalk and Configuration Files
- Storing BizTalk settings in custom configuration file using Enterprise Library
Registry
Another option is accessing the registry to obtain application configuration values. Through custom code in .NET Helper Class using Microsoft.Win32 namespace you can access the registry and read values. See code below to access the registry.
[Serializable]
public static class REGISTRYConfigHelper
{
/// <summary>
/// Return value from registry
/// </summary>
/// <param name="subKey">subKey in the Hive</param>
/// <param name="value">Value from subKey</param>
/// <returns>Request value from the subKey</returns>
public static string Read(string subKey, string value)
{
// Create value variable
string retValue = null;
// Opening the registry key
RegistryKey rk = Registry.CurrentUser;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(subKey,RegistryKeyPermissionCheck.ReadSubTree,System.Security.AccessControl.RegistryRights.FullControl);
// If the RegistrySubKey does exist
if (sk1 != null)
{
try
{
// If the RegistryKey exists get its value or null is returned.
retValue = (string)sk1.GetValue(value.ToUpper());
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message);
throw;
}
}
// Return value
return retValue;
}
}
Storing values in registry is an option, however it is not easy to maintain. In a multiple machine BizTalk deployment this is far from ideal option as you have to maintain multiple registries. The same accounts for using custom file or BTSNTSvc.exe.config.
Business Rule Engine
So far registry and file options have been discussed. Not the best options when you want to consider a central store for your application custom configuration data. A database would be a better option from an administrative viewpoint. Business Rule Engine (BRE) can be used to store configuration data. Using a vocabulary with custom definitions containing constant values. Code below shows how to access custom definition in a vocabulary.
/// <summary>
/// Obtain value from Definition in a Vocabulary
/// </summary>
/// <param name="definitionName">Definition Name e.g. Value1</param>
/// <param name="vocabularyName">Name of Vocabulary i.e. Config</param>
/// <returns>Return Constant Value</returns>
public static string Read(string definitionName, string vocabularyName)
{
// RuleStore Object
RuleStore rlsRuleStore;
// Vocab Info collection
VocabularyInfoCollection vicVocabInfo;
// Vocab itself
Vocabulary vocVocab;
// Provides the default deployment driver used to import, export, deploy, un-deploy, and
// set tracking configuration for published rule sets and vocabularies,
// and to retrieve their deployment characteristics.
RuleSetDeploymentDriver rsdDriver = new RuleSetDeploymentDriver();
// The current RuleStore
rlsRuleStore = rsdDriver.GetRuleStore();
// Set Vocabulary based on Vocabulary collection
vicVocabInfo = rlsRuleStore.GetVocabularies(vocabularyName, RuleStore.Filter.All);
// Get the vocabulary itself
vocVocab = rlsRuleStore.GetVocabulary(vicVocabInfo[0]);
// Get the definition
VocabularyDefinition vocDef = vocVocab.Definitions.GetByName(definitionName);
// Set LiteralDefition
LiteralDefinition literalDefinition = vocDef as LiteralDefinition;
// Return Value
return literalDefinition.Value.ToString();
}
From administrative standpoint maintaining configuration files in BRE can be complex compared to using SSO or CustomDb.
Blogs
SSO Database
One of popular and easy options of retrieving application configuration data is using SSO. Microsoft has built a SSO Application Configuration snap in that provides an good user experience maintaining custom configuration data. When downloading the snap in you will also get code for a .NET Helper Class to support retrieval of data in BizTalk. See code below for retrieving data from SSO.
/// <summary>
/// Read method helps get configuration data
/// </summary>
/// <param name="appName">The name of the affiliate application to represent the configuration container to access</param>
/// <param name="propName">The property name to read</param>
/// <returns>
/// The value of the property stored in the given affiliate application of this component.
/// </returns>
public static string Read(string appName, string propName)
{
try
{
//Instantiate SSOConfigStore Object
SSOConfigStore ssoStore = new SSOConfigStore();
//Instantiate ConfigurationPropertyBag
ConfigurationPropertyBag appMgmtBag = new ConfigurationPropertyBag();
//Get value based on appName
((ISSOConfigStore)ssoStore).GetConfigInfo(appName, idenifierGUID, SSOFlag.SSO_FLAG_RUNTIME, (IPropertyBag)appMgmtBag);
//Create propertyValue object
object propertyValue = null;
//Read property
appMgmtBag.Read(propName, out propertyValue, 0);
//return property value
return (string)propertyValue;
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e.Message);
throw;
}
}
This approach has some major benefits. Data is cached and has a built in refresh mechanism (ESSO Service) and secure. It offers ability to store sensitive information.
Blogs
- Custom Configuration options for BizTalk Solution
- SSO Configuration Application MMC Snap-In
- Storing Application Configuration Information in SSO
Custom Database
Instead of using one of existing BizTalk databases you can use a custom database to store application configuration data. You can use an .Net Helper Class that supports retrieving the data from database (see code below).
/// <summary>
/// Get value from custom database table
/// </summary>
/// <param name="value">name to search in database table</param>
/// <returns>Value of the name</returns>
public static string Read(string name)
{
string result = null;
// Connectionstring, you have to change this setting your own Data Source
// Connectionstring could be obtain from config file
using (var conn = new SqlConnection("Data Source=WIN-8BPNTQKTJ5M;Initial Catalog=ConfigDb;Integrated Security=True"))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT Value FROM ConfigValues WHERE Name='" + name + "'";
//Execute command
SqlDataReader reader = cmd.ExecuteReader();
//Read result
while (reader.Read())
{
result = reader.GetString(0);
}
//Close reader
reader.Close();
//Close connection
conn.Close();
}
// return value
return result;
}
Custom database can be option, but it does not offer the security like SSO. Maintaining custom database is less intuitive as with SSO Application Configuration snap in.
Samples
There are a couple of samples available demonstrating the options:
- Custom Configuration options for BizTalk Solution
- Where do I store my custom configuration for a BizTalk solution
See Also
Read suggested related topics:
Another important place to find a huge amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.