如何在Todictionary C#中使用TryCatch语句?

发布于 2025-01-27 12:20:24 字数 1035 浏览 3 评论 0原文

我有一个异步电话的字典。问题有时是这些异步电话会引发例外,但我不确定如何处理它们。 getUserlicenseTypes返回列表,因此,如果我确实得到异常,我仍然想返回一个空列表。

var tasks = allCompanyUsers.ToDictionary(
                u => u.Id,
                u => _subscriptionService.GetUserSubscription(companyId, u.Id));

            await Task.WhenAll(tasks.Values);

            var licenseListByUserId = tasks.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value.Result);

下面的代码不起作用,但我想做类似的

var tasks = allCompanyUsers.ToDictionary(
                u => u.Id,
                u => {
try{
     return _subscriptionService.GetUserSubscription(companyId, u.Id);
}catch{
     return new List<string>();
}
});

            await Task.WhenAll(tasks.Values);

            var licenseListByUserId = tasks.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value.Result);

注意事项:我得到了反馈,我应该避免在一起例外,但是目前很难考虑到getUserSubscription函数已经在许多位置都使用了我们的应用程序使用的典型惯例是trycatch语句来处理这些抛出的错误。

I have a dictionary of asyncronous call . The problem is sometimes these asyncronous call will throw an exception but I am not entirely sure how to handle them . The GetUserLicenseTypes returns a list so if I do get an exception I still want to return an empty list.

var tasks = allCompanyUsers.ToDictionary(
                u => u.Id,
                u => _subscriptionService.GetUserSubscription(companyId, u.Id));

            await Task.WhenAll(tasks.Values);

            var licenseListByUserId = tasks.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value.Result);

The code below doesn't work but I wanted to do something like this

var tasks = allCompanyUsers.ToDictionary(
                u => u.Id,
                u => {
try{
     return _subscriptionService.GetUserSubscription(companyId, u.Id);
}catch{
     return new List<string>();
}
});

            await Task.WhenAll(tasks.Values);

            var licenseListByUserId = tasks.ToDictionary(
                kvp => kvp.Key,
                kvp => kvp.Value.Result);

Note : I have gotten feedback that I should just avoid exception all together but at the moment it is hard to do considering that the GetUserSubscription function is already used in many location so the typical convention our application use is a trycatch statement to handle these thrown error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2025-02-03 12:20:24

这是一种方法:

async Task<IEnumerable<string>> GetSubscriptions(Guid userId)
{
    try { return await _subscriptionService.GetUserSubscription(companyId, userId); }
    catch { return new List<string>(); }
}

var idsWithSubscriptions = await Task.WhenAll(allCompanyUsers
    .Select(async u => (
        u.Id,
        Subscription: await GetSubscriptions(u.Id))));

var licenseListByUserId = idsWithSubscriptions.ToDictionary(
    iws => iws.Id,
    iws => iws.Subscription);

因为task正在等待从getUserSubscription返回,所以它将解开并丢弃任何异常,这意味着中的值捕获将返回。

Here's one way:

async Task<IEnumerable<string>> GetSubscriptions(Guid userId)
{
    try { return await _subscriptionService.GetUserSubscription(companyId, userId); }
    catch { return new List<string>(); }
}

var idsWithSubscriptions = await Task.WhenAll(allCompanyUsers
    .Select(async u => (
        u.Id,
        Subscription: await GetSubscriptions(u.Id))));

var licenseListByUserId = idsWithSubscriptions.ToDictionary(
    iws => iws.Id,
    iws => iws.Subscription);

Because the Task returned from GetUserSubscription is awaited, it will unwrap and throw any Exception, meaning the value in the catch block will be returned.

摇划花蜜的午后 2025-02-03 12:20:24

它看起来像task&lt; string&gt;&gt;的字典值类型,但是在您的捕获块中,您返回list&lt; string&gt;。如果您返回任务,它应该有效:

var tasks = allCompanyUsers.ToDictionary(
u => u.Id,
u =>
{
   try
   {
        return _subscriptionService.GetUserSubscription(companyId, u.Id);
   }
   catch
   {
        return Task.FromResult(new List<string>());
   }
});

It looks as the type of the values of your dictionary is Task<List<string>>, but in your catch block you return a List<string>. If you return a task, it should work:

var tasks = allCompanyUsers.ToDictionary(
u => u.Id,
u =>
{
   try
   {
        return _subscriptionService.GetUserSubscription(companyId, u.Id);
   }
   catch
   {
        return Task.FromResult(new List<string>());
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文