I used the "Add-In for Visual Studio" wizard to create a new Addin project and now, I'm trying to add some event handlers:
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
_applicationObject.Events.BuildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
_applicationObject.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone;
_applicationObject.Events.SelectionEvents.OnChange += SelectionEvents_OnChange;
_applicationObject.Events.DocumentEvents.DocumentOpened += DocumentEvents_DocumentOpened;
_applicationObject.Events.DocumentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
}
But whatever I do, my handlers are never executed!
Am I blind? Do I have to do anything else to register these handlers or why doesn't it work?
Seems you're a victim of the Garbage Collector. See: http://www.mztools.com/articles/2005/mz2005012.aspx
private readonly BuildEvents _buildEvents;
private readonly SelectionEvents _selectionEvents;
private readonly DocumentEvents _documentEvents;
private readonly Events _events;
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
_events = _applicationObject.Events;
_buildEvents = _events.BuildEvents;
_buildEvents.OnBuildBegin += BuildEvents_OnBuildBegin;
_buildEvents.OnBuildDone += BuildEvents_OnBuildDone;
_selectionEvents = _events.SelectionEvents;
_selectionEvents.OnChange += SelectionEvents_OnChange;
_documentEvents = _events.DocumentEvents;
_documentEvents.DocumentOpened += DocumentEvents_DocumentOpened;
_documentEvents.DocumentSaved += DocumentEvents_DocumentSaved;
}
If you look at applicationObject in the debugger you'll see its a COM object, but the xxxEvents classes are not
(If you can't get the code to break on OnConnection, then possibly your addin isn't getting loaded when you debug, check the tools menu)
Events in COM are handled by a separate COM interface (several in this case) in the other direction which the server (VS) calls to fire them.
Although COM objects have a similar typed assembly concept to CLR assemblies they are unmanaged code internally, so cannot hold roots to managed objects.
So although you can hook a delegate to a COM event in a way that looks exactly like a native CLR event, your event is hooked to an RCW (runtime callable wrapper).
There is a COM reference from the server to the RCW com interface, but without a CLR root the RCW eventually gets disposed which unloads the com interface, after which you won't sink any events.
I'm not sure but I think this normally works when you consume a single COM object with a direct association to its event interfaces so its maybe due to how the DTE COM interface is structured...
Anyway as others have said you just need any kind of managed reference to the BuildEvents, SelectionEvents and DocumentEvents classes from the app object to fix it. Multiple instances of VS load separate instances of the add in so you could just add a list of static object refs and set them in OnConnect.
Subscription to DTE events doesn't seem to work - Events don't get called
http://msdn.microsoft.com/en-us/library/k639e386.aspx