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:
This article will provide guidance on each of possible options.
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"
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 that 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(
key,
nameConfigFile)
{
// Get the application configuration file path.
// Combining location of config file with name of config file
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();
}
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
/// </
param
name
=
"subKey"
>subKey in the Hive</
"value"
>Value from subKey</
returns
>Request value from the subKey</
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;
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.
/// Obtain value from Definition in a Vocabulary
"definitionName"
>Definition Name e.g. Value1</
"vocabularyName"
>Name of Vocabulary i.e. Config</
>Return Constant Value</
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();
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.
/// Read method helps get configuration data
"appName"
>The name of the affiliate application to represent the configuration container to access</
"propName"
>The property name to read</
/// The value of the property stored in the given affiliate application of this component.
public static string Read(string appName, string propName)
//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;
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).
/// Get value from custom database table
>name to search in database table</
>Value of the name</
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;
There are a couple of samples available demonstrating the options:
Read suggested related topics:
Steef-Jan Wiggers edited Revision 1. Comment: Added text
Steef-Jan Wiggers edited Original. Comment: Added tags. added text
Awesome
Steef-Jan Wiggers edited Revision 30. Comment: Minor edits, fixed typo
One of my favorites. Great article!
Sandro Pereira edited Revision 32. Comment: Fixing links to open in a new windows and code format
Steef-Jan Wiggers edited Revision 35. Comment: Formatting
Maheshkumar S Tiwari edited Revision 36. Comment: Minor formatting around See also and added tag