You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-cache-for-redis/cache-dotnet-core-quickstart.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: 'Quickstart: Use Azure Cache for Redis in .NET Core'
3
-
description: In this quickstart, learn how to access Azure Cache for Redis in your .NET Core apps
2
+
title: 'Quickstart: Use Azure Cache for Redis with .NET Core'
3
+
description: Modify a sample .NET Core app and connect the app to Azure Cache for Redis.
4
4
5
5
6
6
@@ -10,19 +10,19 @@ ms.topic: quickstart
10
10
ms.date: 03/25/2022
11
11
---
12
12
13
-
# Quickstart: Use Azure Cache for Redis in .NET Core
13
+
# Quickstart: Use Azure Cache for Redis with a .NET Core app
14
14
15
-
In this quickstart, you incorporate Azure Cache for Redis into a .NET Core app to have access to a secure, dedicated cache that is accessible from any application within Azure. You specifically use the [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) client with C# code in a .NET Core console app.
15
+
In this quickstart, you incorporate Azure Cache for Redis into a .NET Core app to have access to a secure, dedicated cache that is accessible from any application in Azure. You specifically use the [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) client with C# code in a .NET Core console app.
16
16
17
17
## Skip to the code
18
18
19
-
This article describes how to create an app by using the Azure portal and then modify the code to end up with a working sample app.
19
+
This article describes how to modify the code for a sample app to create a working app that connects to Azure Cache for Redis.
20
20
21
-
If you want to go straight to the code, see the [.NET Core sample](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core) on GitHub.
21
+
If you want to go straight to the sample code, see the [.NET Core quickstart sample](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core) on GitHub.
22
22
23
23
## Prerequisites
24
24
25
-
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
25
+
-An Azure subscription. [Create one for free](https://azure.microsoft.com/free/)
Make a note of the values for **HOST NAME** and the **Primary** access key. You ise these values later to construct the `CacheConnection` secret.
34
+
Make a note of the values for **HOST NAME** and the **Primary** access key. You use these values later to construct the `CacheConnection` secret.
35
35
36
36
## Add a local secret for the connection string
37
37
38
-
In your command window, execute the following command to store a new secret named *CacheConnection* after you replace the placeholders (including angle brackets) with your cache name (`<cache name>`) and primary access key (`<primary-access-key>`):
38
+
In your command window, execute the following command to store a new secret named `CacheConnection`. Replace the placeholders (including angle brackets) with your cache name (`<cache name>`) and primary access key (`<primary-access-key>`):
39
39
40
40
```dos
41
41
dotnet user-secrets set CacheConnection "<cache name>.redis.cache.windows.net,abortConnect=false,ssl=true,allowAdmin=true,password=<primary-access-key>"
42
42
```
43
43
44
44
## Connect to the cache by using RedisConnection
45
45
46
-
The connection to your cache is managed by the `RedisConnection` class. First, you make the connection in this statement from*Program.cs*:
46
+
The connection to your cache is managed by the `RedisConnection` class. First, make the connection in this statement in*Program.cs*:
In *RedisConnection.cs*, the `StackExchange.Redis` namespace was added to the code. The namespace is required for the `RedisConnection` class.
53
+
In *RedisConnection.cs*, the StackExchange.Redis namespace is added to the code. The namespace is required for the `RedisConnection` class.
54
54
55
55
```csharp
56
56
usingStackExchange.Redis;
57
57
58
58
```
59
59
60
-
The `RedisConnection` code ensures that there's always a healthy connection to the cache by managing the `ConnectionMultiplexer` instance from `StackExchange.Redis`. The `RedisConnection` class re-creates the connection when a connection is lost and unable to reconnect automatically.
60
+
The `RedisConnection`class code ensures that there's always a healthy connection to the cache. The connection is managed by the `ConnectionMultiplexer` instance from StackExchange.Redis. The `RedisConnection` class re-creates the connection when a connection is lost and can't reconnect automatically.
61
61
62
-
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in a [GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
62
+
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in the [StackExchange.Redis GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
:::image type="content" source="media/cache-dotnet-core-quickstart/cache-console-app-complete.png" alt-text="Screenshot that shows a console test completed.":::
# Quickstart: Use Azure Cache for Redis in .NET Framework
13
12
14
-
In this quickstart, you incorporate Azure Cache for Redis into a .NET Framework app to have access to a secure, dedicated cache that is accessible from any application within Azure. You specifically use the [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) client with C# code in a .NET console app.
13
+
# Quickstart: Use Azure Cache for Redis with a .NET app
14
+
15
+
In this quickstart, you incorporate Azure Cache for Redis into a .NET app to access a secure, dedicated cache that is accessible from any application in Azure. You specifically use the [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) client with C# code in a .NET console app.
15
16
16
17
## Skip to the code on GitHub
17
18
18
-
Clone the repo from [(https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet) on GitHub.
19
+
This article describes how to modify the code for a sample app to create a working app that connects to Azure Cache for Redis.
20
+
21
+
If you want to go straight to the code, see the [.NET quickstart sample](https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet) on GitHub.
19
22
20
23
## Prerequisites
21
24
22
-
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
25
+
-An Azure subscription. [Create one for free](https://azure.microsoft.com/free/)
23
26
-[Visual Studio 2019](https://www.visualstudio.com/downloads/)
24
-
-[.NET Framework 4 or higher](https://dotnet.microsoft.com/download/dotnet-framework), which is required by the StackExchange.Redis client.
27
+
-[.NET Framework 4 or later](https://dotnet.microsoft.com/download/dotnet-framework) as required by the StackExchange.Redis client
1. Create a file on your computer named *CacheSecrets.config* and place it *C:\AppSecrets\CacheSecrets.config*.
35
+
1. Create a file on your computer named *CacheSecrets.config*. Place it in the *C:\AppSecrets\* folder.
33
36
34
37
1. Edit the *CacheSecrets.config* file and add the following contents:
35
38
@@ -39,9 +42,9 @@ Clone the repo from [(https://github.com/Azure-Samples/azure-cache-redis-samples
39
42
</appSettings>
40
43
```
41
44
42
-
1. Replace `<host-name>` with your cache host name.
45
+
- Replace `<host-name>` with your cache host name.
43
46
44
-
1. Replace `<access-key>` with the primary key for your cache.
47
+
- Replace `<access-key>` with the primary key for your cache.
45
48
46
49
1. Save the file.
47
50
@@ -50,17 +53,17 @@ Clone the repo from [(https://github.com/Azure-Samples/azure-cache-redis-samples
50
53
<!-- this section was removed from the core sample -->
51
54
In this section, you prepare the console application to use the [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) client for .NET.
52
55
53
-
1. In Visual Studio, select **Tools** > **NuGet Package Manager** > **Package Manager Console**, and run the following command from the Package Manager Console window.
56
+
1. In Visual Studio, select **Tools** > **NuGet Package Manager** > **Package Manager Console**. Run the following command in the Package Manager Console window:
54
57
55
58
```powershell
56
59
Install-Package StackExchange.Redis
57
60
```
58
-
59
-
1. Once the installation is completed, the *StackExchange.Redis* cache client is available to use with your project.
61
+
62
+
1. When the installation is finished, the *StackExchange.Redis* cache client is available to use with your project.
60
63
61
64
## Connect to the Secrets cache
62
65
63
-
In Visual Studio, open your *App.config* file to verify it contains an `appSettings` `file` attribute that references the *CacheSecrets.config* file.
66
+
In Visual Studio, open your *App.config* file to verify it contains an `appSettings` `file` attribute that references the *CacheSecrets.config* file:
64
67
65
68
```xml
66
69
<?xml version="1.0" encoding="utf-8" ?>
@@ -73,36 +76,35 @@ In Visual Studio, open your *App.config* file to verify it contains an `appSetti
73
76
</configuration>
74
77
```
75
78
76
-
Never store credentials in source code. To keep this sample simple, we use an external secrets config file. A better approach would be to use [Azure Key Vault with certificates](/rest/api/keyvault/certificate-scenarios).
79
+
Never store credentials in your source code. To keep this sample simple, we use an external secrets config file. A better approach would be to use [Azure Key Vault with certificates](/rest/api/keyvault/certificate-scenarios).
77
80
78
-
## Connect to the cache with RedisConnection
81
+
## Connect to the cache by using RedisConnection
79
82
80
-
The connection to your cache is managed by the `RedisConnection` class. The connection is first made in this statement from `Program.cs`:
83
+
The connection to your cache is managed by the `RedisConnection` class. First, make the connection in this statement in *Program.cs*:
The value of the *CacheConnection*appSetting is used to reference the cache connection string from the Azure portal as the password parameter.
91
+
The value of the *CacheConnection*app setting is used to reference the cache connection string from the Azure portal as the password parameter.
89
92
90
-
In `RedisConnection.cs`, you see the `StackExchange.Redis` namespace with the `using`keyword. This is needed for the `RedisConnection` class.
93
+
In *RedisConnection.cs*, the StackExchange.Redis namespace appears as a `using`statement that the `RedisConnection` class requires:
91
94
92
95
```csharp
93
96
usingStackExchange.Redis;
94
97
```
95
98
96
-
<!-- Is this right Philo -->
97
-
The `RedisConnection` code ensures that there is always a healthy connection to the cache by managing the `ConnectionMultiplexer` instance from `StackExchange.Redis`. The `RedisConnection` class recreates the connection when a connection is lost and unable to reconnect automatically.
99
+
The `RedisConnection` class code ensures that there's always a healthy connection to the cache. The connection is managed by the `ConnectionMultiplexer` instance from StackExchange.Redis. The `RedisConnection` class re-creates the connection when a connection is lost and can't reconnect automatically.
98
100
99
-
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in a [GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
101
+
For more information, see [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/) and the code in the [StackExchange.Redis GitHub repo](https://github.com/StackExchange/StackExchange.Redis).
@@ -153,24 +155,25 @@ In the example, you can see the `Message` key is set to value. The app updated t
153
155
154
156
The Redis server stores most data as strings, but these strings can contain many types of data, including serialized binary data, which can be used when storing .NET objects in the cache.
155
157
156
-
Azure Cache for Redis can cache both .NET objects and primitive data types, but before a .NET object can be cached it must be serialized.
158
+
Azure Cache for Redis can cache both .NET objects and primitive data types, but before a .NET object can be cached, it must be serialized.
157
159
158
-
This .NET object serialization is the responsibility of the application developer, and gives the developer flexibility in the choice of the serializer.
160
+
This .NET object serialization is the responsibility of the application developer. You have some flexibility in your choice of the serializer.
159
161
160
-
One simple way to serialize objects is to use the `JsonConvert` serialization methods in `System.text.Json`.
162
+
A simple way to serialize objects is to use the `JsonConvert` serialization methods in *System.text.Json*.
161
163
162
-
Add the `System.text.Json` namespace to Visual Studio:
164
+
Add the System.text.Json namespace in Visual Studio:
If you continuing to the use this quickstart, you can keep the resources created and reuse them.
200
-
201
-
Otherwise, if you are finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.
202
-
203
-
> [!IMPORTANT]
204
-
> Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually on the left instead of deleting the resource group.
205
-
>
206
-
207
-
Sign in to the [Azure portal](https://portal.azure.com) and select **Resource groups**.
208
-
209
-
In the **Filter by name...** textbox, type the name of your resource group. The instructions for this article used a resource group named *TestResources*. On your resource group in the result list, select **...** then **Delete resource group**.
196
+
To build and run the console app to test serialization of .NET objects, select Ctrl+F5.
:::image type="content" source="media/cache-dotnet-core-quickstart/cache-console-app-complete.png" alt-text="Screenshot that shows the console app completed.":::
212
199
213
-
You are asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and select **Delete**.
200
+
<!-- Clean up include -->
214
201
215
-
After a few moments, the resourcegroup and all of its contained resources are deleted.
0 commit comments