Example #1
0
        /// <summary>
        /// Creates the <see cref="Cluster"/> object to connect to Azure CosmosDB Cassandra API
        /// </summary>
        /// <param name="hostName">Host name of the CosmosDB account, as shown in the "Connection String" section of Azure Portal</param>
        /// <param name="userName">UserName to connect to the CosmosDB account, as shown in the "Connection String" section of Azure Portal</param>
        /// <param name="password">
        /// Password to connect the CosmosDB account, as shown in the "Connection String" section of Azure Portal.
        /// You can use either the "Primary Master ReadWrite" or "Seconday Master ReadWrite" keys here.
        /// </param>
        public static Cluster CreateClusterForCosmosDB(string hostName, string userName, string password)
        {
            // Refer https://docs.datastax.com/en/developer/java-driver/2.1/manual/pooling/ for more details
            PoolingOptions poolingOptions = new PoolingOptions();

            poolingOptions.SetCoreConnectionsPerHost(HostDistance.Local, ClusterBuilderHelpers.CoreConnectionsPerHost);
            poolingOptions.SetMaxRequestsPerConnection(ClusterBuilderHelpers.MaxRequestsPerConnection);
            poolingOptions.SetMaxConnectionsPerHost(HostDistance.Local, ClusterBuilderHelpers.MaxConnectionsPerHost);

            // Increase the page size to Max to avoid multiple pagination calls.
            QueryOptions defaultOptions = new QueryOptions();

            defaultOptions.SetPageSize(ClusterBuilderHelpers.DefaultPageSize);

            // Set a custom host name resolver to ensure that SSL does not fail with RemoteCertificateNameMismatch
            // Ensure that we return the same hostname is passed so that it can be matched with the CNAME from the certificate.
            SSLOptions sslOptions = new SSLOptions(SslProtocols.Tls12, checkCertificateRevocation: true, remoteCertValidationCallback: ValidateServerCertificate);

            sslOptions.SetHostNameResolver((ipAddress) => hostName);

            return(Cluster
                   .Builder()
                   .WithCredentials(userName, password)
                   .WithPort(ClusterBuilderHelpers.CosmosDBCassandraPort)
                   .WithQueryOptions(defaultOptions)
                   .WithRetryPolicy(new LoggingRetryPolicy(new CosmosDBMultipleRetryPolicy(ClusterBuilderHelpers.MaxRetryOnRateLimiting)))
                   .AddContactPoint(hostName)
                   .WithSSL(sslOptions)
                   .Build());
        }