Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
APPLIES TO:
NoSQL
This article explains how to convert between different session token formats to ensure compatibility between SDK versions.
Note
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. The instructions in this article only apply with the following conditions:
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.
A simple session token has this format: {pkrangeid}:{globalLSN}
A vector session token has the following format:
{pkrangeid}:{Version}#{GlobalLSN}#{RegionId1}={LocalLsn1}#{RegionId2}={LocalLsn2}....#{RegionIdN}={LocalLsnN}
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.
private static readonly char[] SegmentSeparator = (new[] { '#' });
private static readonly char[] PkRangeSeparator = (new[] { ':' });
// sessionTokenToConvert = session token from previous response
string[] items = sessionTokenToConvert.Split(PkRangeSeparator, StringSplitOptions.RemoveEmptyEntries);
string[] sessionTokenSegments = items[1].Split(SessionTokenHelpers.SegmentSeparator, StringSplitOptions.RemoveEmptyEntries);
string sessionTokenInSimpleFormat;
if (sessionTokenSegments.Length == 1)
{
// returning the same token since it already has the correct format
sessionTokenInSimpleFormat = sessionTokenToConvert;
}
else
{
long version = 0;
long globalLSN = 0;
if (!long.TryParse(sessionTokenSegments[0], out version)
|| !long.TryParse(sessionTokenSegments[1], out globalLSN))
{
throw new ArgumentException("Invalid session token format", sessionTokenToConvert);
}
sessionTokenInSimpleFormat = string.Format("{0}:{1}", items[0], globalLSN);
}
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.
private static readonly char[] SegmentSeparator = (new[] { '#' });
private static readonly char[] PkRangeSeparator = (new[] { ':' });
// sessionTokenToConvert = session token from previous response
string[] items = sessionTokenToConvert.Split(PkRangeSeparator, StringSplitOptions.RemoveEmptyEntries);
string[] sessionTokenSegments = items[1].Split(SegmentSeparator, StringSplitOptions.RemoveEmptyEntries);
string sessionTokenInVectorFormat;
if (sessionTokenSegments.Length == 1)
{
long globalLSN = 0;
if (long.TryParse(sessionTokenSegments[0], out globalLSN))
{
sessionTokenInVectorFormat = string.Format("{0}:-2#{1}", items[0], globalLSN);
}
else
{
throw new ArgumentException("Invalid session token format", sessionTokenToConvert);
}
}
else
{
// returning the same token since it already has the correct format
sessionTokenInVectorFormat = sessionTokenToConvert;
}
Read the following articles:
Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Learning path
Connect to Azure Cosmos DB for NoSQL with the SDK - Training
Connect to Azure Cosmos DB for NoSQL with the SDK