|
| 1 | +--- |
| 2 | +title: How to convert session token formats in .NET SDK - Azure Cosmos DB |
| 3 | +description: Learn how to convert session token formats to ensure compatibilities between different .NET SDK versions |
| 4 | +author: vinhms |
| 5 | +ms.service: cosmos-db |
| 6 | +ms.topic: conceptual |
| 7 | +ms.date: 04/30/2020 |
| 8 | +ms.author: vitrinh |
| 9 | +--- |
| 10 | + |
| 11 | +# Convert session token formats in .NET SDK |
| 12 | + |
| 13 | +This article explains how to convert between different session token formats to ensure compatibility between SDK versions. |
| 14 | + |
| 15 | +> [!NOTE] |
| 16 | +> By default, the SDK keeps track of the session token automatically and it will use the most recent session token. For more information, please visit [Utilize session tokens](how-to-manage-consistency.md#utilize-session-tokens). The instructions in this article only apply with the following conditions: |
| 17 | +> * Your Azure Cosmos DB account uses Session consistency. |
| 18 | +> * You are managing the session tokens are manually. |
| 19 | +> * You are using multiple versions of the SDK at the same time. |
| 20 | +
|
| 21 | +## Session token formats |
| 22 | + |
| 23 | +There are two session token formats: **simple** and **vector**. These two formats are not interchangeable so, the format should be converted when passing to the client application with different versions. |
| 24 | +- The **simple** session token format is used by the .NET SDK V1 (Microsoft.Azure.DocumentDB -version 1.x) |
| 25 | +- The **vector** session token format is used by the .NET SDK V2 (Microsoft.Azure.DocumentDB -version 2.x) |
| 26 | + |
| 27 | +### Simple session token |
| 28 | + |
| 29 | +A simple session token has this format: `{pkrangeid}:{globalLSN}` |
| 30 | + |
| 31 | +### Vector session token |
| 32 | + |
| 33 | +A vector session token has the following format: |
| 34 | +`{pkrangeid}:{Version}#{GlobalLSN}#{RegionId1}={LocalLsn1}#{RegionId2}={LocalLsn2}....#{RegionIdN}={LocalLsnN}` |
| 35 | + |
| 36 | +## Convert to Simple session token |
| 37 | + |
| 38 | +To pass a session token to client using .NET SDK V1, use a **simple** session token format. For example, use the following sample code to convert it. |
| 39 | + |
| 40 | +```csharp |
| 41 | +private static readonly char[] SegmentSeparator = (new[] { '#' }); |
| 42 | +private static readonly char[] PkRangeSeparator = (new[] { ':' }); |
| 43 | + |
| 44 | +// sessionTokenToConvert = session token from previous response |
| 45 | +string[] items = sessionTokenToConvert.Split(PkRangeSeparator, StringSplitOptions.RemoveEmptyEntries); |
| 46 | +string[] sessionTokenSegments = items[1].Split(SessionTokenHelpers.SegmentSeparator, StringSplitOptions.RemoveEmptyEntries); |
| 47 | + |
| 48 | +string sessionTokenInSimpleFormat; |
| 49 | + |
| 50 | +if (sessionTokenSegments.Length == 1) |
| 51 | +{ |
| 52 | + // returning the same token since it already has the correct format |
| 53 | + sessionTokenInSimpleFormat = sessionTokenToConvert; |
| 54 | +} |
| 55 | +else |
| 56 | +{ |
| 57 | + long version = 0; |
| 58 | + long globalLSN = 0; |
| 59 | + |
| 60 | + if (!long.TryParse(sessionTokenSegments[0], out version) |
| 61 | + || !long.TryParse(sessionTokenSegments[1], out globalLSN)) |
| 62 | + { |
| 63 | + throw new ArgumentException("Invalid session token format", sessionTokenToConvert); |
| 64 | + } |
| 65 | + |
| 66 | + sessionTokenInSimpleFormat = string.Format("{0}:{1}", items[0], globalLSN); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Convert to Vector session token |
| 71 | + |
| 72 | +To pass a session token to client using .NET SDK V2, use the **vector** session token format. For example, use the following sample code to convert it. |
| 73 | + |
| 74 | +```csharp |
| 75 | + |
| 76 | +private static readonly char[] SegmentSeparator = (new[] { '#' }); |
| 77 | +private static readonly char[] PkRangeSeparator = (new[] { ':' }); |
| 78 | + |
| 79 | +// sessionTokenToConvert = session token from previous response |
| 80 | +string[] items = sessionTokenToConvert.Split(PkRangeSeparator, StringSplitOptions.RemoveEmptyEntries); |
| 81 | +string[] sessionTokenSegments = items[1].Split(SegmentSeparator, StringSplitOptions.RemoveEmptyEntries); |
| 82 | + |
| 83 | +string sessionTokenInVectorFormat; |
| 84 | + |
| 85 | +if (sessionTokenSegments.Length == 1) |
| 86 | +{ |
| 87 | + long globalLSN = 0; |
| 88 | + if (long.TryParse(sessionTokenSegments[0], out globalLSN)) |
| 89 | + { |
| 90 | + sessionTokenInVectorFormat = string.Format("{0}:-2#{1}", items[0], globalLSN); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + throw new ArgumentException("Invalid session token format", sessionTokenToConvert); |
| 95 | + } |
| 96 | +} |
| 97 | +else |
| 98 | +{ |
| 99 | + // returning the same token since it already has the correct format |
| 100 | + sessionTokenInVectorFormat = sessionTokenToConvert; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Next steps |
| 105 | + |
| 106 | +Read the following articles: |
| 107 | + |
| 108 | +* [Use session tokens to manage consistency in Azure Cosmos DB](how-to-manage-consistency.md#utilize-session-tokens) |
| 109 | +* [Choose the right consistency level in Azure Cosmos DB](consistency-levels-choosing.md) |
| 110 | +* [Consistency, availability, and performance tradeoffs in Azure Cosmos DB](consistency-levels-tradeoffs.md) |
| 111 | +* [Availability and performance tradeoffs for various consistency levels](consistency-levels-tradeoffs.md) |
0 commit comments