Condividi tramite


VSProject2.AddWebReference(String) Method

Definition

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.

public:
 EnvDTE::ProjectItem ^ AddWebReference(System::String ^ bstrUrl);
public:
 EnvDTE::ProjectItem ^ AddWebReference(Platform::String ^ bstrUrl);
EnvDTE::ProjectItem AddWebReference(std::wstring const & bstrUrl);
[System.Runtime.InteropServices.DispId(7)]
public EnvDTE.ProjectItem AddWebReference (string bstrUrl);
[<System.Runtime.InteropServices.DispId(7)>]
abstract member AddWebReference : string -> EnvDTE.ProjectItem
Public Function AddWebReference (bstrUrl As String) As ProjectItem

Parameters

bstrUrl
String

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

Returns

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

Implements

Attributes

Examples

This example adds a web service to a Visual Basic or Visual C# project. Before running this sample, replace the bstrUrl parameter : http://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( _  
    "http://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  
("http://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());  
}  

Remarks

If the WebReferencesFolder is Nothing (a null 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.

Applies to