2

I am trying to constrcut a net core console app project programmatically. To do this, I coded like below. The problem is ..... FileCodeModel instance "fcm" is always null in the code. if I forcefully show the messagebox after calling AddFromTemplate(), the issue does not occur. but i do not prefer this workaround. does anyone know why this issue ouucrs and how to avoid it?

string itemName = "View.cs";
string itemTemplatePath =solution2.GetProjectItemTemplate("CodeFile", "CSharp");        

destProject.ProjectItems.AddFromTemplate(itemTemplatePath, itemName);

//if I forcefully show the msgbox, fcm is not null.!!
//MessageBox.show("ok","ok");

ProjectItem destProjectItem = null;
foreach(ProjectItem projectItem in destProject.ProjectItems)
{
if(projectItem.Name == itemName)
     {
     destProjectItem = projectItem;
      break;
          }
}  

FileCodeModel fcm = destProjectItem.FileCodeModel; 
if (fcm == null)
 throw new ArgumentNullException("fcm is null!!");

fcm.AddNamespace("MyNameSpace");

now I am using VS 2017 latest version wit net core sdk.

Jinwoo Nam
  • 21
  • 2
  • Given showing that MessageBox appears to fix the issue, it would appear this is a timing issue. The project system needs time to actually generate the underlying model. Consider invoking FileCodeModel2.Synchronize() to ensure the model is generated before attempting to retrieve. – Ed Dore Jun 07 '18 at 05:39

0 Answers0