Exemplo n.º 1
0
        /// <summary>
        /// Returns the expected configuration key with full path, for the given option.
        /// </summary>
        public static string GetKey <TOptions, TProperty>(Expression <Func <TOptions, TProperty> > propertySelector)
        {
            string path        = OptionsAttribute.GetConfigurationPath <TOptions>();
            string propertyKey = ((MemberExpression)propertySelector.Body).Member.Name;

            if (string.IsNullOrEmpty(path))
            {
                return(propertyKey);
            }
            else
            {
                return(path + ConfigurationPathSeparator + propertyKey);
            }
        }
Exemplo n.º 2
0
        private static string LoadRhetosRuntimePath(string configurationFolder, ILogProvider logProvider)
        {
            string configurationFile = Path.Combine(configurationFolder, RhetosAppEnvironment.ConfigurationFileName);
            var    configuration     = new ConfigurationBuilder(logProvider)
                                       .AddJsonFile(configurationFile)
                                       .Build();

            var runtimeSettings = configuration.GetOptions <RhetosAppOptions>();

            if (string.IsNullOrEmpty(runtimeSettings.RhetosRuntimePath))
            {
                throw new FrameworkException($"Configuration setting '{OptionsAttribute.GetConfigurationPath<RhetosAppOptions>()}:{nameof(RhetosAppOptions.RhetosRuntimePath)}' is not specified in '{configurationFile}'.");
            }

            return(runtimeSettings.RhetosRuntimePath);
        }
        public static IConfigurationBuilder AddOptions(this IConfigurationBuilder builder, object options, string configurationPath = "")
        {
            if (string.IsNullOrEmpty(configurationPath))
            {
                configurationPath = OptionsAttribute.GetConfigurationPath(options.GetType());
            }

            var members = options.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Select(member => (member.Name, Value: member.GetValue(options)))
                          .Concat(options.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public).Select(member => (member.Name, Value: member.GetValue(options))));

            string keyPrefix = !string.IsNullOrEmpty(configurationPath) ? configurationPath + ConfigurationProvider.ConfigurationPathSeparator : "";
            var    settings  = members
                               .Select(member => new KeyValuePair <string, object>(keyPrefix + member.Name, member.Value))
                               .ToList();

            return(builder.AddKeyValues(settings));
        }
Exemplo n.º 4
0
        public T GetOptions <T>(string configurationPath = "", bool requireAllMembers = false) where T : class
        {
            var optionsType     = typeof(T);
            var optionsInstance = Activator.CreateInstance(optionsType);

            if (string.IsNullOrEmpty(configurationPath))
            {
                configurationPath = OptionsAttribute.GetConfigurationPath <T>();
            }

            var props   = optionsType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(prop => prop.CanWrite);
            var fields  = optionsType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(field => !field.IsInitOnly);
            var members = props.Cast <MemberInfo>().Concat(fields.Cast <MemberInfo>()).ToList();

            var membersBound = new List <MemberInfo>();

            foreach (var member in members)
            {
                bool convertRelativePath = member.GetCustomAttribute <AbsolutePathOptionAttribute>() != null;
                if (TryGetConfigurationValueForMemberName(member.Name, out var memberValue, configurationPath, convertRelativePath))
                {
                    SetMemberValue(optionsInstance, member, memberValue);

                    if (requireAllMembers)
                    {
                        membersBound.Add(member);
                    }
                }
            }

            if (requireAllMembers && membersBound.Count != members.Count)
            {
                var missing = members.Where(member => !membersBound.Contains(member)).Select(member => member.Name);
                throw new FrameworkException($"Binding requires all members to be present in configuration, but some are missing: {string.Join(",", missing)}.");
            }

            return((T)optionsInstance);
        }