TableContinuationToken没有正确地从JSON反序列化

时间:2016-07-19 16:50:31

标签: json azure azure-table-storage

我在尝试从Azure TableStorage检索大型数据集时遇到问题。经过几次试图一次性尝试,我放弃了,现在正在使用TableContinuation令牌,现在没有正确反序列化。对象正在创建,但所有Next ...值(即NextRowKey,NextPartitionKey,等等是NULL,当创建的stringresponse中,您可以看到应填充的值......

我传递的类包含一个对象列表和令牌

public class FlorDataset 
{
    public List<FlorData> Flors { get; set; }
    public TableContinuationToken Token { get; set; }
}

控制器代码也不完全是火箭科学......

        [HttpGet, Route("api/list/{token}")]
    public IHttpActionResult FindAll(string token)
    {
        try
        {
            TableContinuationToken actualToken = token == "None"
                ? null
                : new TableContinuationToken()
                {
                    NextPartitionKey = NextPartition,
                    NextRowKey = token,
                    NextTableName = NextTableName
                };

            var x = Run(actualToken);
            Flors = x.Flors;
            actualToken = x.Token;
            NextTableName = actualToken.NextTableName;
            NextPartition = actualToken.NextPartitionKey;

                return Flors != null
                    ? (IHttpActionResult)new IsoncOkResult<FlorDataset>(x, this)
                    : NotFound();

        }
        catch (Exception ex)
        {
            Trace.TraceError(ex.ToString());
            return NotFound();
        }
    }

    private  FlorDataset Run(TableContinuationToken token)
    {
        return _repo.GetAllByYear("2016", token) as FlorDataset;
    }

调用我相当标准的Web API 2控制器的调用代码是:

                do
            {
                try
                {
                    HttpResponseMessage response = null;
                    if (string.IsNullOrEmpty(token.NextRowKey))
                    {
                        response = await client.GetAsync("api/list/None");
                    }
                    else
                    {
                        response = await client.GetAsync($"api/list/{token.NextRowKey}");
                    }
                    if (response.IsSuccessStatusCode)
                    {
                        var stringresponse = await response.Content.ReadAsStringAsync();
                        var ds = JsonConvert.DeserializeObject<FlorDataset>(stringresponse);
                        token = ds.Token;
                        Flors.AddRange(ds.Flors);
                    }
                    else
                    {
                        token = null;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    token = null;
                }
            } while (token != null);

1 个答案:

答案 0 :(得分:0)

好吧,这不是最好的解决方案,但它是迄今为止唯一有效的事情,以防其他人在尝试同样的问题而绊倒我的问题....

在调用代码位中,你在进行反序列化之前做了一些可怕的字符串替换....我真的觉得很脏,只是张贴这个,所以如果有人想出更好的答案,请随时分享.. ...

if (response.IsSuccessStatusCode)
{
    var stringresponse = await response.Content.ReadAsStringAsync();
    stringresponse = stringresponse.Replace(">k__BackingField", "");
    stringresponse = stringresponse.Replace("<", "");
    var ds = JsonConvert.DeserializeObject<FlorDataset>(stringresponse);
    token = ds.Token;
    Flors.AddRange(ds.Flors);                      
}

不好,不漂亮,但确实有用!!!! :-D现在用漂白剂洗我的手指!