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
- Obtain the load balancer IP address and port of the GeminiDB Redis instance that you want to access.
- For how to obtain the load balancer IP address, see Viewing the Load Balancer IP Address and Port.
- For how to obtain the port, see Viewing the Port for Accessing Each Instance Node.
- For details about how to view the IP address of each instance, see Viewing the Private IP Address or EIP.
- Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
- 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.
- 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.
- Creating a project
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot