I want to use Cosmos DB in my ASP.net 5 WebAPI project. To create the client I want to use Dependency injection to inject it to my Service. To do so I created this statement in my public void ConfigureServices(IServiceCollection services)
services.AddSingleton(async s => {
var dbConfig = Configuration.GetSection("Database");
var conn = dbConfig["ConnectionString"];
var dbName = dbConfig["DbName"];
if (string.IsNullOrEmpty(conn))
{
throw new ConfigurationErrorsException();
}
var client = new CosmosClient(conn);
await client.CreateDatabaseIfNotExistsAsync(dbName);
return client;
});
But in this case I get an error in my service when I try to inject it via:
public ProductService(ILogger<ProductService> logger, CosmosClient client)
{
this._client = client;
_logger = logger;
}
Because my arrow function returns Task<CosmosClient>
instead of CosmosClient
. But how can I change that therefore Database-creation methos is async and I have to use await?!
THanks