Host Item Events cause Host App to crash

Last post 05-27-2008, 11:23 AM by Melody. 19 replies.
Page 1 of 2 (20 items)   1 2 Next >
Sort Posts: Previous Next
  •  04-27-2008, 2:23 PM 1063

    Host Item Events cause Host App to crash

    I have an AddIn with a single HostItem which has one property and one custom Event (delcared using a delegate).

    I can load the addin, raise the event and then unload the addin with no prolems.  When I load the addin for the second time in the same session (not makeing any changes to the addin) and raise the event again I get a runtime error "The target application domain has been unloaded."  The full deatils are at the end of this post.

    I saw another post that seemed to be related but it talked about the addin developer unregistering an event, which isn't something I understand:

    http://www.summsoft.com/forums/permalink/276/275/ShowThread.aspx#275

    Is ths a knwon issue?  If so, is there a work-around that I can employ?

    Thx

     

    ===============================================================

     

    See the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.AppDomainUnloadedException: The target application domain has been unloaded.

    Server stack trace:
       at System.Threading.Thread.InternalCrossContextCallback(Context ctx, IntPtr ctxID, Int32 appDomainID, InternalCrossContextDelegate ftnToCall, Object[] args)
       at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)
       at System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(IMessage reqMsg)

    Exception rethrown at [0]:
       at Microsoft.VisualStudio.Tools.Applications.ExceptionFilterHelper.DynamicInvoke(IExceptionManager mgr, IExceptionNotificationObject exceptionNotificationObject, IRemoteDelegateContract delegateContract, IRemoteArgumentArrayContract remoteArgs, TypeInfrastructureManager typeInfrastructureManager)
       at Microsoft.VisualStudio.Tools.Applications.DelegateProxy.InvokeDelegate_VV_Return[R,A0,A1](A0 a0, A1 a1)
       at Microsoft.VisualStudio.Tools.Applications.DelegateProxy.InvokeDelegate_VV_NoReturn[A0,A1](A0 a0, A1 a1)
       at BLService.OnObjectInitEventHandler.Invoke(Object sender, ObjectEventArgs e)
       at BLService.BusinessObject.DoWork() in C:\Data\SimplePCR\BLService\BusinessObject.vb:line 33
       at UI.Form1.btnRaiseEvent_Click(Object sender, EventArgs e) in C:\Data\SimplePCR\UI\Form1.vb:line 37
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    UI
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Data/SimplePCR/UI/bin/Debug/UI.exe
    ----------------------------------------
    Microsoft.VisualBasic
        Assembly Version: 8.0.0.0
        Win32 Version: 8.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
    ----------------------------------------
    System
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.Runtime.Remoting
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
    ----------------------------------------
    BLService
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Data/SimplePCR/UI/bin/Debug/BLService.DLL
    ----------------------------------------
    VSTAIntegration
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/VSTAIntegration/1.0.0.0__41bdf6df642826a5/VSTAIntegration.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0
        Assembly Version: 9.0.0.0
        Win32 Version: 9.0.21022.50
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll
    ----------------------------------------
    System.AddIn
        Assembly Version: 3.5.0.0
        Win32 Version: 3.5.21022.8 built by: RTM
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.AddIn/3.5.0.0__b77a5c561934e089/System.AddIn.dll
    ----------------------------------------
    BLServiceDesign
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Data/SimplePCR/UI/bin/Debug/BLServiceDesign.DLL
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.DesignTime.v9.0
        Assembly Version: 9.0.0.0
        Win32 Version: 9.0.21022.50
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.DesignTime.v9.0/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.DesignTime.v9.0.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.Hosting.v9.0
        Assembly Version: 9.0.0.0
        Win32 Version: 9.0.21022.50
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Hosting.v9.0/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Hosting.v9.0.dll
    ----------------------------------------
    System.Core
        Assembly Version: 3.5.0.0
        Win32 Version: 3.5.21022.8 built by: RTM
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Core/3.5.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System.AddIn.Contract
        Assembly Version: 2.0.0.0
        Win32 Version: 3.5.21022.8 built by: RTM
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.AddIn.Contract/2.0.0.0__b03f5f7f11d50a3a/System.AddIn.Contract.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.HostAdapter.v9.0
        Assembly Version: 9.0.0.0
        Win32 Version: 9.0.21022.50
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.HostAdapter.v9.0/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.HostAdapter.v9.0.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.Contract.v9.0
        Assembly Version: 9.0.0.0
        Win32 Version: 9.0.21022.50
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Contract.v9.0/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Contract.v9.0.dll
    ----------------------------------------
    Anonymously Hosted DynamicMethods Assembly
        Assembly Version: 0.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_32/mscorlib/2.0.0.0__b77a5c561934e089/mscorlib.dll
    ----------------------------------------
    Microsoft.VisualStudio.Tools.Applications.Adapter.v9.0
        Assembly Version: 9.0.0.0
        Win32 Version: 9.0.21022.50
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.Tools.Applications.Adapter.v9.0/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Tools.Applications.Adapter.v9.0.dll
    ----------------------------------------
    System.Data.SqlXml
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Data.SqlXml/2.0.0.0__b77a5c561934e089/System.Data.SqlXml.dll
    ----------------------------------------
    System.Web
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
        CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Web/2.0.0.0__b03f5f7f11d50a3a/System.Web.dll
    ----------------------------------------

    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.

    For example:

    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>

    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.


     

  •  04-28-2008, 11:03 AM 1067 in reply to 1063

    Re: Host Item Events cause Host App to crash

    The link you referenced is for VSTA 1.0.

    VSTA 2.0 proxy implementation is different.

    Is the event being raised by the host application?  Do your symptoms change if you do not hook up the event? 

    What if you unhook the event explicitly before unloading the add-in? -- Does this make any difference when reloading the add-in?

     

  •  04-28-2008, 5:59 PM 1068 in reply to 1067

    Re: Host Item Events cause Host App to crash

    I call a method in my Host Object, which raises the event from inside itself.

    I'm confused about hooking up the event, since I don't do anything to hook it up or unhook it.  The Event is declaired as part of the Host Object class so that it is available in the AddIn Project when the IDE opens it up.

    Thx.

  •  04-29-2008, 12:22 PM 1071 in reply to 1068

    Re: Host Item Events cause Host App to crash

    Terry,
    I have seen the "The target application domain has been unloaded." error when an add-in hooks up to an event in the host, then the add-in is unloaded, then the event fires in the host.

    This happens when add-ins are unloaded like:

    internal void Disconnect()
    {
       foreach
    (IEntryPoint ep in this.addInList)

       {
          try

          {
             ep.OnShutdown();
          }
          catch (Exception ex)
          {
             System.Diagnostics.Debug.WriteLine("Exception unloading addIn");
             System.Diagnostics.Debug.WriteLine(ex.ToString());
          }
             AddInController controller = AddInController.GetAddInController(ep);
             controller.Shutdown();
       }
       this.addInList.Clear();
    }


    When add-ins are unloaded like this, no error is raised (that I've seen so far); however, the unloaded add-in catches the event and executes what ever code the add-in hooked into the event.

    internal void Disconnect()
    {
       foreach (IEntryPoint ep in this.addInList) 
       {
          ep.OnShutdown();
       }
       this.addInList.Clear();
    }


    We have reported this to Microsoft as a bug and will pass on any feedback we get back.


    In the mean time there are two ways to get around this. 
    One is to ensure that all events hooked into by an add-in are removed in the OnShutdown method. 
    The second is to use an EventHelper class which unhooks any events that cause errors.  Here’s an example of an EventHelper class- it’s based off the EventHelper class included with the VSTA v 1 SDK samples. 

    *Notice with Visual Basic you append the event with Event when passing it to the EventHelper.

    Public Event CreatedDrawing As CreatedDrawingEventHandler

    Public Function NewDrawing() As Drawing

       Dim newDrawing_value As Drawing = New Drawing(Me)

        EventHelper.Invoke(CreatedDrawingEvent, Me, New CreatedDrawingEventArgs(newDrawing_value))

    End Function


    Imports System

    Imports System.Collections.Generic

    Imports System.Text

     

    Friend Class EventHelper

       'Invoke all delegates.  If a delegate is a proxy for an add-in,

       'catch any exceptions and remove the delegate from the invocation list.

       Public Shared Sub Invoke(ByVal eventDelegate As MulticastDelegate, ByVal ParamArray args() As Object)

     

          If Not eventDelegate Is Nothing Then

             'get the list of delegates

             Dim list As System.Delegate() = eventDelegate.GetInvocationList()

     

             'create an empty list of delegates to store delegates to be removed

             Dim blockList As List(Of [Delegate]) = New List(Of [Delegate])(list.Length)

     

             'FOR each delegate in the list

             For Each handler As [Delegate] In list

     

                Try

     

                   'attempt to invoke it

                   CType(handler, MulticastDelegate).DynamicInvoke(args)

     

                Catch ex As Exception

     

                   'IF the invoke failed, add the delegate to the list of delegates to be removed

                   blockList.Add(handler)

     

                End Try

     

             Next   'delegate

     

             'FOR each delegate to be removed

             For Each handler As [Delegate] In blockList

     

                'remove the handler

                System.Delegate.Remove(eventDelegate, handler)

     

             Next   'delegate to be removed

     

          End If  'the eventDelegate has a value

       End Sub

    End Class


    I hope this helps, please let me know if you have any questions.
    Thanks!
    -Melody
  •  05-02-2008, 9:25 AM 1074 in reply to 1071

    Re: Host Item Events cause Host App to crash

    Let me confirm that I'm understanding this correctly:

     

    1) I should declair the event directly in the class (using a delegate as the type) like I would normally.

    2) When it comes time to raise an event, I should use this helper-type code.

    3) The Helper code uses the delegate to get a list of invocation targets.

    4) One of these invocation targets may be an old reference to an app domain that no longer exists and the try-catch will cath it.

    5) When an error is caught, we remove the invalid reference from the invocation list.

     

    Thanks.

     

  •  05-02-2008, 9:53 AM 1075 in reply to 1074

    Re: Host Item Events cause Host App to crash

    Terry,
    You got it. 

    FYI-
    This is the VSTA v 1 way, if we hear a new way for VSTA v 2 we will let you know.

    Let me know if there's anything else I can help with! 
    Thanks,
    -Melody
  •  05-02-2008, 11:39 AM 1076 in reply to 1075

    Re: Host Item Events cause Host App to crash

     

    Well, I implemented the functionality and it's generating an error when I call the DynamicInvoke().  I have two arguments for my event.  In typical fashion the first is the sender (sent as Me from the HostItem) and the second is an args class that I declaired myself, using the shape app as an example.

     

    When the DynamicInvoke runs it gives this:

    InnerException = {"Unable to cast transparent proxy to type 'PGT.Win.Component.ElectricalControls.Machine.FileEventArgs'."}

    Here's my FileEventArgs class, which is in my proxy.dll and ItemTypeProvider:

    Public Class FieldEventArgs

    Private mstrName As String

    Public Sub New(ByVal FieldName As String)

    Me.mstrName = FieldName

    End Sub

    Public ReadOnly Property FieldName() As String

    Get

    Return Me.mstrName

    End Get

    End Property

    End Class

    I tried inheriting from EventArgs and MarshalByRefObject and all three throw the same error.  Of course, if I just use the RaiseEvent() instead of this DynamicInvoke it works fine... but then on the second try I get the app domain error so I need to do this DynamicInvoke thing.

    Any Ideas?

  •  05-02-2008, 1:04 PM 1077 in reply to 1076

    Re: Host Item Events cause Host App to crash

    Terry,

    >it's generating an error when I call the DynamicInvoke()
    When is this error being generated- when the host (w/o add-ins) raises the event, when the host raises the event with an add-in loaded, or when the host raises the event with the add-in unloaded?  Does it work in any of the above scenarios?

    >I have two arguments for my event.  In typical fashion the first is the sender (sent as Me from the HostItem) and the second is an args class that I declaired myself, using the shape app as an example.
    >Here's my FileEventArgs class, which is in my proxy.dll and ItemTypeProvider:
    Raising the event sending me and the event args, both of which are defined in the HostTypeMapProvider, is fine. 

    >I tried inheriting from EventArgs and MarshalByRefObject and all three throw the same error.
    The EventArgs should inherit from System.EventArgs

    Thanks,
       -Melody
  •  05-02-2008, 1:17 PM 1078 in reply to 1077

    Re: Host Item Events cause Host App to crash

    I have an instance of my Host Object, which is the primary Host Item in the AddIn.  It has an public method that is called to "Do work" and inside it I have this:

    Me.RaiseAddInEvent(Me.FileInitializedEvent, Me, New FileEventArgs(Me.Name))

    This "RaiseAddInEvent" is a version of what you posted earlier.  My code from it is here, and the line in yellow throws the exception:

    'Get the current Invocation List from the Delegate we are dealing with.

    Dim delInvocationList As System.Delegate() = EventDelegate.GetInvocationList()

    'Create an empty invocation list to store invalid references

    Dim delInvalidInvocationTargets As List(Of [Delegate]) = New List(Of [Delegate])(delInvocationList.Length)

    'Loop through all items in the Invocation List

    For Each delInvocationTarget As [Delegate] In delInvocationList

    'Try to invoke this reference

    Try

    'Invoke the reference

    CType(delInvocationTarget, MulticastDelegate).DynamicInvoke(args)

    Catch ex As Exception

    'The Invocation failed, so put this in our invalid list.

    delInvalidInvocationTargets.Add(delInvocationTarget)

    End Try

    Next

    'Loop through any invalid references and remove them from this delegate's invocation list

    For Each delInvalidTarget As [Delegate] In delInvalidInvocationTargets

    'Remove this invocation target

    System.Delegate.Remove(EventDelegate, delInvalidTarget)

    Next

    End If

    At the time the exception is thrown, I have loaded the Addin (which has successfully hit the Startup Event).  I have tried this with the event argument object inheriting from System.Event, but with the same results.

     

  •  05-02-2008, 1:21 PM 1079 in reply to 1077

    Re: Host Item Events cause Host App to crash

    Also, I tried running the code without loading the AddIn, but then it never gets to the DynamicInvoke() method becuase it's in a for each loop that's looping through the Invocation list.  Since the AddIn hasn't loaded, no events have been registered because I don't have an event handler in my Host Item Class for this event.  The event exists purely for the AddIn to catch and handle.

    Thx.

     

  •  05-05-2008, 3:23 PM 1084 in reply to 1079

    Re: Host Item Events cause Host App to crash

    The EventHelper class I posted is intended to be used in the host, not the add-ins.  
    I will look into raising events directly from add-ins- however, you may want to consider implementing this in the host instead of adding the class to each add-in.

    >"Unable to cast transparent proxy to type 'PGT.Win.Component.ElectricalControls.Machine.FileEventArgs'."
    The type ...FileEventArgs exists in the host, and the instances of this class passed to the proxy are actually transparent proxies.  When the transparent proxies are passed back to the host the "real" host side version of the object is used.

    >I'm confused about hooking up the event, since I don't do anything to hook it up or unhook it.  The Event is declared as part of the Host Object class so that it is available in the AddIn Project when the IDE opens it up.
    I'm not sure I understand what you mean by this. 
    There are three ways to hook up events:
    1)  Using the handles keyword:
    Private Sub AppAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    2)  Using the AddHandler

    AddHandler Me.ShapeAdded, New AddedEventHandler(AddressOf AddIn_ShapeAdded)


    3)  Using C#
    this
    .ActiveDrawing.ShapeAdded += new AddedEventHandler(ActiveDrawing_ShapeAdded);

    How is your add-in hooking into an event?


    I assume you are unloading the add-in like this- are you?

    internal void Disconnect()
    {
       foreach
    (IEntryPoint ep in this.addInList)

       {
          try

          {
             ep.OnShutdown();
          }
          catch (Exception ex)
          {
             System.Diagnostics.Debug.WriteLine("Exception unloading addIn");
             System.Diagnostics.Debug.WriteLine(ex.ToString());
          }
             AddInController controller = AddInController.GetAddInController(ep);
             controller.Shutdown();
       }
       this.addInList.Clear();
    }


    >>I tried inheriting from EventArgs and MarshalByRefObject and all three throw the same error.
    EventArg classes should inherit from the System.EventArgs class; however, (fyi) this is a special case with VSTA and that inhreitence is not carried through into the proxy.

    >
    I have tried this with the event argument object inheriting from System.Event, but with the same results.
    Could you show an example of your event? 
    For example- all of this code is from the host:
    'An event handler type for when an item is added to a collection.

    Public
    Delegate Sub AddedEventHandler(ByVal sender As Object, ByVal e As AddedEventArgs)

    Partial Public Class Drawing

        'Event that is fired when a shape is added to a drawing.
       
    Public Event ShapeAdded As AddedEventHandler

     
        Private Sub shapes_ShapeAdded(ByVal sender As Object, ByVal e As AddedEventArgs)

            'raise  the event
           
    EventHelper.Invoke(ShapeAddedEvent, Me, New AddedEventArgs(thisShape))

        End Sub

    End Class

    I'll look into raising events directly from add-ins and let you know what I find. 

    Thanks,
    -Melody

  •  05-05-2008, 5:04 PM 1085 in reply to 1084

    Re: Host Item Events cause Host App to crash

    Terry,
    Wasn't sure if you knew, but when you use the drop downs in the IDE to hook up events with handles, you can unhook them with the RemoveHandler:

    RemoveHandler Me.NewDocumentCreated, AddressOf AppAddIn_NewDocumentCreated


  •  05-13-2008, 12:14 PM 1101 in reply to 1063

    Re: Host Item Events cause Host App to crash

    Update:
    The error "The target application domain has been unloaded." seen when events raised attempt to access code from unloaded add-ins was reported as a bug. 

    The feedback from Microsoft is to explicitly unhook all events in add-ins prior to unloading.

    As I fail safe, I recommend
    1)  Unloading shuttingdown the controller for add-ins when they are unloaded (see code below)
    2)  Using an Event Helper so that add-ins which do not properly unhook events, do not cause the error.

    internal void Disconnect()
    {
       foreach
    (IEntryPoint ep in this.addInList)

       {
          try

          {
             ep.OnShutdown();
          }
          catch (Exception ex)
          {
             System.Diagnostics.Debug.WriteLine("Exception unloading addIn");
             System.Diagnostics.Debug.WriteLine(ex.ToString());
          }
             AddInController controller = AddInController.GetAddInController(ep);
             controller.Shutdown();
       }
       this.addInList.Clear();
    }


  •  05-22-2008, 10:16 AM 1121 in reply to 1101

    Re: Host Item Events cause Host App to crash

    Sorry for the long delay, but I got distracted with the NON-VSTA portion of our system design for a couple weeks.  Now I'm back to getting the VSTA stuff ironed out.

    I'm not sure how to best get back into this discussion so I'll re-state my issue and try to clerify a couple points.

    I've implemented the event helper code as recommended.  This appears to be doing it's job in handling the "App domain has been unloaded" issue.  The problem I'm having is that the DynamicInvoke is throwing an exception when I try to raise/call my event.  I'm not trying to raise an event from the AddIn (as suspected above).  This code is all in my HostObject.  Here's my event Delegate declaration:

    Public Delegate Sub OnFileInitEventHandler(ByVal sender As Object, ByVal e As FileEventArgs)

    I  raise the event from my HostObject with this line of code:

    Me.RaiseAddInEvent(FileInitializedEvent, Me, New FileEventArgs(Me.Name))

    This RaiseAddInEvent() method is my EventHelper, which has all the code from the example including this call to DynamicInvoke:

    CType(delInvocationTarget, MulticastDelegate).DynamicInvoke(args)

    Unfortunately, it has some problem with the second parameter in my event and throws this exception:

    InnerException = {"Unable to cast transparent proxy to type 'PGT.Win.Component.ElectricalControls.Machine.FileEventArgs'."}

    This FileEventArgs is a Class that inherits from System.EventArgs (as recommended).

    Now, I know the event and it's parameters "work" correctly because if I skip all this EventHelper/DynamicInvoke stuff and just do this from my HostObject, it works fine:

    RaiseEvent FileInitialized(Me, New FileEventArgs(Me.Name))

    Of course, when i do it this way, the second time around I get the App Domain Unloaded issue which means I have to use the EventHelper/DynamicInvoke.  So, in summary:  Rock|Me|HardPlace

    Bring it!

     

     

  •  05-22-2008, 10:41 AM 1122 in reply to 1121

    Re: Host Item Events cause Host App to crash

    Hi Terry- too funny!

    It sounds like the issue is not with the event helper, but where it's getting the event args. 

    >"Unable to cast transparent proxy to type 'PGT.Win.Component.ElectricalControls.Machine.FileEventArgs'."}
    The transparent proxy is a type in the add-in, it should not be on the host side.  When this type is passed from the add-in to the host the HostTypeMapProvider should translate it to a host side type.  Where are the event args being created?

    :)
    -Melody
Page 1 of 2 (20 items)   1 2 Next >
View as RSS news feed in XML