【问题标题】:Session only captures the data of the last object within the listSession 只捕获列表中最后一个对象的数据
【发布时间】:2019-02-17 02:02:41
【问题描述】:

Session["DeviceIDs"] 有一个存储有两个 id 的列表。 Foreach 方法会将每个 id 传递给 web url/deviceIDs。唯一被读取的 id 是最后一个 id。如何传递存储在 Session["DeviceIDs] 中的所有 id?这与 var response = await httpClient.SendAsync(request); 有什么关系吗?提前谢谢

    public async void PatchMethodAsync()
    {
        using (var httpClient = new HttpClient())
        {
            if (Session["DeviceIDs"] != null)
            {
                //Check all ids in Session
                foreach (var ids in Session["DeviceIDs"].ToString())
                {
                    //Pass each individual id from Session to device id in url
                    using (var request = new HttpRequestMessage(new HttpMethod("PATCH"), "https://www.website.com/devices/" + Session["DeviceIDs"]))
                    {
                        request.Content = new StringContent("{\"enabled\": \"y\"}", Encoding.UTF8, "application/json");

                        var response = await httpClient.SendAsync(request);
                    }

                }
            }
            else
            {
                //Do something
            }
        }
    }

【问题讨论】:

    标签: c# json api session-state


    【解决方案1】:

    我相信你正在寻找这样的东西

    List<Task<ResponseObject>> tasks = new List<Task>();
    foreach (var id in Session["DeviceIDs"].ToString())
    {
        ...//create the request Message using id
        tasks.Add(httpClient.SendAsync(request));
    }
    
    var responses = await Task.WhenAll(tasks.ToArray());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多