即使为Access to disposed closure
/ [InstantHandle]
设置了Action
属性,我也收到了警告Func
。
我的IDE warning screenshot with code和Resharper settings。
如果重写为同步版本(使用Wait()
执行任务),也会出现问题。
版本:
目标.NET Framework :4.6.2
Visual Studio :2017 Enterprise, 15.6.6
ReSharper :2018.1
// Nuget package
using JetBrains.Annotations;
public static void SendMessageAsync(string message)
{
Task.Factory.StartNew(async () => {
var form = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("chat_id", "1"),
new KeyValuePair<string, string>("message", message),
});
await ForTryAsync(nameof(SendMessageAsync), async () => {
using (var resp = await Http.PostAsync("/sendMessage", form))
resp.EnsureSuccessStatusCode();
}, false);
form.Dispose();
});
}
public static async Task ForTryAsync(string methodName, [InstantHandle] Func<Task> action)
{
Exception innerException = null;
for (int i = 0; i < 5; i++)
{
try
{
await action();
return;
}
catch (Exception ex)
{
innerException = ex;
// Some error handling here
}
}
throw new Exception($"Method \"{methodName}\" failed after 5 retries. Last error: ${innerException?.Message}", innerException);
}
谢谢。