#define TRACE using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Xml.Linq; namespace ArchestrAServices.Common; public class ConfigurationRepository { private readonly Dictionary configurationRepositoryInternal = new Dictionary(); public ConfigurationRepository() { configurationRepositoryInternal = new Dictionary(); Initialize(); } public static string ExtractValueFromKey(XElement configuration, string key, string defaultValue) { string text = LookupKeyInElement(configuration, "ServiceParameters", key); if (string.IsNullOrEmpty(text)) { text = LookupKeyInElement(configuration, "CustomData", key); } if (string.IsNullOrEmpty(text)) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 2, $"ExtractValueFromKey cannot find key '{key}', using default '{defaultValue}'"); text = defaultValue; } return text; } public void InsertExtractedParameter(XElement configuration, string Name, string DefaultValue = null) { if (string.IsNullOrEmpty(DefaultValue)) { DefaultValue = GetParameter(Name); } string value = ExtractValueFromKey(configuration, Name, DefaultValue); configurationRepositoryInternal[Name] = value; } public void InsertParameter(string Name, string Value) { configurationRepositoryInternal[Name] = Value; } public string GetParameter(string Name) { string result = string.Empty; if (configurationRepositoryInternal.ContainsKey(Name)) { result = configurationRepositoryInternal[Name]; } return result; } public string GetParameter(string Name, string DefaultValue) { string text = GetParameter(Name); if (string.IsNullOrEmpty(text)) { text = DefaultValue; } return text; } private static string LookupKeyInElement(XElement configuration, string OuterElement, string key) { string empty = string.Empty; try { string text = (from ServiceParameters in configuration.Element(OuterElement).Elements("Parameter") where key == ServiceParameters.Attribute("name").Value select ServiceParameters.Attribute("value").Value).First(); if (text != null && text.Length > 0) { SvcTrace.DiagDiagnostics.TraceEvent(TraceEventType.Information, 3, $"ExtractValueFromKey extracting value '{text}' from key '{key}'"); empty = text; } else { empty = string.Empty; } } catch (Exception ex) { empty = string.Empty; SvcTrace.DiagException.TraceEvent(TraceEventType.Error, 0, $"Exception raised : {ex.Message}"); } return empty; } private void Initialize() { InsertParameter("netTcpBinding.Security.Mode", "None"); InsertParameter("netTcpBinding.TransferMode", "Buffered"); InsertParameter("netTcpBinding.MaxReceivedMessageSize", int.MaxValue.ToString()); InsertParameter("netTcpBinding.MaxBufferSize", int.MaxValue.ToString()); InsertParameter("netTcpBinding.MaxBufferPoolSize", long.MaxValue.ToString()); InsertParameter("netTcpBinding.ReaderQuotas.MaxArrayLength", int.MaxValue.ToString()); InsertParameter("netTcpBinding.ReaderQuotas.MaxBytesPerRead", int.MaxValue.ToString()); InsertParameter("netTcpBinding.ReaderQuotas.MaxDepth", int.MaxValue.ToString()); InsertParameter("netTcpBinding.ReaderQuotas.MaxNameTableCharCount", int.MaxValue.ToString()); InsertParameter("netTcpBinding.ReaderQuotas.MaxStringContentLength", int.MaxValue.ToString()); InsertParameter("netTcpBinding.OpenTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("netTcpBinding.ReceiveTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("netTcpBinding.SendTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("netTcpBinding.CloseTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("netTcpBinding.ReliableSession.InactivityTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("HttpBinding.Security.Mode", "None"); InsertParameter("HttpBinding.AllowCookies", "false"); InsertParameter("HttpBinding.BypassProxyOnLocal", "true"); InsertParameter("HttpBinding.HostNameComparisonMode", "StrongWildcard"); InsertParameter("HttpBinding.MessageEncoding", "Text"); InsertParameter("HttpBinding.MaxReceivedMessageSize", int.MaxValue.ToString()); InsertParameter("HttpBinding.MaxBufferPoolSize", long.MaxValue.ToString()); InsertParameter("HttpBinding.ReaderQuotas.MaxArrayLength", int.MaxValue.ToString()); InsertParameter("HttpBinding.ReaderQuotas.MaxBytesPerRead", int.MaxValue.ToString()); InsertParameter("HttpBinding.ReaderQuotas.MaxDepth", int.MaxValue.ToString()); InsertParameter("HttpBinding.ReaderQuotas.MaxNameTableCharCount", int.MaxValue.ToString()); InsertParameter("HttpBinding.ReaderQuotas.MaxStringContentLength", int.MaxValue.ToString()); InsertParameter("HttpBinding.OpenTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("HttpBinding.ReceiveTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("HttpBinding.SendTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("HttpBinding.CloseTimeout", new TimeSpan(0, 1, 0).ToString()); InsertParameter("HttpBinding.ReliableSession.InactivityTimeout", new TimeSpan(0, 1, 0).ToString()); } }