I need to get the connectionstring from usersecrets. I tried getting it that way:
CosmosClient cosmos = new CosmosClient("CosmosConnectionString", new CosmosClientOptions
{
ConnectionMode = ConnectionMode.Gateway
});
But this doesn't work, since the CosmosClient understands only the literal string. I tried injecting Azure Functions with usersecrets, and for that I made a startup class:
[assembly: FunctionsStartup(typeof(Startup))]
namespace AltProject
{
public class Startup : FunctionsStartup
{
// override configure method
public override void Configure(IFunctionsHostBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", true)
.AddUserSecrets(Assembly.GetExecutingAssembly(), false)
.AddEnvironmentVariables()
.Build();
builder.Services.AddSingleton<IConfiguration>(config);
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
Console.WriteLine(" {0} = {1}", de.Key, de.Value); // register your other services
}
}
}
Then I tried to get the connectionstring in this way in the CosmosClient:
CosmosClient cosmos = new CosmosClient(System.Environment.GetEnvironmentVariable("CosmosConnectionString", EnvironmentVariableTarget.Process), new CosmosClientOptions
{
ConnectionMode = ConnectionMode.Gateway
});
But this gives me no results, it seems the Environment Values don't get injected with the usersecrets. Is there a way to make this work?