While working with the ShapeAppMacroRecording, a sample included in the updated release of VSTA v 2, I noticed a couple of problems. They are listed here with workarounds. If you have found a problem that is not listed here, or have a question please let me know by posting on the forum.
1) Changes made in the template used for macros, or selecting a different template to use for macros, doesn't work.
Make sure you delete all macro folders and files from %MyDocuments%\ShapeAppCSharp to force ShapeApp to CreateNewMacroProject instead of OpenMacroProject.
2) The ShapeAppMacro project fails to build for the following reason:
Unexpected error creating debug information file %My Documents%\ShapeAppCSharp\Macros\obj\Debug\ShapeAppMacros.PDB' -- '%My Documents%\ShapeAppCSharp\Macros\obj\Debug\ShapeAppMacros.pdb: The process cannot access the file because it is being used by another process.
This is a known issue and has been reported as a bug to Microsoft. This usually happens when a macro is recorded after a build (for example a build from the VSTA DTE or from recording a previous macro). The macro project will build in a VSTA DTE not associated with the running host, but will continue to fail in a VSTA DTE associated with a running host.
This is a timing issue and happens only during debugging. The work around is to add an additional catch to the DeletePdb method. The entire method is shown here:
private static void DeleteMacroPdb()
{
string projectPath = Path.GetDirectoryName(VstaDesignTimeIntegration.MacroProjectFilePath);
string[] pdbFiles = Directory.GetFiles(projectPath, "*.pdb", SearchOption.AllDirectories);
// Delete all PDB files for the project under \obj\.. and \bin\.. folders.
// If a file is locked, retry to delete that file after 100 milliseconds.
foreach (string pdbFile in pdbFiles)
{
bool done = false;
int retry = 0;
// Retry a maximum of 5 seconds to delete the file.
while (!done && retry <= 50)
{
try
{
File.Delete(pdbFile);
done = true;
}
catch (IOException ex)
{
System.Diagnostics.Debug.Write(ex.Message);
Thread.Sleep(100);
retry++;
}
// Add this catch
catch (UnauthorizedAccessException ex)
{
Thread.Sleep(100);
retry++;
}
}
}
}
3) When debugging after finishing a macro recording the program hangs when stepping into the following line which is called from VstaRuntimeIntegration.UnloadCurMacroAddIn (try{this.macroAddIn.OnShutdown();})
namespace Microsoft.VisualStudio.Tools.Applications.Samples.ShapeApp
[global::System.AddIn.Pipeline.AddInBaseAttribute(ActivatableAs = new global::System.Type[] { typeof(Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint) })]
public partial class ApplicationEntryPoint : global::Microsoft.VisualStudio.Tools.Applications.Runtime.IExtendedEntryPoint
{
void Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint.OnShutdown()
{
this.OnShutdown(); //hangs here with message
}
}
}
Workaround- This may be related to issue #2 listed above. You cannot step into (F11) this because pbd bits are not available. I'm not sure why it hangs instead of stepping over like it would for any other file that it does not have source code for; however, selecting step over (F10) avoids this issue. I got this work around from here.
4) My macro or add-in project fails to build with errors like "'ShapeAppCSharpAppAddIn1.AppAddIn' does not contain a definition for 'Startup'...".
In the VSTA DTE Project Explorer, select the Show All Files icon and expand the node for the main code file (AppAddIn). If there is an "empty" or "missing" icon for the file like AppAddIn.designer.cs, then there was an error when extracting the files from the template. Close the VSTA DTE (and the host if running) and rerun the "vsta /hostID <hostID> /setup" command, wait a bit to let the setup finish, then re-open the VSTA DTE with "vsta /hostID <hostID>" and try re-opening the project. If this does not resolve the problem, confirm there is a file like AppAddIn.designer.cs in the template. If there is not, re-create the template with ProjectGen.