Skip to content

Commit e759fcb

Browse files
committed
session token article
1 parent a971299 commit e759fcb

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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). This article only applies to the following conditions:
17+
> * The Azure Cosmos DB account has Session consistency
18+
> * The session tokens are managed manually
19+
> * Multiple versions of the SDK are used 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 and will need to be converted when passing to the client with different versions.
24+
- The **simple** session token format is used by the .NET SDK V1
25+
- The **vector** session token format is used by the .NET SDK V2.
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+
string[] items = sessionTokenToConvert.Split(PkRangeSeparator, StringSplitOptions.RemoveEmptyEntries);
45+
string[] sessionTokenSegments = items[1].Split(SessionTokenHelpers.SegmentSeparator, StringSplitOptions.RemoveEmptyEntries);
46+
47+
string sessionTokenInSimpleFormat;
48+
49+
if (sessionTokenSegments.Length == 1)
50+
{
51+
// returning the same token since it already has the correct format
52+
sessionTokenInSimpleFormat = sessionTokenToConvert;
53+
}
54+
else
55+
{
56+
long version = 0;
57+
long globalLSN = 0;
58+
59+
if (!long.TryParse(sessionTokenSegments[0], out version)
60+
|| !long.TryParse(sessionTokenSegments[1], out globalLSN))
61+
{
62+
throw new ArgumentException("Invalid session token format", sessionTokenToConvert);
63+
}
64+
65+
sessionTokenInSimpleFormat = string.Format("{0}:{1}", items[0], globalLSN);
66+
}
67+
```
68+
69+
## Convert to Vector session token
70+
71+
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.
72+
73+
```csharp
74+
75+
private static readonly char[] SegmentSeparator = (new[] { '#' });
76+
private static readonly char[] PkRangeSeparator = (new[] { ':' });
77+
78+
// sessionTokenToConvert = session token from previous response
79+
string[] items = sessionTokenToConvert.Split(PkRangeSeparator, StringSplitOptions.RemoveEmptyEntries);
80+
string[] sessionTokenSegments = items[1].Split(SegmentSeparator, StringSplitOptions.RemoveEmptyEntries);
81+
82+
string sessionTokenInVectorFormat;
83+
84+
if (sessionTokenSegments.Length == 1)
85+
{
86+
long globalLSN = 0;
87+
if (long.TryParse(sessionTokenSegments[0], out globalLSN))
88+
{
89+
sessionTokenInVectorFormat = string.Format("{0}:-2#{1}", items[0], globalLSN);
90+
}
91+
else
92+
{
93+
throw new ArgumentException("Invalid session token format", sessionTokenToConvert);
94+
}
95+
}
96+
else
97+
{
98+
// returning the same token since it already has the correct format
99+
sessionTokenInVectorFormat = sessionTokenToConvert;
100+
}
101+
```
102+
103+
## Next steps
104+
105+
Read the following articles:
106+
107+
* [Use session tokens to manage consistency in Azure Cosmos DB](how-to-manage-consistency.md#utilize-session-tokens)
108+
* [Choose the right consistency level in Azure Cosmos DB](consistency-levels-choosing.md)
109+
* [Consistency, availability, and performance tradeoffs in Azure Cosmos DB](consistency-levels-tradeoffs.md)
110+
* [Availability and performance tradeoffs for various consistency levels](consistency-levels-tradeoffs.md)

0 commit comments

Comments
 (0)