How to: Respond to Web References Events (Visual Basic)

The environment events defined in VSLangProj and VSLangProj80 are specific to Visual C# and Visual Basic projects. VSLangProjWebReferencesEvents is a new type in Visual Studio. It is raised when a Web reference is added to or removed from a Visual Basic or Visual C# project, or when the properties of a Web reference are modified in a Visual Basic or Visual C# project.

This example uses Visual Basic to connect event-handling methods for a specific project by using the VSLangProjWebReferencesEvents property.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

  1. Create a Visual Studio add-in project in Visual Basic.

  2. On the Project menu, click Add Reference, click the .NET tab, select the first VSLangProj and VSLangProj80 and click OK.

  3. Add Imports VSLangProj and Imports VSLangProj80 to the top of the Connect.vb file.

  4. In the Connect class, initialize a variable to handle the VSLangProjWebReferencesEvents object.

    Public Class Connect
        Implements IDTExtensibility2
        Dim _applicationObject As DTE2
        Dim _addInInstance As AddIn
        Public WithEvents webRefEvents As _ 
        VSLangProj80.VSLangProjWebReferencesEvents
    

    In this example, the variable is named webRefEvents.

    Other objects in the automation model relate to other types of events that are specific to the project. For example, ImportsEvents is raised when an import is added to or removed from an Imports collection. BuildManagerEvents applies to events related to temporary assemblies built from outputs of custom tools. For more information on BuildManager object, see Introduction to the BuildManager Object. For a complete list of the events that are specific to types of projects, see Event Objects (Specific to Types of Projects). For a list of general automation events, see Automation Event Objects.

  5. Add the following method call to the OnConnection method.

    Public Sub OnConnection(ByVal application As Object, _
     ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
     ByRef custom As Array) Implements IDTExtensibility2.OnConnection
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)
        WebReferenceEventsSample(_applicationObject)
    End Sub
    
  6. Add the WebReferenceEventsSample method declaration directly below the OnConnection method.

    Sub WebReferenceEventsSample(ByVal dte As DTE2)
    
    End Sub
    
  7. Add the following declarations to the top of the WebReferenceEventsSample method.

    Dim soln As Solution2 = CType(_applicationObject.Solution, Solution2)
    Dim proj As Project
    Dim vsproj As VSProject2
    Dim webServiceRef As String
    proj = soln.Projects.Item(1)
    

    The declaration proj = soln.Projects.Item(1) requires that a project is open in the Visual Studio integrated development environment (IDE) when you run this example. In particular, the project must be a Visual Basic or Visual C# project because the VSLangProjWebReferencesEvents object is specific to those project types.

  8. Cast the project to a VSProject2 object by adding the following code to the WebReferenceEventsSample method.

    ' Cast the project to a VSProject2.
    vsproj = CType(proj.Object, VSProject2)
    
  9. Also in the WebReferenceEventsSample method, retrieve the event objects from the automation model.

    webRefEvents = vsproj.Events2.VSLangProjWebReferencesEvents
    
  10. Connect the events handlers.

    AddHandler webRefEvents.OnAfterWebReferenceAdded, _
     AddressOf WebReferenceAdded
    AddHandler webRefEvents.OnBeforeWebReferenceRemoved, _
     AddressOf WebReferenceRemoved
    AddHandler webRefEvents.OnWebReferenceChanged, _
     AddressOf WebReferenceChanged
    
  11. Add a Web reference to the project.

    ' Create a Web references folder.
    MsgBox("Creating a Web references folder.", MsgBoxStyle.Information)
    vsproj.CreateWebReferencesFolder()
    ' Add a Web reference to the folder.
    ' Replace the "<web reference>" with an actual Web service URL.
    MsgBox("Adding a Web reference.", MsgBoxStyle.Information)
    webServiceRef = "<web reference>"
    vsproj.AddWebReference(webServiceRef)
    

    To programmatically add a Web service reference to the project, you must replace the placeholder text, <web reference>, in the code with the URL of an actual Web service.

    This step causes the OnAfterWebReferenceAdded event to fire when you run this add-in.

  12. Add procedures for each event related to the event object.

    Sub WebReferenceRemoved(ByVal removedRef As Object)
        MsgBox("A Web reference was removed.")
    End Sub
    Sub WebReferenceChanged(ByVal changedRef As Object)
        MsgBox("A Web reference was changed.")
    End Sub
    Sub WebReferenceAdded(ByVal addedRef As Object)
        MsgBox("A Web reference was added.")
    End Sub
    
  13. Finally, to prevent Visual Studio from slowing your system by continuing to monitor window-related events after you close the add-in, disable event handling. In Visual Basic, this is done by setting the event handler to Nothing.

    Public Sub OnDisconnection(ByVal RemoveMode As_
     Extensibility.ext_DisconnectMode, ByRef custom As System.Array)_
     Implements Extensibility.IDTExtensibility2.OnDisconnection
        webRefEvents = Nothing
    End Sub
    

    The complete code is listed in the Example section of this topic.

  14. To build the add-in, click Build Solution on the Build menu.

  15. Open a Visual C# or Visual Basic project in the Visual Studio integrated development environment (IDE).

  16. On the Tools menu, click Add-in Manager, and select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

