Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Connecting to an Instance Using C#

Updated on 2025-01-03 GMT+08:00

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.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback