Example #1
0
        /// <summary>
        /// Parses connection string or node address into <paramref name="parameters"/>
        /// </summary>
        /// <param name="connectionString">Connection string or node address</param>
        /// <param name="parameters">Target parameters</param>
        /// <exception cref="ArgumentNullException">Null arguments</exception>
        /// <exception cref="FormatException">Incorrect format</exception>
        public static void ParseConnectionStringInto(string connectionString, IModifiableBobConnectionParameters parameters)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException(nameof(connectionString));
            }
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new FormatException("Connection string cannot be empty. At least 'address' should be specified");
            }

            if (connectionString.IndexOfAny(_connectionStringMarkers) < 0)
            {
                BobNodeAddress.TryParseCore(connectionString, true, out string host, out int?port);
                parameters.Host = host;
                if (port != null)
                {
                    parameters.Port = port;
                }
            }
            else
            {
                var keyValues = ParseConnectionStringIntoKeyValues(connectionString);

                foreach (var keyValue in keyValues)
                {
                    parameters.SetValue(keyValue.Key, keyValue.Value, allowCustomParameters: true);
                }
            }

            parameters.Validate(ValidationExceptionBehaviour.FormatException);
        }
Example #2
0
        /// <summary>
        /// Sets value for specified key
        /// </summary>
        /// <param name="parameters">Parameters that will be changed in this operation</param>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        /// <param name="allowCustomParameters">If false, an exception will be thrown if the parameter does not match any known. Otherwise, the value will be written into CustomParameters</param>
        public static void SetValue(this IModifiableBobConnectionParameters parameters, string key, string value, bool allowCustomParameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Key cannot be an empty string", nameof(key));
            }

            switch (key.ToLower())
            {
            case "host":
                parameters.Host = value;
                break;

            case "port":
                if (string.IsNullOrWhiteSpace(value))
                {
                    parameters.Port = null;
                }
                else if (!int.TryParse(value, out int portVal))
                {
                    throw new FormatException($"Unable to parse 'port' value: {value}");
                }
                else
                {
                    parameters.Port = portVal;
                }
                break;

            case "address":
            case "server":
                if (value == null)
                {
                    parameters.Host = null;
                    parameters.Port = null;
                }
                else if (string.IsNullOrWhiteSpace(value))
                {
                    parameters.Host = value;
                }
                else
                {
                    BobNodeAddress.TryParseCore(value, true, out string addrHostVal, out int?addrPortVal);
                    parameters.Host = addrHostVal;
                    if (addrPortVal != null)
                    {
                        parameters.Port = addrPortVal;
                    }
                }
                break;

            case "user":
            case "user id":
                parameters.User = value;
                break;

            case "password":
                parameters.Password = value;
                break;

            case "maxreceivemessagesize":
            case "maxreceivemessagelength":
                if (string.IsNullOrWhiteSpace(value))
                {
                    parameters.MaxReceiveMessageSize = null;
                }
                else if (!int.TryParse(value, out int maxReceiveMessageSizeVal))
                {
                    throw new FormatException($"Unable to parse '{key}' value: {value}");
                }
                else
                {
                    parameters.MaxReceiveMessageSize = maxReceiveMessageSizeVal;
                }
                break;

            case "maxsendmessagesize":
            case "maxsendmessagelength":
                if (string.IsNullOrWhiteSpace(value))
                {
                    parameters.MaxSendMessageSize = null;
                }
                else if (!int.TryParse(value, out int maxSendMessageSizeVal))
                {
                    throw new FormatException($"Unable to parse '{key}' value: {value}");
                }
                else
                {
                    parameters.MaxSendMessageSize = maxSendMessageSizeVal;
                }
                break;

            case "operationtimeout":
                if (string.IsNullOrWhiteSpace(value))
                {
                    parameters.OperationTimeout = null;
                }
                else
                {
                    parameters.OperationTimeout = ParseTimeInterval(key, value);
                }
                break;

            case "connectiontimeout":
            case "connect timeout":
                if (string.IsNullOrWhiteSpace(value))
                {
                    parameters.ConnectionTimeout = null;
                }
                else
                {
                    parameters.ConnectionTimeout = ParseTimeInterval(key, value);
                }
                break;

            case "timeout":
                if (string.IsNullOrWhiteSpace(value))
                {
                    parameters.ConnectionTimeout = null;
                    parameters.OperationTimeout  = null;
                }
                else
                {
                    parameters.ConnectionTimeout = ParseTimeInterval(key, value);
                    parameters.OperationTimeout  = parameters.ConnectionTimeout;
                }
                break;

            default:
                if (!allowCustomParameters)
                {
                    throw new ArgumentException($"Unknown key: {key}", nameof(key));
                }
                parameters.CustomParameters[key] = value;
                break;
            }
        }