An ASP.NET Core minimal API uses the Microsoft.Azure.Cosmos .NET SDK with DefaultAzureCredential to read items from the product container in the cosmicworks database. Requirements:
- The process must use exactly one
CosmosClientfor its whole lifetime. - The first incoming request must not pay the connection setup and routing discovery cost.
- The client must be disposed when the host stops.
How should you create and register the client in Program.cs?
Choose one answer.
- Create a client with
CreateAndInitializeAsyncforproductinside each request handler, wrapped in ausingdeclaration - Create it with
CreateAndInitializeAsyncforproduct, register it withAddSingleton(client), and keep itsusingscope alive until the host stops - Register
AddSingleton<CosmosClient>with a factory delegate that constructs the client and callsGetContainerforproduct - Create it with
CreateAndInitializeAsyncforproduct, register it withAddSingleton(client), and add no shutdown code - Register
AddSingleton<CosmosClient>with a factory delegate that calls theCosmosClientconstructor, and add no shutdown code