In my add-in, I'm creating random points using a constraining feature class with the Create Random Points tool. However, since the tool creates the number of points for each feature in the constraining feature class, I first dissolve the feature class and use that as the constraining feature class. I use a temporary file name for the dissolved feature class and delete it after creating the points.
private Geodatabase MemoryGeodatabase; //set elsewhere
private SchemaBuilder MemorySchemaBuilder; //set elsewhere
MemoryConnectionProperties memoryConnectionProperties = new();
string tempFileName = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
using (MemoryGeodatabase ??= SchemaBuilder.CreateGeodatabase(memoryConnectionProperties))
{
MemorySchemaBuilder ??= new(MemoryGeodatabase);
StatusMessage = $"Dissolving {SelectedFrameFeatureLayer.Name}...";
IGPResult dissolveResult = await DissolveFeatures(frameFeatureClass, "", "", $"memory\\{tempFileName}", GPExecuteToolFlags.AddToHistory);
if (dissolveResult.IsFailed)
{
Show_Status_Bar = false;
StatusMessage = "";
MessageBox.Show("Dissolving feature class failed");
return;
}
using (FeatureClass dissolveFC = MemoryGeodatabase.OpenDataset<FeatureClass>(tempFileName))
{
string Distance = "";
if (Use_Minimum_Distance && Minimum_Distance > 0) Distance = $"{Minimum_Distance} {SR.Unit.ToString().ToLower()}";
StatusMessage = "Generating points...";
IGPResult CreateRandomPointsResult = await CreateRandomPoints($"memory\\{tempFileName}", Path.GetDirectoryName(outputFileName), Path.GetFileName(outputFileName), SimpleRandomCount, Distance, GPExecuteToolFlags.AddOutputsToMap);
FeatureClassDescription DissolveFCDescription = new(dissolveFC.GetDefinition());
MemorySchemaBuilder.Delete(DissolveFCDescription);
MemorySchemaBuilder.Build();
}
}This runs properly the first time, but if I run it again, it throws an error on line 16 when it gets the temporary layer. The layer is created according to the history, but I get the error
"Could not open the dataset 'tmp5EB3' of type FeatureClass. Please make sure a valid geodatabase or data store has been opened first."

Why isn't this working on the second attempt?