VSProject2.AddWebReference Method

Adds a reference to a Web Service to the project. A new Web Service reference subfolder is added to the Web References folder of the project. This new folder contains several other project items related to the Web Service. The method returns the ProjectItem object associated with the new Web Service folder.

Namespace:  VSLangProj80
Assembly:  VSLangProj80 (in VSLangProj80.dll)

Syntax

'Declaration
Function AddWebReference ( _
    bstrUrl As String _
) As ProjectItem
ProjectItem AddWebReference(
    string bstrUrl
)
ProjectItem^ AddWebReference(
    [InAttribute] String^ bstrUrl
)
abstract AddWebReference : 
        bstrUrl:string -> ProjectItem
function AddWebReference(
    bstrUrl : String
) : ProjectItem

Parameters

  • bstrUrl
    Type: System.String

    Required. Typically, this is a file name URL with a .disco or .vsdisco extension.

Return Value

Type: EnvDTE.ProjectItem
Returns a ProjectItem object that is the new Web Reference folder.

Remarks

If the WebReferencesFolder is Nothing (a nulla null reference (Nothing in Visual Basic) reference), then a ProjectItem for the Web References folder for the project is created and the WebReferencesFolder is set.

When a Web reference to a Web service is created, a new folder type, ProjectItem, is added to the project's ProjectItems collection. This new ProjectItem contains, in its ProjectItems property, the individual items that make up a Web reference specification. The four types of items included in a Web reference specification are described in the following table.

Item

Purpose

Map file (Reference.map)

This XML file maps URLs to the local cached file location. It lists the discovery file and the service contract files for the Web service.

Service Contract files (.wsdl)

These SOAP files specify the interface of the Web service. There may be more than one contract file in the Web Reference folder.

XML Schema Definition files (.xsd)

These files contain XML schema definitions for the Web service. There may be more than one schema file in the Web Reference folder.

Discovery file (.disco or .vsdisco)

This XML file contains links to other resources that describe the Web service.

Examples

This example adds a web service to a Visual Basic or Visual C# project. Before running this sample, replace the bstrUrl parameter : https://ServerName/Application/myServiceName.asmx,with an actual URL. To run this example as an add-in, see How to: Compile and Run the Automation Object Model Code Examples.

[Visual Basic]

' Add-in code.
Imports VSLangProj
Imports VSLangProj80
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)
    AddWebReferenceExample(applicationObject)
End Sub
Sub AddWebReferenceExample(ByVal dte As DTE2)
    ' This example assumes that the first project in the solution is 
    ' a Visual Basic or C# project.
    Dim aVSProject As VSProject2 = _
    CType(applicationObject.Solution.Projects.Item(1).Object, _
    VSProject2)
    ' The new project item is a folder.
    Dim newFolder As ProjectItem
    ' Replace the sample URL with an actual URL.
    newFolder = aVSProject.AddWebReference( _
    "https://ServerName/Application/myServiceName.asmx")
    ' The new name of the folder appears in Solution Explorer.
    newFolder.Name = "NewName"
    ' The ProjectItems collection for the folder is not empty.
    MsgBox(newFolder.ProjectItems.Count.ToString())
End Sub

[C#]

using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;

public void OnConnection(object application, ext_ConnectMode
 connectMode, object addInInst, ref Array custom)
{
    applicationObject = (DTE2)application;
    addInInstance = (AddIn)addInInst;
    AddWebReferenceExample((DTE2)applicationObject);
}

public void AddWebReferenceExample(DTE2 dte)
{
    // This example assumes that the first project in the solution is 
    // a Visual Basic or C# project.
    VSProject2 aVSProject =
 ((VSProject2)( applicationObject.Solution.Projects.Item(1).Object));
    // The new project item is a folder.
    ProjectItem newFolder = null;
    // Replace the sample URL with an actual URL.
    newFolder = aVSProject.AddWebReference
("https://ServerName/Application/myServiceName.asmx ");
    // The new name of the folder appears in Solution Explorer.
    newFolder.Name = "NewName";
    // The ProjectItems collection for the folder is not empty.
    MessageBox.Show("Number of items in the Web Reference folder: \n"
 + newFolder.ProjectItems.Count.ToString());
}

.NET Framework Security

See Also

Reference

VSProject2 Interface

VSLangProj80 Namespace