I am encountering a problem quite frequently in a console application (.NET Framework 4.7.2) that attempts to build and then deploy a Tabular Database to a Power BI workspace as a Semantic Model.
Whenever I try and retrieve the newly-added Database or access the Database's Model object I very frequently hit AmoExceptions or NullReferenceExceptions, especially if attempting to do so very soon after deploying the Database (even though I can see the Database through the PowerBI website).
I have been able to reduce the instability a little by coding in retry logic, but that seems untenable and unsustainable outside of a Dev environment.
For example, the (simplified) code below attempts to swap a temporary Database's Model with the Model of the standard (non-temporary) Database.
Here's my code:
// C# code
// Previous code to deploy Power BI database to PBI Workspace
Microsoft.AnalysisServices.Tabular.Server PBIServer = new Microsoft.AnalysisServices.Tabular.Server();
PBIServer.Connect(PowerBIConnectionString);
Microsoft.AnalysisServices.Tabular.Database tabularCubeFrom = PBIServer.Databases.FindByName(TabularCubeTemporaryName);
Microsoft.AnalysisServices.Tabular.Database tabularCubeTo = PBIServer.Databases.FindByName(TabularCubeName);
tabularCubeFrom.Model.CopyTo(tabularCubeTo.Model);
// Code to save changes, disconnect, etc.
Two similar errors are occurring in the code above (and related code):
- FindByName() not properly returning the Database soon after it is created (the Database can be seen in PowerBI)
- The Model object on the Database object not being populated (such as in the line calling CopyTo() above)
If I debug the issue and then retry the code after a slight delay, however, the Database and Model will be populated. Again, logic to retry the erroring code a few times after a slight delay reduces the instability but I don't believe that's a good "solution".
Is there a reliable way to ensure the Model object is fully loaded without special logic to retry after a delay?