BizTalk and Configuration Files
In one of my previous post I mentioned Log4Net. Configuration of log4net is done through a registry pointing to a configuration file, where one can determine how configuration of logging inside BizTalk for instance can be done. So using a config file for configuration purposes in BizTalk is also possible in another way. One can create an environmental variable by going to control panel --> system --> advanced.
Next one can create an new enviromental variable by clicking enviromental variables and new button under system variables.
After creating the variable it can be checked by using the command prompt and type : set enviromental variable.
Once this is al done a class library can be added to a BizTalk solution. In class module the following assemblies are necessary:
#region Using
using System;
using System.Xml;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
#endregion
namespace xxx.Components
{
///
/// BizTalk Helper Class
///
[Serializable]
public class HelperClass
{
///
/// Get values from configuration file (app.config)
///
/// Key string
///return value as object
public object GetConfigValue(string key)
{
// Get the application configuration file path.
// Combining location of config file with name of config file
string exeFilePath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("The Created Environmetal Variable"),"ConfigFileName");
// Log filepath
System.Diagnostics.EventLog.WriteEntry("CREATE", exeFilePath);
// 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
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(configFile,
ConfigurationUserLevel.None);
// Log value
System.Diagnostics.EventLog.WriteEntry("CREATE In",config.AppSettings.Settings[key].Value.ToString());
// Return desired value from configuration file
return config.AppSettings.Settings[key].Value.ToString();
}
}
}
Only hardcoded names will be in used in the combine methode of the System.IO.Path. Compared to log4net a registry key will read for the location of the config file. This is as mentioned a different approach in reading in a config file inside a BizTalk Solution. The reason I needed the config file was to put credentials for dynamic file send port, which where different in development, test and production environment. Below you see code used in a message assign shape:
//SET PATH (SHARE)
NewProjectMsg_FILEPort(Microsoft.XLANGs.BaseTypes.Address) = "file://\\\\"+ serverName + "\\" + directoryName + "\\" + guid + ".xml";
//Set Location
newProjectMsg.LOCATION = serverName;
//Set message properties for file
newProjectMsg(FILE.CopyMode) = System.Convert.ToUInt32(helper.GetConfigValue("FileCopyMode"));
newProjectMsg(FILE.Username) = System.Convert.ToString(helper.GetConfigValue("Username"));
newProjectMsg(FILE.Password) = System.Convert.ToString(helper.GetConfigValue("Password"));
Hopefully this usefull to anyone or maybe there is a better way. The latter I am interested in, so please comment if you do.
Technorati: BizTalk 2006 Configuration Files Log4Net
Next one can create an new enviromental variable by clicking enviromental variables and new button under system variables.
After creating the variable it can be checked by using the command prompt and type : set enviromental variable.
Once this is al done a class library can be added to a BizTalk solution. In class module the following assemblies are necessary:
#region Using
using System;
using System.Xml;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
#endregion
namespace xxx.Components
{
///
/// BizTalk Helper Class
///
[Serializable]
public class HelperClass
{
///
/// Get values from configuration file (app.config)
///
/// Key string
///
public object GetConfigValue(string key)
{
// Get the application configuration file path.
// Combining location of config file with name of config file
string exeFilePath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("The Created Environmetal Variable"),"ConfigFileName");
// Log filepath
System.Diagnostics.EventLog.WriteEntry("CREATE", exeFilePath);
// 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
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(configFile,
ConfigurationUserLevel.None);
// Log value
System.Diagnostics.EventLog.WriteEntry("CREATE In",config.AppSettings.Settings[key].Value.ToString());
// Return desired value from configuration file
return config.AppSettings.Settings[key].Value.ToString();
}
}
}
Only hardcoded names will be in used in the combine methode of the System.IO.Path. Compared to log4net a registry key will read for the location of the config file. This is as mentioned a different approach in reading in a config file inside a BizTalk Solution. The reason I needed the config file was to put credentials for dynamic file send port, which where different in development, test and production environment. Below you see code used in a message assign shape:
//SET PATH (SHARE)
NewProjectMsg_FILEPort(Microsoft.XLANGs.BaseTypes.Address) = "file://\\\\"+ serverName + "\\" + directoryName + "\\" + guid + ".xml";
//Set Location
newProjectMsg.LOCATION = serverName;
//Set message properties for file
newProjectMsg(FILE.CopyMode) = System.Convert.ToUInt32(helper.GetConfigValue("FileCopyMode"));
newProjectMsg(FILE.Username) = System.Convert.ToString(helper.GetConfigValue("Username"));
newProjectMsg(FILE.Password) = System.Convert.ToString(helper.GetConfigValue("Password"));
Hopefully this usefull to anyone or maybe there is a better way. The latter I am interested in, so please comment if you do.
Technorati: BizTalk 2006 Configuration Files Log4Net
Comments