Exemplo n.º 1
0
        /// <summary>
        /// Creates a connection string builder.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <returns></returns>
        /// <exception cref="MongoException">
        /// </exception>
        public static ConnectionStringBuilder Create(string connectionString)
        {
            var connection = connectionString;

            if (!connection.StartsWith(PROTOCOL, StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    connection = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
                }
                catch (NullReferenceException)
                {
                    throw new MongoException("Connection string is missing");
                }

                if (string.IsNullOrEmpty(connection) ||
                    !connection.StartsWith(PROTOCOL, StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new MongoException("Invalid connection string: the protocol must be mongodb://");
                }
            }

            var parts = connection.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
            var options = parts.Length == 2 ? parts[1] : null;
            var sb = new StringBuilder(parts[0].Substring(PROTOCOL.Length));
            var builder = new ConnectionStringBuilder
            {
                QueryTimeout = 30,
                Timeout = 30,
                EnableExpandoProperties = false,
                StrictMode = true,
                Pooled = true,
                PoolSize = 25,
            };

            // var coreBuilder = new StringBuilder();
            builder.BuildAuthentication(sb/*, coreBuilder*/)
                .BuildDatabase(sb)
                .BuildServerList(sb);

            BuildOptions(builder, options);
            return builder;
        }
Exemplo n.º 2
0
        public static ConnectionStringBuilder Create(string connectionString)
        {
            if (!connectionString.StartsWith(PROTOCOL, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new MongoException("Invalid connection string: the protocol must be mongodb://");
            }
            var parts = connectionString.Split(new[]{'?'}, StringSplitOptions.RemoveEmptyEntries);
            var options = parts.Length == 2 ? parts[1] : null;
            var sb = new StringBuilder(parts[0].Substring(PROTOCOL.Length));
            var builder = new ConnectionStringBuilder
                              {
                                  QueryTimeout = 30,
                                  Timeout = 30,
                                  EnableExpandoProperties = false,
                                  StrictMode = true,
                                  Pooled =  true,
                                  PoolSize = 25,
                              };
            var coreBuilder = new StringBuilder();
            builder.BuildAuthentication(sb, coreBuilder)
                .BuildDatabase(sb)
                .BuildServerList(sb);

            BuildOptions(builder, options);
            return builder;
        }