AddIn.Load throws an exception

Latest post 01-03-2008 10:12 AM by Melody. 10 replies.
  • 09-20-2007 10:41 AM

    AddIn.Load throws an exception

    I have a colletion class with the following definition --

    public class CollateralCollection : IEnumerable

    {   ...

          public IEnumerator GetEnumerator()

          {

             return (IEnumerator) collaterals.GetEnumerator();

             //return (IEnumerator)null;

          }

          private List<Collateral> collaterals;

    ...

    }

    The proxy code for the GetEnumerator method is --

    public global::System.Collections.IEnumerator GetEnumerator() {

    object[] _param = new object[0];

    if ((this.@__vstaCacheGetEnumerator4 == null)) {

    this.@__vstaCacheGetEnumerator4 = this.remoteType.GetMethod("GetEnumerator", (((global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.InvokeMethod)

    | global::System.Reflection.BindingFlags.Instance)

    | global::System.Reflection.BindingFlags.Default), null, new global::System.Type[0], null);

    }

    global::System.Collections.IEnumerator retVal_param;

    retVal_param = ((global::System.Collections.IEnumerator)(((global::System.Reflection.MethodInfo)(this.@__vstaCacheGetEnumerator4)).Invoke(this, _param)));  // ********throws an exception

    return retVal_param;

    }

    When I tried to call AddIn.Load upon program startup, it throws an exception (see attached for exception details) at the statement right before the return in proxy code.  If I change the GetEnumerator code to { return (IEnumerator)null; }, it won't the exception in that code.  Of course, some other exceptions are thrown because a null was returned.  I am wondering what I did wrong.  Thanks a lot for the help.

    Exception details --

    Microsoft.VisualStudio.Tools.Applications.Contract.AddInException was unhandled
      Message="System.Collections.Generic.KeyNotFoundException, mscorlib\r\n\r\nSystem.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.\r\n\r\nServer stack trace: \r\n   at System.ThrowHelper.ThrowKeyNotFoundException()\r\n   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)\r\n   at Microsoft.VisualStudio.Tools.Applications.TypeInfrastructureManager.GetCanonicalName(TypeDescription localType)\r\n   at Microsoft.VisualStudio.Tools.Applications.TypeInfrastructureManager.GetCanonicalName(Type localType)\r\n   at Microsoft.VisualStudio.Tools.Applications.SerializableObjectContractFormatter.Serialize(Object serialize, Type typeToUse, TypeInfrastructureManager typeInfrastructureManager)\r\n   at Microsoft.VisualStudio.Tools.Applications.TypeServices.CreateSerializableRemoteArgument(Object objToPack, Type typeToUse, Boolean isByRef, TypeInfrastructureManager typeInfrastructureManager)\r\n   at Microsoft.VisualStudio.Tools.Applications.TypeServices.RemoteArgumentFromObject(Object objToPack, Type typeToReflect, Boolean isByRef, Boolean isOut, TypeInfrastructureManager typeInfrastructureManager)\r\n   at Microsoft.VisualStudio.Tools.Applications.Internal.AdapterHelpers.PostProcessArguments(IRemoteArgumentArrayContract arguments, Object[] outputArguments, ParameterInfo[] parameters, Object returnValue, Type returnType, TypeInfrastructureManager typeInfrastructureManager, ContractLockData lockData)\r\n   at Microsoft.VisualStudio.Tools.Applications.RemoteMethodInfoAdapter.System.AddIn.Contract.Automation.IRemoteMethodInfoContract.Invoke(IRemoteObjectContract target, BindingFlags bindingFlags, IRemoteArgumentArrayContract arguments, Int32 lcid)\r\n   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)\r\n   at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)\r\n   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)\r\n\r\nException rethrown at [0]: \r\n   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n   at System.AddIn.Contract.Automation.IRemoteMethodInfoContract.Invoke(IRemoteObjectContract target, BindingFlags bindingFlags, IRemoteArgumentArrayContract parameters, Int32 localeId)\r\n   at Microsoft.VisualStudio.Tools.Applications.ExceptionFilterHelper.Invoke(IExceptionManagerContract mgr, IExceptionNotificationObjectContract exceptionNotificationObjectContract, IRemoteMethodInfoContract miContract, IRemoteObjectContract remoteObjectContract, BindingFlags bindingAttr, IRemoteArgumentArrayContract parameters, Int32 lcid)\r\n   at Microsoft.VisualStudio.Tools.Applications.RemoteMethodInfo.Invoke(Object target, BindingFlags invokeAttr, Binder binder, Object[] arguments, CultureInfo culture)\r\n   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)\r\n   at ASAP2.CollateralCollection.GetEnumerator()\r\n   at ASAP2CSharpAppAddIn.script.script_Startup(Object sender, EventArgs e)\r\n   at ASAP2CSharpAppAddIn.script.FinishInitialization()\r\n   at ASAP2.Application.Microsoft.VisualStudio.Tools.Applications.Contract.IEntryPointContract.FinishInitialization()\r\n   at Microsoft.VisualStudio.Tools.Applications.EntryPointAdapter.Microsoft.VisualStudio.Tools.Applications.Contract.IEntryPointContract.FinishInitialization()"
      Source="Microsoft.VisualStudio.Tools.Applications.Adapter"
      StackTrace:
        Server stack trace:
           at Microsoft.VisualStudio.Tools.Applications.EntryPointAdapter.Microsoft.VisualStudio.Tools.Applications.Contract.IEntryPointContract.FinishInitialization()
           at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
           at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
           at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)
        Exception rethrown at [0]:
           at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
           at Microsoft.VisualStudio.Tools.Applications.Contract.IEntryPointContract.FinishInitialization()
           at Microsoft.VisualStudio.Tools.Applications.Internal.ExceptionFilter.ExceptionFilterHelper.CallFinishInitialization(IEntryPointContract hi, IExceptionFilterCallback callback)
           at Microsoft.VisualStudio.Tools.Applications.AddInImpl.ExecutePhase(String methodName, IEntryPointContract targetEntryPoint)
           at Microsoft.VisualStudio.Tools.Applications.AddInImpl.AttemptInternalLoad(IAppDomainBindingContract appDomainBinding, String appBase, String configFile, PermissionSet topOfStackPermission, StrongName[] strongNameAssemblies)
           at Microsoft.VisualStudio.Tools.Applications.AddInImpl.InternalLoad(IAppDomainBindingContract appDomainBinding, String appBase, String configFile, PermissionSet topOfStackPermission, StrongName[] strongNameAssemblies)
           at Microsoft.VisualStudio.Tools.Applications.AddInImpl.Load(String appBasePath)
           at Microsoft.VisualStudio.Tools.Applications.AddIn.Load(String appBasePath)
           at ASAP2.Extension.LoadAddIns() in C:\VSTA\ASAP2\ASAP2\Extension.cs:line 47
           at ASAP2.Extension.Connect(Application application) in C:\VSTA\ASAP2\ASAP2\Extension.cs:line 22
           at ASAP2.Program.Main() in C:\VSTA\ASAP2\ASAP2\Program.cs:line 23
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

     

  • 09-20-2007 1:21 PM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: AddIn.Load throws an exception

    Leighyu,

      The System.Collections.Generic.KeyNotFoundException indicates that there is a problem with the type map.  I believe you wrote previously that you were looking at Tom Quinn's Generic Sample, the blog that accompanies the sample addresses this:  "The problem turned out to be that the type returned by GetEnumerator was IEnumerator<T> and not just IEnumerator. "  I would suggest checking out the blog and the sections of the samples that deal with this, including CollectionAdapter.cs, the InitializeTypInfrastructureManager and typeInfrastructureManager_AdapterResolve methods in the extension.cs file, and in the proxy project the ShapesCollection.cs file and the collections/typemap/tim_adapter section of the Proxy.cs file.  You may simply need to change/add to the type maps or proxy.  If you run into any problems, or have any questions about this please let me know.

    Thanks,

      -Melody

  • 09-20-2007 3:19 PM In reply to

    Re: AddIn.Load throws an exception

    Thanks for pointing me to the right direction.  Yes, the issue only occurs when I use foreach loop instead of for loop in my Add-in.  I will play around with the generics adapter classes suggested by Tom.
  • 09-20-2007 3:24 PM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: AddIn.Load throws an exception

    Glad to help.  Good luck!

    -Melody

  • 09-20-2007 4:48 PM In reply to

    Re: AddIn.Load throws an exception

    I spent some time playing with the basic ShapeApp, which has a shapeCollection with a data member List<IShape>, and a public method "public IEnumerator GetEnumerator()". 

    I changed the add-in so that it loops through a shape collection using foreach rather than for loop.  The basic ShapeApp then throws the same ThrowKeyNotFoundException.

    So to make it clear, to get "foreach" work for the basic ShapeApp, I need to modify the GetEnumerator to return IEnumerator<IShape> instead of IEnumerator, and other corresponding changes suggested by Tom?

     

  • 09-20-2007 5:14 PM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: AddIn.Load throws an exception

    Leighyu,

     I'd have to dig a little deeper to be sure; however, that sounds correct to me. 

    In the non-generic ShapeAppAdvanced sample, check out the document level add-in.  In the DocAddIn_Startup they use a "for" instead of a "foreach".  When I changed this to "foreach" (using the advanced version) it did not work. 

    In the generic ShapeAppAdvancedCSharpGenerics sample, the above change does work.  (remember to re-compile the host app then add-in when switching between the two samples)

    foreach (IShape thisShape in this.Shapes)

    thisShape.LocationChanged += new EventHandler<LocationChangedEventArgs>(ShapeApp_LocationChanged);

     

    Hope this helps!

    -Melody

  • 09-21-2007 9:35 AM In reply to

    Re: AddIn.Load throws an exception

    Melody --

    I m wondering if your company can include some sample code to make foreach work for the basic ShapeApp? 

    The reason I ask is that the generics sample adds a lot code to make the generics collections work for classes derived from the generics collection.  I am wondering if making the generics collection a data member will simplify the process ... to make "GetEnumerator" work? 

    Thanks

  • 09-21-2007 11:22 AM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: AddIn.Load throws an exception

    Leighyu,

        Sure- no problem.  It will take a little time though (maybe next week or week after). 

    -Melody

  • 01-03-2008 2:53 AM In reply to

    Re: AddIn.Load throws an exception

    Hi Melody,

       I'm also getting an error from the Addin.Load method. The error is something like "System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Common.EntityModel.AppIdentity...'"

       I'm sorry if i'm asking a lot of questions lately. I really need to integrate our application with VSTA as soon as possible. :(

    Thanks again,

    Ric 

  • 01-03-2008 3:12 AM In reply to

    Re: AddIn.Load throws an exception

    I feel so stupid right now... I fixed my problem. I just needed to add a dll to the addin...
  • 01-03-2008 10:12 AM In reply to

    • Melody
    • Top 10 Contributor
    • Joined on 04-26-2007
    • Syracuse, NY
    • Posts 245

    Re: AddIn.Load throws an exception

    Ric,

       I'm glad to hear you are making progress!  Don't feel bad about the reference question, we see that a lot.  Did you know you can add additional references to the project (add-in) templates with ProjectGen?  Also, if you are interested in getting your add-ins to work like the ShapeApp samples (where the add-in dll's are automatically placed in %MyDocuments%\Host\AppAddins) check out my blog on this

    Good Luck!

    -Melody

    Filed under: ,
Page 1 of 1 (11 items) | RSS
Copyright Summit Software Company, 2008. All rights reserved.