`CosmosClient.CreateAndInitializeAsync` 可以安全地与依赖注入一起使用吗?

huangapple go评论67阅读模式
英文:

Can `CosmosClient.CreateAndInitializeAsync` be safely used with dependency injection?

问题

以下是翻译好的内容:

作为我的应用程序托管在 AKS 上,可能会重新启动容器,因此这种优化会很有用。

通常我会使用 ConfigureServicesIServiceCollection 中注册 CosmosClient 的单例实例,然而这不能异步运行。我可以同步运行 CreateAndInitializeAsync,但这可能会阻塞应用程序启动。如果在此处使用 CreateAndInitializeAsync 时发生临时故障,也可能会阻塞启动。

在 .NET Core 应用程序中安全地使用 CreateAndInitializeAsync 的方法是什么?

英文:

I have been reading this Microsoft blog around optimizing CosmosDB initialisation.
TLDR; you can improve latency on your first few calls by using CreateAndInitializeAsync

As my application is hosted on AKS the pods could potentially be restarted so this optimization would be nice to have.

I would typically use ConfigureServices to register a singleton instance of CosmosClient in an IServiceCollection, however this cannot be run asynchronously. I could run CreateAndInitializeAsync synchronously instead but that could potentially block application start-up. A transient failure with CreateAndInitializeAsync could also block start-up if used here.

Is there a way of safely utilizing CreateAndInitializeAsync in a .NET Core Application?

答案1

得分: 1

CreateAndInitializeAsync 应该正确处理瞬态故障,以便即使存在影响初始化优化的网络条件,仍能完成执行。

唯一应抛出异常的情况是如果方法的输入无效(例如,传递了空的容器列表,或者名称为null,或者身份验证密钥/令牌为空)。

英文:

CreateAndInitializeAsync should handle transient failures correctly in order to still complete execution even if there are network conditions affecting the initialization optimization.

The only scenarios where an exception should be thrown is if the inputs to the method are invalid (for example, you are passing an empty list of Containers, or the names are null, or the authentication keys/tokens are empty).

huangapple
  • 本文由 发表于 2023年7月6日 22:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定