using AWS DynamoDB

using AWS DynamoDB

lionel.kai
Advisor Advisor
880 Views
10 Replies
Message 1 of 11

using AWS DynamoDB

lionel.kai
Advisor
Advisor

First off, I'm just trying to add shared database functionality to my add-in - if there's a different/easier provider anyone can recommend, with sample code, go for it. 🙂

 

per .NET code examples - Amazon DynamoDB I added 4 lines to app.config:

 

  <appSettings>
    <add key="AWSProfileName" value="default"/>
    <add key="AWSRegion" value="us-west-2" />
  </appSettings>

 

I then made a button to call the test function based on Hello DynamoDB. I added a few TaskDialogs to try to see progress along the way.

 

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class TestAWSDynamoDB : IExternalCommand
    {
        public static string message = "";

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            TaskDialog.Show("TestAWSDynamoDB", "Testing...");
            TaskDialog.Show("TestAWSDynamoDB", message);

            /*
            AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
            // This client will access the US East 1 region.
            clientConfig.RegionEndpoint = RegionEndpoint.USEast1;
            AmazonDynamoDBClient client = new AmazonDynamoDBClient(clientConfig);*/

            Task task = Test();

            TaskDialog.Show("TestAWSDynamoDB", "...testing");

            return Result.Succeeded;
        }

        static async Task Test()
        {
            var dynamoDbClient = new AmazonDynamoDBClient();

            message += "Hello Amazon Dynamo DB! Following are some of your tables:\n\n";

            TaskDialog.Show("TestAWSDynamoDB", message);

            // You can use await and any of the async methods to get a response.
            // Let's get the first five tables.
            var response = await dynamoDbClient.ListTablesAsync(
                new ListTablesRequest()
                {
                    Limit = 1
                });

            message += "response.ContentLength: " + response.ContentLength + "\n\n";

            TaskDialog.Show("TestAWSDynamoDB", "response.ContentLength: " + response.ContentLength);

            foreach (var table in response.TableNames)
            {
                TaskDialog.Show("TestAWSDynamoDB", $"\tTable: {table}");

                //Console.WriteLine($"\tTable: {table}");
                //Console.WriteLine();
            }
        }
    }

 

I see the first "Testing..." and the blank (message) dialogs, then after a pause the "...testing" dialog, but none of the dialogs from within the Test() method. And when I click the button a second time, message is still blank.

 

I don't have any thread experience in C# (and it's been MANY years since I've done Java), so I'm mostly going by the sample code and VisualStudio's hints (for the await, async, & Task task =). I would like to have it be threaded eventually (so the user doesn't have to wait for saves to the DB), but at this point I'd be fine with it being single threaded and locking the UI until done. I've considered just using text files on the server, but I'd like to also get info from users' home machines...


Lionel J. Camara
BIM Manager at KAI Hawaii, Inc. - Structural and Forensic Engineers
Autodesk Certified Professional
0 Likes
881 Views
10 Replies
Replies (10)
Message 2 of 11

jeremy_tammik
Alumni
Alumni

I implemented several samples demonstrating connecting the desktop and the cloud based on Revit add-ins on the Windows desktop and MongoDB NoSql cloud databases:

   

     

I hope you find them useful.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 11

mhannonQ65N2
Collaborator
Collaborator

You might not be able to call TaskDialog.Show from another thread. Try making Test() not async; you'll just have to replace await dynamoDbClient.ListTablesAsync(...) with dynamoDbClient.ListTablesAsync(...).Result.

Message 4 of 11

lionel.kai
Advisor
Advisor

[This is the status update - code & new question in next posts]

@mhannonQ65N2 Thank you! That (by itself) didn't "fix" it, but it at least got me a proper error message: 

2023-09-26_Unable to find credentials.png

That sent me on a whole journey (with various errors) trying to figure out how to properly specify the credentials. I'm not sure why specifying the credentials in AWS Explorer (per the getting started tutorial) didn't work, but I eventually wound up just using this overload:

public AmazonDynamoDBClient(string awsAccessKeyId, string awsSecretAccessKey, RegionEndpoint region);

Even generating the access keys was a whole journey in itself - trying to navigate AWS's web interface - I wound up creating a new (different type of) user. Then I got this error:

2023-09-28_Could not load file or assembly.png

