Connect with CosmosDB

Connect with CosmosDB

Anonymous
Not applicable
454 Views
2 Replies
Message 1 of 3

Connect with CosmosDB

Anonymous
Not applicable

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?

0 Likes
455 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

Also, here's the CosmosDBDataAccess class:

 

using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DatabaseOperations.AccessCosmos
{
    public class CosmosDBDataAccess
    {
        private readonly string _endpointUrl;
        private readonly string _primaryKey;
        private readonly string _databaseName;
        private readonly string _containerName;
        private CosmosClient _cosmosClient;
        private Database _database;
        private Container _container;

        public CosmosDBDataAccess(string endpointUrl, string primaryKey, string databaseName, string containerName)
        {
            _endpointUrl = endpointUrl;
            _primaryKey = primaryKey;
            _databaseName = databaseName;
            _containerName = containerName;

            _cosmosClient = new CosmosClient(_endpointUrl, _primaryKey);
            _database = _cosmosClient.GetDatabase(_databaseName);
            _container = _database.GetContainer(_containerName);
        }

        public void UpsertRecord<T>(T record)
        {
            _container.UpsertItemAsync(record);
        }
    }
}

 

0 Likes
Message 3 of 3

Anonymous
Not applicable

I tried accessing the Cosmos DB from a brand new Revit ExternalCommand.
I get the following error:

diazs17_0-1623276772862.png

Since Microsoft.Azure.Cosmos has a .NET Standard 2.0 dependency, I also tried targeting the .NET Standard 2.0
This time I get the following error:

diazs17_1-1623276832586.png

 

Any idea what is wrong?

 

0 Likes