"The read session is not available for the input session token." 异常

"The read session is not available for the input session token." exception

我在使用单个分区集合的 Azure DocumentDB 上遇到问题。 每当我尝试以编程方式插入或查询任何文档时,我都会收到一条异常消息

"The read session is not available for the input session token."

由于此集合是新创建的,我认为这是一个一般性错误,因此我尝试在另一个数据库上重新创建该集合,但随后在尝试创建该集合时我无法提交部署,因为我被要求分区键。 error

站在 documentation 所说的立场上,

"You do not have to specify a partition key for these collections."

有人可以帮忙吗?难道我做错了什么? 该地区是西欧(以防万一)

对于您在输入 session 令牌时遇到的错误,您可以在此处添加您的代码吗?

对于您尝试创建 collection 的门户中的问题,请执行以下操作:

  • 在分区键框中,输入space,然后按删除,您应该会在框中看到一个绿色的复选标记。

这将很快在门户中得到解决。

根据您的代码,我假设您正在尝试创建一个通用的 分页 逻辑。根据我使用 DocDB 的经验,需要使用 Continuation Token.

来实现分页

我通常有 an extension 获取所述令牌,然后我在后续请求中使用它,如下所示:

/// <summary>
/// Paged results with continuation token
/// </summary>
/// <typeparam name="T"></typeparam>
public class PagedResults<T>
{
    public PagedResults()
    {
        Results = new List<T>();
    }
    /// <summary>
    /// Continuation Token for DocumentDB
    /// </summary>
    public string ContinuationToken { get; set; }

    /// <summary>
    /// Results
    /// </summary>
    public List<T> Results { get; set; }
}

/// <summary>
/// Creates a pagination wrapper with Continuation Token support
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static async Task<PagedResults<T>> ToPagedResults<T>(this IQueryable<T> source)
{
    var documentQuery = source.AsDocumentQuery();
    var results = new PagedResults<T>();

    try
    {
        var queryResult = await documentQuery.ExecuteNextAsync<T>();
        if (!queryResult.Any())
        {
            return results;
        }
        results.ContinuationToken = queryResult.ResponseContinuation;
        results.Results.AddRange(queryResult);
    }
    catch
    {
        //documentQuery.ExecuteNextAsync might throw an Exception if there are no results
        return results;
    }

    return results;
}

您可以在您的代码中使用此助手以及 FeedOptions:

var feedOptions = new FeedOptions() { MaxItemCount = sizeOfPage };
var collectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
PagedResults<T> results = await client.CreateDocumentQuery<T>(collectionUri,feedOptions).Where(predicate).ToPagedResults();
//You can check of the ContinuationToken and use it on another query
if(!string.IsNullOrEmpty(results.ContinuationToken)){
    feedOptions.RequestContinuation = results.ContinuationToken;
    PagedResults<T> moreResults = await client.CreateDocumentQuery<T>( collectionUri,feedOptions ).Where(predicate).ToPagedResults();
}

此外,我在 Github 上维护了一个包含 helpers and providers for DocDB that you are free to use if you want, most are based on the Performance guidelines article 和个人经验的回购协议。

另一个建议,尝试将您的 SDK 更新到最新版本,.Net Full framework or the .Net Core 版本(取决于您的项目)。