You should surround your code with try {} catch {} to see what errors you get. I can see at least one bug in this snippet:
if (layer is BasicFeatureLayer)
{
FeatureLayer featureLayer = layer as FeatureLayer;
//THIS ONLY GETS CALLED ONCE - THEN NEVER AGAIN!
await QueuedTask.Run(() =>
{
var table = featureLayer.GetTable();
TableDefinition tableDefinition = table.GetDefinition();
string alias = tableDefinition.GetAliasName();
//do something here with alias name.....e.g.
System.Diagnostics.Debug.WriteLine($"The alias name is: {alias}");
});
}
The problem is that AnnotationLayer, DimensionLayer and FeatureLayer all derive from BasicFeatureLayer. So in your code you check if layer is of the type 'BasicFeatureLayer' and if true you assign the casted layer to the featureLayer variable:
var featureLayer = layer as FeatureLayer;
Needless to say, your featureLayer variable will be null for Dimension or Annotation layers. Consequently, your code is using featureLayer without checking for null which will throw an exception.
The code snippet will work for maps with no Dimension or Annotation layers.