Geeks With Blogs
Koushal Deshpande kd
In dotnet or in BizTalk, many times we do need to store configuration values. All though there are different ways of doing it, I love SSO way. This is a quick post to show how to retrieve the values from SSO. A post on how to store values will follow soon.Following function is useful to read values from SSO. You can find multiple versions of simillar methods everywhere on net.
This is a class implementing IPropertyBag interface. Values will be populated in the object of this class when we read from SSO.
public class ConfigurationPropertyBag : IPropertyBag
{
 private HybridDictionary properties;
 internal ConfigurationPropertyBag()
 {
  properties = new HybridDictionary();
 }
 public void Read(string propName, out object ptrVar, int errLog)
 {
  ptrVar = properties[propName];
 }
 public void Write(string propName, ref object ptrVar)
 {
  properties.Add(propName, ptrVar);
 }
 public bool Contains(string key)
 {
  return properties.Contains(key);
 }
 public void Remove(string key)
 {
  properties.Remove(key);
 }
}

 

This class needs to be used like below:

public static class SSOClientHelper
{
 /// <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 identifier, string propName)
 {
  try
  {
   SSOConfigStore ssoStore = new SSOConfigStore();
   ConfigurationPropertyBag appMgmtBag = new ConfigurationPropertyBag();
   ((ISSOConfigStore)ssoStore).GetConfigInfo(appName, identifier, SSOFlag.SSO_FLAG_RUNTIME, (IPropertyBag)appMgmtBag);
   object propertyValue = null;
   appMgmtBag.Read(propName, out propertyValue, 0);
   return (string)propertyValue;
  }
  catch (Exception e)
  {
   System.Diagnostics.Trace.WriteLine(e.Message);
   throw;
  }
 }
}

 

When calling from dotnet code or BizTalk you have to pass SSO App name, Identifier assigned while creating SSO Application and a property name which you want to read. You can auto refresh data read from SSO like below:

private static Timer cacheRefreshTimer;
private static ISSOConfigStore ssoConfigStore;
private static ReaderWriterLock syncLock;

// Cache refresh interval in milliseconds
private const int CacheRefreshInterval = 5 * 60 * 1000;
private static ConfigPropertyBag ssoPropBag;

static ConfigParameters()
{
    ssoConfigStore = new ISSOConfigStore();
    ssoPropBag = new ConfigPropertyBag();
    syncLock = new ReaderWriterLock();

    ssoConfigStore.GetConfigInfo(SSO_CONFIG_APP,
         SSO_IDENTIFIER_NAME, SSOFlag.SSO_FLAG_RUNTIME,
         ssoPropBag);

    cacheRefreshTimer = new Timer(
        new TimerCallback(ConfigParameters.cacheRefreshCallback),
        null, CacheRefreshInterval, CacheRefreshInterval);
}

---------------------------------------------------------------------------------------------------------------

private static void cacheRefreshCallback(object state)
{
    // Disable the timer until we are done loading the cache.
    cacheRefreshTimer.Change(Timeout.Infinite, CacheRefreshInterval);

    // Put the data from SSO in a new property bag so that
    // we don't have to lock the property bag and block it from being
    // used. The SSO call is a remote call and may take a while.
    ConfigPropertyBag propBag2 = new ConfigPropertyBag();
    ssoConfigStore.GetConfigInfo(SSO_CONFIG_APP,
        SSO_IDENTIFIER_NAME, SSOFlag.SSO_FLAG_RUNTIME, propBag2);

    // Get a writer lock before updating the cached values.
    syncLock.AcquireWriterLock(Timeout.Infinite);

    try
    {
        ssoPropBag = propBag2;
    }
    finally
    {
        syncLock.ReleaseWriterLock();
    }
    // Enable the timer.
    cacheRefreshTimer.Change(CacheRefreshInterval,
        CacheRefreshInterval);
}

 

Above method needs to combine with read method to read from property bag. Easy isn't it?

Posted on Thursday, August 29, 2013 1:10 PM BizTalk 2010 , c# , ENTSSO , SSO , BizTalk | Back to top


Comments on this post: ENTSSO for Configuration

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Koushal Deshpande | Powered by: GeeksWithBlogs.net