在 nopCommerce 3.9 计划任务中调用异步方法



我正在编写一个计划任务,用于刷新来自tradeGecko的访问令牌。这是任务的代码:

using Newtonsoft.Json.Linq;
using Nop.Core;
using Nop.Core.Domain.Orders;
using Nop.Services.Configuration;
using Nop.Services.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static Nop.Core.TradeGeckoHelperMethods;
namespace Nop.Services.Catalog
{
public partial class TradeGeckoRefreshToken : ITask
{
private readonly IStoreContext _storeContext;
private readonly ISettingService _settingService;
public TradeGeckoRefreshToken(IStoreContext storeContext, ISettingService settingService)
{
_storeContext = storeContext;
_settingService = settingService;
}
public async System.Threading.Tasks.Task UpdateRefreshToken()
{
var orderSettings = _settingService.LoadSetting<OrderSettings>();
Task<JObject> returnObject = TradeGeckoHelperMethods.RefreshAccessToken(orderSettings.TradeGeckoApplicationId, orderSettings.TradeGeckoSecretId, orderSettings.TradeGeckoCallBackUrl, orderSettings.TradeGeckoRefreshToken);
JObject refresheObject = await returnObject;
if (refresheObject[JsonKeys.AccessToken] != null)
{
Console.WriteLine(refresheObject[JsonKeys.AccessToken].ToString());
orderSettings.TradeGeckoAccessToken = refresheObject[JsonKeys.AccessToken].ToString();
orderSettings.TradeGeckoRefreshToken = refresheObject[JsonKeys.RefreshToken].ToString();
_settingService.SaveSetting(orderSettings);
_settingService.ClearCache();
}
}
public void Execute()
{
System.Threading.Tasks.Task t = UpdateRefreshToken();
Thread.Sleep(2000);
}
}
}

从管理面板手动运行任务时,有时会产生以下错误:

消息=已有一个与此命令关联的打开的 DataReader,必须先关闭该读取器。

我做得对吗?或者有一个更好的 a 来做到这一点。

请在连接字符串中添加此 MultipleActiveResultSets=True。

请参阅此链接以获取更多信息。

https://www.nopcommerce.com/boards/t/16243/there-is-already-an-open-datareader-error-on-25.aspx

相关内容

  • 没有找到相关文章

最新更新