That was an easy fix as I just copied the 6 files (AWSSDK.Core & AWSSDK.DynamoDBv2: .dll, .pdb, & .xml) into my deployment folder and it worked! It showed me the name of the test table! That was last night / this morning, and now as I was going to post the final code here, after doing some cleanup, it's not working again!😵🙄

 

Thanks also to @jeremy_tammik for suggesting the example app - I'll be checking out the code at some point. I did take a look at the MongoDB site, but it seems like they're hosted on AWS anyway, so I wonder if I'll have the same issues...🤔


Lionel J. Camara
BIM Manager at KAI Hawaii, Inc. - Structural and Forensic Engineers
Autodesk Certified Professional
Message 5 of 11

lionel.kai
Advisor
Advisor

[This is the latest edited code - it IS working ok]

EDIT: I've got to pay more attention to what I name my variables - "message" LOL.🙄

 

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon;

namespace Buttons2021
{
    //https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/CodeSamples.DotNet.html
    //The AWS SDK for .NET provides thread-safe clients for working with DynamoDB. As a best practice, your applications should create one client and reuse the client between threads.

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class TestAWSDynamoDB : IExternalCommand
    {
        public static string logMessage = "";

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            TaskDialog.Show("TestAWSDynamoDB", "1:\n\n" + logMessage);

            Test();

            TaskDialog.Show("TestAWSDynamoDB", "2:\n\n" + logMessage);

            return Result.Succeeded;
        }

        static void Test()
        {
            var dynamoDbClient = new AmazonDynamoDBClient("<accessKey>", "<secretKey>", RegionEndpoint.USWest2);

            logMessage += "\nHello Amazon Dynamo DB! Following are some of your tables:\n\n";

            // Let's get the first five tables.
            var response = dynamoDbClient.ListTablesAsync(
                new ListTablesRequest()
                {
                    Limit = 5
                }).Result;

            logMessage += "response.ContentLength: " + response.ContentLength + "\n\n";

            foreach (var table in response.TableNames)
            {
                logMessage += $"\n\tTable: {table}";
            }
        }

    }

}

 

 


Lionel J. Camara
BIM Manager at KAI Hawaii, Inc. - Structural and Forensic Engineers
Autodesk Certified Professional
Message 6 of 11

lionel.kai
Advisor
Advisor

[This is the new error]

2023-09-28_Could not load type 'Amazon.Runtime.IDefaultConfiguration'.png

I've tried deleting AWSSDK.Core & AWSSDK.DynamoDBv2 from the References in the solution, and updating both with NuGet (to 3.7.202.17 & 3.7.202.3), but that didn't seem to change the versions under Reference, though it seems to match what the error dialog above is looking for (3.3.0.0):

2023-09-28_AWSSDK.Core_version.png

but the properties of the actual .dll that gets copied has a different number for File Version:

2023-09-28_AWSSDK.DynamoDBv2_dll-properties.png


Lionel J. Camara
BIM Manager at KAI Hawaii, Inc. - Structural and Forensic Engineers
Autodesk Certified Professional
0 Likes
Message 7 of 11

lionel.kai
Advisor
Advisor

I just tried rolling back the AWSSDK packages (using NuGet) to 3.3.0 version (from 7 years ago) and it works again! I'm not sure how I feel about intentionally using 7-year-old packages, but it seems to work ok now and I don't really wanna mess with it... even though I'd like to try something in between.


Lionel J. Camara
BIM Manager at KAI Hawaii, Inc. - Structural and Forensic Engineers
Autodesk Certified Professional
Message 8 of 11

mizrachi_amir
Contributor
Contributor

Hi @lionel.kai

 

I encountered now a similar error to your, downgraded my awssdk package to 3.3.0 but now my region (eu-north-1) is missing from the region enum. Any suggestion for that?

0 Likes
Message 9 of 11

mizrachi_amir
Contributor
Contributor

I managed to overcome this by using this instead:

var serviceURL = "https://dynamodb.eu-north-1.amazonaws.com"
0 Likes
Message 10 of 11

lionel.kai
Advisor
Advisor

@mizrachi_amir I'm glad you got yours working - I never did. I wound up just using text files for now. I'm not a full-time developer, so I didn't have the time to devote to troubleshooting it. I'll try again eventually...


Lionel J. Camara
BIM Manager at KAI Hawaii, Inc. - Structural and Forensic Engineers
Autodesk Certified Professional
0 Likes
Message 11 of 11

mizrachi_amir
Contributor
Contributor
Don't hesitate to contact me and I'll try to assist with your code
0 Likes