cancel
Showing results for 
Search instead for 
Did you mean: 
Jacob_Barton
Content Contributor
Content Contributor

This post explains how to create an OnBase compatible .NET assembly that reads from a separate config file. This process will allow any changes from within the configuration file to be reflected whenever the script is executed, without having to reset the Application Pool. 

 

Things To Consider:


When running Unity Scripts, the application server runs these as built assemblies. Therefore, whenever writing scripts that work with files external of OnBase, you must consider these files' path with references from the application server or be explicit with the declaration of the file path. With that being said, if an imported assembly were to use the ConfigurationManager class to implicitly draw in the AppSettings, this assembly would be reading from the Web.Config file that is found in the Application Server folder.

 

**NOTE: This kind of functionality is outside of our immediate scope as API Support. More information could be found in the Microsoft Documentation.

 

Creating The Assembly:


Let's create our assembly that we will import into OnBase Studio. This will be a simple assembly that returns a string of the Key/Value pairs within the custom configuration file.

using System.Configuration;namespace TestDLL{  public class KeyValueReader    {       public static string ReadAllSettings()        {            string output = "";            try            {              ExeConfigurationFileMap map = new ExeConfigurationFileMap{ ExeConfigFilename = @"C:\App.config"};                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);                var appSettings = config.AppSettings.Settings;                if (appSettings.Count == 0)                {                    output += "AppSettings is empty.";                }                else                {                    foreach (var key in appSettings.AllKeys)                    {                        output += $"Key: {key} Value: {appSettings[key].Value}\n";                    }                }            }            catch (ConfigurationErrorsException)            {                output += "Error reading app settings";            }            return output;        }    }}

This class has one method where it reads the "App.config" file located on the C: drive. The path is mapped, and that mapping is used along with the ConfigurationUserLevel enum (more info on the enum can be found here) to create the configuration object. the data can then be extracted from this configuration object. In our case, we use the AppSetings.Settings to grab each key and its corresponding value and add that to the output string.

Now all we need is a configuration file to read from.

 

Creating The Config File:


We will now create a configuration file in our designated file path to read from. This will be a simple XML file with only a couple key value pairs.

<?xml version="1.0" encoding="utf-8" ?>  <configuration>     <startup>        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />     </startup>       <appSettings>          <add key="Foo" value="Bar"/>          <add key="Spam" value="Ham"/>       </appSettings></configuration>

This base configuration file can be expanded to include all information needed to support your solution.

 

Conclusion:


With our assembly and our Configuration file created, we can now import into OnBase Studio and further extend the flexibility of your OnBase API solutions.