[英]Cancellation Token Disposed Exception
我有這段代碼,最初工作正常。 但是,在將其添加到我存儲重用的方法的類之后,它會一直失敗。 捕獲的異常表明CancellationTokenSource已被Disposed。 有人能指出我正確的方向嗎?
我已嘗試從HTTPClient類創建一個新客戶端並將CancellationToken.None添加到PutAsync()方法,但它仍然因CancellationTokenSource Disposed異常而失敗。
public async void AddProduct(Product product)
{
string storeId = "";
try
{
var storeData = JObject.Parse(Connect.Json).SelectToken("store").ToString();
var stores = JsonConvert.DeserializeObject<List<Store>>(storeData);
var store = stores[0];
storeId = store.Id;
store.Products.Add(product);
ProdInfo info = new Info();
foreach(Product p in store.Products)
{
info.AddedProducts = + p.Id;
}
var content = JsonConvert.SerializeObject(info);
using (Connect.Client)
using (var response = await Connect.Client.PutAsync(_url + "/stores/" + storeId, new StringContent(content)))
{
var cont = response.Content;
string result = await cont.ReadAsStringAsync();
if ((int)response.StatusCode == 200)
{
this.JobResult = result;
//this.JobResult = "Store has been successfully updated";
}
else
{
this.JobResult = result;
//this.JobResult = "Store was not updated!";
}
}
}
catch (Exception ex)
{
//this.JobResult = "Store has not been updated due to an error.";
this.JobResult = ex.Message;
}
}
我能夠通過簡單地從我的所有方法中刪除'using(Connect.Client)'來解決這個問題。 正如@sellotape所說,在我能夠再次使用之前,他們正在處理HttpClient。 謝謝大家的貢獻。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.