Unable to cast object of type 'System.Object' to type 'InstantiateToolWindowData'.
Frequently receive the following error when debugging VSIX. Only resolution is to reset the experimental instance. However, it then occurs after some unknown circumstances.
System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'InstantiateToolWindowData'.
at Community.VisualStudio.Toolkit.ToolkitPackage.InstantiateToolWindow(Type toolWindowType, Object context)
at Microsoft.VisualStudio.Shell.Package.<>c__DisplayClass84_0.<CreateToolWindow>g__CreateToolWindowWorker|1()
at Microsoft.VisualStudio.Shell.Package.CreateToolWindow(Type toolWindowType, Int32 id, UInt32 flags, Object context, Guid persistenceGuid)
at Microsoft.VisualStudio.Shell.Package.CreateToolWindow(Type toolWindowType, Int32 id, ProvideToolWindowAttribute tool, Object context)
at Microsoft.VisualStudio.Shell.AsyncPackage.<CreateToolWindowAsync>d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Shell.AsyncPackage.<>c__DisplayClass45_0.<<FindToolWindowAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Shell.ToolWindowCollection.ToolWindowListener.<>c__DisplayClass1_0.<<-ctor>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Threading.AwaitExtensions.ExecuteContinuationSynchronouslyAwaiter`1.GetResult()
at Microsoft.VisualStudio.Threading.AsyncLazy`1.<>c__DisplayClass13_0.<<GetValueAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Shell.AsyncPackage.<FindToolWindowAsync>d__45.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Shell.AsyncPackage.<ShowToolWindowAsync>d__46.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Community.VisualStudio.Toolkit.BaseToolWindow`1.<ShowAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyPackage.MyToolWindowCommand.<ExecuteAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Community.VisualStudio.Toolkit.BaseCommand`1.<>c__DisplayClass10_0.<<Execute>b__0>d.MoveNext()
Hmm, that's strange. Are any of your extensions overriding the InitializeToolWindowAsync method? That's the only way I can see that the context would be an Object instead of an InstantiateToolWindowData.
https://github.com/VsixCommunity/Community.VisualStudio.Toolkit/blob/9c32305c83827d0bfd7c85777f9e79fc71e22c2b/src/toolkit/Community.VisualStudio.Toolkit.Shared/ToolkitPackage.cs#L55
Ok, I think I found the culprit. Solution structure is as follows:
MyPackage.VSIX.2019 - Loaded, refernces 2019 toolkit (v16) MyPackage.VSIX.2022 - Loaded and Set as Startup Project, references 2022 toolkit (v17) MyPackage.VSIX.Shared - Loaded and toolwindow code resides in this project
When I unload MyPackage.VSIX.2019 the error no longer occurs or hasn't occurred in a while.