Extend Your Apps with External Add-ins: VB.NET: Create a Helper Class: Listing 1

A helper class can simplify and isolate hosting add-ins. Making this class generic allows a single helper class for use with all add-ins. This version improves performance by ensuring the store is built exaclty once during the application run and caching the loaded add-ins. The Shutdown method unloads an add-in by unloading its containing AppDomain.

Imports System.AddIn.Hosting

Public Class AddInSupport(Of T)
   Private Shared mHaveCheckedStore _
      As Boolean
   Private Shared mLoadedAddIns _
      As New Dictionary(Of String, T)

   Public Shared Function GetAddInList( _
      ByVal addInRoot As String) _
      As IEnumerable(Of AddInToken)
      UpdateStore(addInRoot)
      Return AddInStore.FindAddIns( _
         GetType(T), addInRoot)
   End Function

   Public Shared Function GetAddIn( _
      ByVal addInRoot As String, _
      ByVal addInName As String) _
      As T
      Dim addIn As T = Nothing
      If mLoadedAddIns.TryGetValue( _
         addInName, addIn) Then
         Return addIn
      End If
      Dim addIns = GetAddInList(addInRoot)
      If addIns.Count = 0 Then
         ' Log for user 
         Return Nothing
      End If
      Dim selectedAddToken _
         As AddInToken = Nothing
      If String.IsNullOrEmpty( _
         addInName) Then
         selectedAddToken = addIns(0)
      Else
      selectedAddToken = ( _
         From x In addIns _
         Where x.Name.Equals( _
         addInName, _
         StringComparison. _
         OrdinalIgnoreCase) _
         Select x).FirstOrDefault
      End If
      If selectedAddToken Is Nothing Then
         Return Nothing
      End If
      Try
         addIn = _
            selectedAddToken. _
            Activate( Of T)( _
            AddInSecurityLevel. _
            Internet)
      mLoadedAddIns.Add( _
         addInName, addIn)
         Return addIn
      Catch ex As Exception
         Console.WriteLine(ex)
      End Try
      Return Nothing
   End Function

   Private Shared Sub UpdateStore( _
      ByVal addInRoot As String)
      If Not mHaveCheckedStore Then
      Try
         If Not IO.Directory.Exists( _
            addInRoot) Then
         Throw New _
            ArgumentException( _
            "Directory not available")
         End If
         Dim warnings() As String = _
            AddInStore.Update( _
            addInRoot)
         If (warnings.Length > 0) Then
            Console.WriteLine( _
               "WARNINGS *****")
            Dim warning As String
            For Each warning _
               In warnings
            Console.WriteLine( _
               warning)
         Next
            Console.WriteLine( _
               "END WARNINGS *****")
         End If
      Catch ex As Exception
         Console.WriteLine(ex)
         Throw
      End Try
         mHaveCheckedStore _
            = True
      End If
   End Sub

   Public Shared Sub ShutDownAddIn( _
      ByVal addIn As T)
      Try
         For Each pair In _
            mLoadedAddIns
         If pair.Value.Equals( _
            addIn) Then
            mLoadedAddIns.Remove( _
               pair.Key)
         End If
      Next
         Dim controller = _
            AddInController. _
            GetAddInController( _
            addIn)
         controller.Shutdown()
      Catch ex As Exception
         
         Throw New _
            ArgumentException( _
            "Can't shutdown AddIn", _
            ex)
      End Try
   End Sub

End Class
comments powered by Disqus

Featured

  • Mastering AI Development and Building AI Apps with GitHub Copilot

    Two Microsoft experts explain how GitHub Copilot is evolving from a coding assistant into a broader platform for building, customizing and testing AI-powered developer workflows.

  • VS Code 1.123 Adds Agent Session Sync, 1M Context Windows

    Microsoft released Visual Studio Code 1.123 on June 3, adding agent-focused features, larger model context support, integrated browser updates and a new delay for some automatic extension updates.

  • Copilot Billing Shock Hits Developers

    Developer complaints about GitHub Copilot's new usage-based billing model have centered on unexpectedly rapid AI credit consumption, and neither GitHub nor Microsoft has responded directly to the backlash, though they have previously published guidance to lessen model usage costs.

  • Hands On with GitHub Copilot App Technical Preview: Turning a Blazor Issue into a PR

    GitHub's brand-new Copilot desktop app, in technical preview, handled a small Blazor issue from planning through pull request creation, but the hands-on test also showed why developers still need to verify agent work in the running app before merging.

Subscribe on YouTube