【发布时间】:2021-11-19 13:10:31
【问题描述】:
在这种情况下,我根据解释了解发生了什么以及错误原因。
然后我最近在应用http://www.asyncfixer.com/建议的最佳实践后遇到了同样的错误
代码是一个自定义本地化程序,它使用 SQL 数据库而不是资源文件作为存储。简而言之,如果它尚未缓存在内存中,它会提供来自 db 的字符串。此外,如果一个字符串不在 db 中,它也会将它添加到 db 中。
有问题的代码片段如下(2 种方法)。要发生错误,必须对与异步等待语法相关的 cmets 中的两种方法进行 3 处更改
我想通过这些更改了解有关此异常的内部原理
方法一
public async Task<LocalizedString> GetResourceAsync(string resourceKey, CultureInfo culture, string location = "shared")
{
var tenant = ((MultiTenantContextAccessor<AppTenant>)_serviceProvider.GetRequiredService(typeof(IMultiTenantContextAccessor<AppTenant>))).MultiTenantContext.TenantInfo;
if (string.IsNullOrWhiteSpace(resourceKey))
throw new ArgumentNullException(nameof(resourceKey));
var cacheKey = $"{location}.{resourceKey}";
if (!_cacheProvider.TryGetValue(tenant.Id, cacheKey, culture, out var value))
{
using (var scope = _serviceProvider.GetScopedService(out T context))
{
var item = context
.Set<LocalizationResource>()
.SelectMany(r => r.Translations)
.Where(t => t.Resource.ResourceKey == resourceKey
&& t.Resource.Module == location
&& t.Language == culture.Name)
.Select(p => new
{
p.LocalizedValue
})
.SingleOrDefault();
//change 1) change above to use **await context** {...} **SingleOrDefaultAsync()**
if (item == null)
if (_settings.CreateMissingTranslationsIfNotFound)
await AddMissingResourceKeysAsync(resourceKey, location); //AddMissingResourceKeys(resourceKey);
value = item?.LocalizedValue ?? string.Empty;
if (string.IsNullOrWhiteSpace(value))
switch (_settings.FallBackBehavior)
{
case FallBackBehaviorEnum.KeyName:
value = resourceKey;
break;
case FallBackBehaviorEnum.DefaultCulture:
if (culture.Name != DefaultCulture.Name)
return await GetResourceAsync(resourceKey, DefaultCulture,location);
break;
}
}
_cacheProvider.Set(tenant.Id, cacheKey, culture, value);
}
return new LocalizedString(resourceKey, value!);
}
方法二
private async Task AddMissingResourceKeysAsync(string resourceKey, string location="shared")
////Change 2): remove async from method signature to
////private Task AddMissingResourceKeysAsync(string resourceKey, string location="shared")
{
var modificationDate = DateTime.UtcNow;
//resourceKey = $"{location}.{resourceKey}";
using var scope = _serviceProvider.GetScopedService(out T context);
var resource = context
.Set<LocalizationResource>()
.Include(t => t.Translations)
.SingleOrDefault(r => r.ResourceKey == resourceKey && r.Module==location);
if (resource == null)
{
resource = new LocalizationResource
{
Module = location,
ResourceKey = resourceKey,
//Modified = modificationDate,
Translations = new List<LocalizationResourceTranslation>()
};
context.Add(resource);
}
_requestLocalizationSettings.SupportedCultures.ToList()
.ForEach(culture =>
{
if (resource.Translations.All(t => t.Language != culture.Name))
{
//resource.Modified = modificationDate;
resource.Translations.Add(new LocalizationResourceTranslation
{
Language = culture.Name
//, Modified = modificationDate
});
}
});
await context.SaveChangesAsync();
////change 3) change above to return context.SaveChangesAsync();
}
一旦完成上述 3 项更改,它就会始终抛出此异常,如果我删除这些更改,它会正常工作。我想知道发生此异常的幕后可能发生了什么
【问题讨论】:
-
至少显示调用堆栈。还有其他功能会产生问题。
-
为简洁起见,我避免放置调用堆栈。 Callstack 正是指向我提到的这些行。方法 1 调用方法 2 将缺少的资源添加到 db ......所以基本上这两种方法都在读取/写入 db ......所以很有可能这就是原因......但不确定为什么添加 async/await 更改会引发例外。
-
你可能会觉得这很有趣:Eliding Async and Await.
标签: c# async-await entity-framework-core