英文:
Can `CosmosClient.CreateAndInitializeAsync` be safely used with dependency injection?
问题
以下是翻译好的内容:
作为我的应用程序托管在 AKS 上,可能会重新启动容器,因此这种优化会很有用。
通常我会使用 ConfigureServices
在 IServiceCollection
中注册 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论