Message 1 of 3
Connect with CosmosDB

Not applicable
06-06-2021
12:44 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to send add-in usage data to CosmosDB, but Revit crashes at the moment of creating an instance of CosmosClient.
Any idea what's wrong?
// UI button code-behind
private async void BtnRun_Click(object sender, RoutedEventArgs e)
{
await ConnectionCosmos.LogUsage("Command Name", "Cmd", "Category", "No message");
}
// Connection to CosmosDB
public static class ConnectionCosmos
{
private static CosmosDBDataAccess db;
public static async Task LogUsage(string commandName, string nickname, string commandCategory, string uiFeedback)
{
CommandUsageModel doc = new CommandUsageModel(commandName, nickname, commandCategory, uiFeedback);
var c = GetCosmosInfo();
db = new CosmosDBDataAccess(c.endpointUrl, c.primaryKey, c.databaseName, c.containerName);
await db.UpsertRecordAsync(doc);
}
private static (string endpointUrl, string primaryKey, string databaseName, string containerName) GetCosmosInfo()
{
(string endpointUrl, string primaryKey, string databaseName, string containerName) output;
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Autodesk\\Revit\\Addins\\2020");
//var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
var builder = new ConfigurationBuilder().SetBasePath(path).AddJsonFile("appsettings.json");
var config = builder.Build();
output.endpointUrl = config.GetValue<string>("CosmosDB:EndpointUrl");
output.primaryKey = config.GetValue<string>("CosmosDB:PrimaryKey");
output.databaseName = config.GetValue<string>("CosmosDB:DatabaseName");
output.containerName = config.GetValue<string>("CosmosDB:ContainerName");
return output;
}
}
In the above snippet I dumped the UI code-behind and the method that connects to CosmosDB.
I confirmed that the appsettings.json is being read correctly, the connection to CosmosDB should work.
If there isn't enough info here to indicate what's wrong, could someone at least share a working example of sending a document to CosmosDB from a Revit add-in?