To test the VSLangProjWebReferencesEvents event handling

  1. The OnAfterWebReferenceAdded event fires once when you run the add-in, because the example adds a reference to a Web service programmatically.

  2. To test the OnWebReferenceChanged event:

    • Expand the Web References folder in Solution Explorer.

    • Select the Web service and right-click it.

    • Select Properties from the drop-down menu.

    • In the Properties window, change the URL behavior from dynamic to static by selecting Static from the URL Behavior combo box.

  3. To test the OnBeforeWebReferenceRemoved event:

    • Select the Web service and right-click it.

    • Select Delete from the drop-down menu.

Example

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80

Public Class Connect
    Implements IDTExtensibility2
    Dim _applicationObject As DTE2
    Dim _addInInstance As AddIn
    Public WithEvents webRefEvents As _
    VSLangProj80.VSLangProjWebReferencesEvents
    Public Sub New()
    End Sub
    Public Sub OnConnection(ByVal application As Object, _
     ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object _
    , ByRef custom As Array) Implements IDTExtensibility2.OnConnection
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)
        WebReferenceEventsSample(_applicationObject)
    End Sub
    Sub WebReferenceEventsSample(ByVal dte As DTE2)
        Try
            Dim soln As Solution2 = CType(_applicationObject.Solution _
            , Solution2)
            Dim proj As Project
            Dim vsproj As VSProject2
            Dim webServiceRef As String
            proj = soln.Projects.Item(1)
            ' Cast the project to a VSProject2.
            vsproj = CType(proj.Object, VSProject2)
            webRefEvents = vsproj.Events2.VSLangProjWebReferencesEvents
            AddHandler webRefEvents.OnAfterWebReferenceAdded, _
            AddressOf WebReferenceAdded
            AddHandler webRefEvents.OnBeforeWebReferenceRemoved, _
            AddressOf WebReferenceRemoved
            AddHandler webRefEvents.OnWebReferenceChanged, _
            AddressOf WebReferenceChanged
            ' Create a Web references folder.
            MsgBox("Creating a Web references folder." _
            , MsgBoxStyle.Information)
            vsproj.CreateWebReferencesFolder()
            ' Add a Web reference to the folder.
            ' Replace the place holder <web reference> with
            ' a Web service URL.
            MsgBox("Adding a Web reference.", MsgBoxStyle.Information)
            webServiceRef = "<web reference>"
            vsproj.AddWebReference(webServiceRef)
        Catch ex As System.Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Sub WebReferenceRemoved(ByVal removedRef As Object)
        MsgBox("A Web reference was removed.")
    End Sub
    Sub WebReferenceChanged(ByVal changedRef As Object)
        MsgBox("A Web reference was changed.")
    End Sub
    Sub WebReferenceAdded(ByVal addedRef As Object)
        MsgBox("A Web reference was added.")
    End Sub
    Public Sub OnDisconnection(ByVal disconnectMode As _
    ext_DisconnectMode, ByRef custom As Array) Implements _
    IDTExtensibility2.OnDisconnection
        ' Turns off Web reference event handling when the 
        ' add-in shuts down.
        webRefEvents = Nothing
    End Sub
    Public Sub OnAddInsUpdate(ByRef custom As Array) _
    Implements IDTExtensibility2.OnAddInsUpdate
    End Sub
    Public Sub OnStartupComplete(ByRef custom As Array) _
    Implements IDTExtensibility2.OnStartupComplete
    End Sub
    Public Sub OnBeginShutdown(ByRef custom As Array) _
    Implements IDTExtensibility2.OnBeginShutdown
    End Sub
End Class

Compiling the Code

To compile this code, create a new Visual Studio add-in project in Visual Basic and replace the code of the Connect class with the code in the example. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.

See Also

Tasks

How to: Respond to Web References Events (Visual C#)

Other Resources

Responding to Automation Events

Responding to Events (Visual Basic and Visual C# Projects)