Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

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

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

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To handle events related to Web references by using Visual C#

  1. Create a Visual Studio add-in project in Visual C#.

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

  3. Add using VSLangProj; and using VSLangProj80; to the top of the Connect.cs file.

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

    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    private VSLangProj80.VSLangProjWebReferencesEvents webRefEvents;
    

    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 void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        // Call the WebReferenceEventsSample method.
        WebReferenceEventsSample (_applicationObject);
    }
    
  6. Add the WebReferenceEventsSample method declaration directly below the OnConnection method with the following declarations.

    public void WebReferenceEventsSample(DTE2 dte)
    {
        Solution2 soln = (Solution2)_applicationObject.Solution;
        Project proj;
        VSProject2 vsproj;
        String webServiceRef;
        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, Visual C#, or Visual J# project because the VSLangProjWebReferencesEvents object is specific to those project types.

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

    // Get a reference to the VSProject2 object.
    vsproj = (VSProject2)proj.Object;
    
  8. Retrieve the event objects from the automation model in the WebReferenceEventsSample method,.

    webRefEvents = vsproj.Events2.VSLangProjWebReferencesEvents;
    
  9. In the WebReferenceEventsSample method, connect the events handlers.

    webRefEvents.OnAfterWebReferenceAdded += new
     _dispVSLangProjWebReferencesEvents_OnAfterWebReferenceAddedEventHandler
    (webRefEvents_OnAfterWebReferenceAdded);
    webRefEvents.OnBeforeWebReferenceRemoved += new
    _dispVSLangProjWebReferencesEvents_OnBeforeWebReferenceRemovedEventHandler
    (webRefEvents_OnBeforeWebReferenceRemoved);
    webRefEvents.OnWebReferenceChanged += new
     _dispVSLangProjWebReferencesEvents_OnWebReferenceChangedEventHandler
    (webRefEvents_OnWebReferenceChanged);
    
  10. In the WebReferenceEventsSample method, add a reference to a Web service to the project.

    // Add a Web reference to the folder.
    // Replace the "<web reference>" with an actual Web service.
    MessageBox.Show("Adding a Web reference.", "Information");
    webServiceRef = "<web reference>";
    vsproj.AddWebReference(webServiceRef);
    

    To programmatically add a reference to a Web service 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.

  11. Add procedures for each event related to the event object below the WebReferenceEventsSample method.

    void webRefEvents_OnWebReferenceChanged(object pDispatch)
    {
        MessageBox.Show("A Web reference was changed.");
    }
    void webRefEvents_OnBeforeWebReferenceRemoved(object pDispatch)
    {
        MessageBox.Show("A Web reference was removed.");
    }
    void webRefEvents_OnAfterWebReferenceAdded(object pDispatch)
    {
        MessageBox.Show("A Web reference was added." + pDispatch.Name);
    }
    
  12. 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 the OnDisconnection method.

    public void OnDisconnection(ext_DisconnectMode disconnectMode,
     ref Array custom)
    {
        // If the delegate handlers have been connected, then 
        // disconnect them here. 
        // If you do not do this, the handlers may still 
        // fire because garbage collection has not removed them.
        if (webRefEvents != null)
        {
            webRefEvents.OnAfterWebReferenceAdded -= new
     _dispVSLangProjWebReferencesEvents
    _OnAfterWebReferenceAddedEventHandler
    (webRefEvents_OnAfterWebReferenceAdded);
            webRefEvents.OnBeforeWebReferenceRemoved -= new
     _dispVSLangProjWebReferencesEvents
    _OnBeforeWebReferenceRemovedEventHandler
    (webRefEvents_OnBeforeWebReferenceRemoved);
            webRefEvents.OnWebReferenceChanged -= new
     _dispVSLangProjWebReferencesEvents
    _OnWebReferenceChangedEventHandler
    (webRefEvents_OnWebReferenceChanged);
        }
    }
    

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

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

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

  15. 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.

    • On 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

The following example is a basic Visual Studio add-in that demonstrates how to respond to web references events by using Visual Studio automation.

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using System.Windows.Forms;
namespace MyAddin
{
public class Connect : Object, IDTExtensibility2
{
    public Connect()
    {
    }
    public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        // Call the WebReferenceEventsSample method.
        WebReferenceEventsSample(_applicationObject);
    }
    public void WebReferenceEventsSample(DTE2 dte)
    {
        try
         {
            Solution2 soln =
 (Solution2)_applicationObject.Solution;
            Project proj;
            VSProject2 vsproj;
            String webServiceRef;
            proj = soln.Projects.Item(1);
            // Get a reference to the VSProject2 object.
            vsproj = (VSProject2)proj.Object;
            //Connect Web references events.
            webRefEvents = 
vsproj.Events2.VSLangProjWebReferencesEvents;
            webRefEvents.OnAfterWebReferenceAdded +=new
 _dispVSLangProjWebReferencesEvents
_OnAfterWebReferenceAddedEventHandler
(webRefEvents_OnAfterWebReferenceAdded);
            webRefEvents.OnBeforeWebReferenceRemoved += new 
_dispVSLangProjWebReferencesEvents
_OnBeforeWebReferenceRemovedEventHandler
(webRefEvents_OnBeforeWebReferenceRemoved);
            webRefEvents.OnWebReferenceChanged += new
 _dispVSLangProjWebReferencesEvents
_OnWebReferenceChangedEventHandler
(webRefEvents_OnWebReferenceChanged);
            // Create a Web references folder.
            MessageBox.Show("Creating a Web references folder."
, "Information");
            vsproj.CreateWebReferencesFolder();
            // Add a Web reference to the folder.
            // Replace the "<web reference>" with an 
            // actual Web service URL.
            MessageBox.Show("Adding a Web reference."
, "Information");
            webServiceRef = "<web reference>";
            vsproj.AddWebReference(webServiceRef);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    void webRefEvents_OnWebReferenceChanged(object pDispatch)
    {
        MessageBox.Show("A Web reference was changed.");
    }
    void webRefEvents_OnBeforeWebReferenceRemoved(object pDispatch)
    {
        MessageBox.Show("A Web reference was removed.");
    }
    void webRefEvents_OnAfterWebReferenceAdded(object pDispatch)
    {
        MessageBox.Show("A Web reference was added.");
    }
    public void OnDisconnection(ext_DisconnectMode disconnectMode,
ref Array custom)
    {
        // If the delegate handlers have been connected, then 
        // disconnect them here. 
        // If you do not do this, the handlers may still 
        // fire because garbage collection has not removed them.
        if (webRefEvents != null)
        {
            webRefEvents.OnAfterWebReferenceAdded -= new
 _dispVSLangProjWebReferencesEvents
_OnAfterWebReferenceAddedEventHandler
 (webRefEvents_OnAfterWebReferenceAdded);
            webRefEvents.OnBeforeWebReferenceRemoved -= new
 _dispVSLangProjWebReferencesEvents
_OnBeforeWebReferenceRemovedEventHandler
(webRefEvents_OnBeforeWebReferenceRemoved);
            webRefEvents.OnWebReferenceChanged -= new
 _dispVSLangProjWebReferencesEvents
_OnWebReferenceChangedEventHandler
(webRefEvents_OnWebReferenceChanged);
        }
    }
    public void OnAddInsUpdate(ref Array custom)
    {
    }
    public void OnStartupComplete(ref Array custom)
    {
    }
    public void OnBeginShutdown(ref Array custom)
    {
    }

    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    private VSLangProj80.VSLangProjWebReferencesEvents webRefEvents;
    }
}

Compiling the Code

To compile this code, create a new Visual Studio add-in project in Visual C# 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 with the Add-In Manager.

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.