Updated on 2024-10-30 GMT+08:00

Connecting to an Instance Using C#

This section describes how to use C# to access a GeminiDB Redis instance.

Prerequisites

  • A GeminiDB Redis instance has been created and is in the Available status.
  • An ECS is available. For details, see Purchasing an ECS.
  • GNU Compiler Collection (GCC) has been installed on the ECS.
  • The created ECS is in the same region, AZ, VPC, and security group as the GeminiDB Redis instance.

Procedure

  1. Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.

  2. Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
  3. Install .Net. For a Windows host, click here to download .NET. For a Linux host, you need to install .NET Core key and repository, and then install the .NET runtime and SDK.

    sudo rpm -Uvh https://packages.microsoft.com/config/centos/8/packages-microsoft-prod.rpm
    sudo yum install dotnet-sdk-7.0
    sudo yum install dotnet-runtime-7.0    

    Run the following code.

    dotnet --version 

    You'll see your .Net version information.

  4. Use the StackExchange.Redis client to connect to the GeminiDB Redis instance.

    • Creating a project

      Run the following command to create a C# console application or create a new C# console application in Visual Studio.

      dotnet new console -o redisdemo
    • Installing the StackExchange.Redis package of the C# client of the Redis. In Visual Studio, you can install StackExchange.Redis from the NuGet package manager. Run the following command in the command line window where the dotnet project is located:
       dotnet add package StackExchange.Redis
    • Connecting to GeminiDB Redis in single-node mode
      using System;
      using StackExchange.Redis;  
      namespace redisdemo
      {
          class Program
          {
              static void  Main(string[] args)
              {
                  //Create a ConnectionMultiplexer object connected to the Redis server.
                  string redisConnectionString = "192.xx.xx.xx:6379"; // Load balancer address obtained in step 1
                  ConfigurationOptions options = ConfigurationOptions.Parse(redisConnectionString);
                    // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
                    // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
                  string password = Environment.GetEnvironmentVariable("EXAMPLE_PASSWORD_ENV");
                  options.Password = password; 
                  ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(options);
       
                  //Obtain the Redis database object.
                  IDatabase redisDb = redis.GetDatabase();
       
                  //Set a key-value pair.
                  string key = "mykey";
                  string value = "myvalue";
                  redisDb.StringSet(key, value);
                  string valueGet = redisDb.StringGet(key);
                  Console.WriteLine ($"The value of {key}: {valueGet}");
              }
          }
      }  

    Expected output:

    The value of mykey is myvalue.
    • Connecting to the GeminiDB Redis cluster in cluster mode
      using System;
      using StackExchange.Redis;
      
      namespace redisdemo
      {
          class Program
          {
              static void  Main(string[] args)
              {
                      ConfigurationOptions options = new ConfigurationOptions();
                      options.EndPoints.Add("192.xx.xx.xx:6379"); // IP address and port number of node 1 in the instance cluster obtained in Step 1               
                      options.EndPoints.Add("192.xx.xx.xx:6379"); // IP address and port number of node 2 in the instance cluster obtained in Step 1               
                      options.Password = "your_password"; // Set the password.
                      ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(options);
                  //Obtain the Redis database object.
                      IDatabase redisDb = redis.GetDatabase();
                  //Set a key-value pair.
                      string key = "mykey";
                      string value = "myvalue";
                      redisDb.StringSet(key, value);
                      string valueGet = redisDb.StringGet(key);
                  Console.WriteLine ($"The value of {key}: {valueGet}");
              }
          }
      }     

    Expected output:

    The value of mykey is myvalue.