VSProject2.AddWebReference(String) 方法

定义

向项目添加对 Web 服务的引用。 新的 Web 服务引用子文件夹将添加到项目的 "Web 引用" 文件夹中。 此新文件夹包含与 Web 服务相关的其他几个项目项。 方法返回 ProjectItem 与新的 Web 服务文件夹关联的对象。

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

参数

bstrUrl
String

必需。 通常,这是扩展名为 .disco 或 .vsdisco 的文件名 URL。

返回

ProjectItem

返回一个 ProjectItem 对象,该对象是新的 Web 引用文件夹。

实现

属性

示例

此示例将 web 服务添加到 Visual Basic 或 Visual C# 项目。 在运行此示例之前,请将参数替换为 bstrUrl http://ServerName/Application/myServiceName.asmx 实际 URL。 若要将此示例作为外接程序运行,请参阅 如何:编译和运行自动化对象模型代码示例

[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());  
}  

注解

如果 WebReferencesFolder Nothing (null 引用) ,则会 ProjectItem 创建项目的 "Web 引用" 文件夹的,并 WebReferencesFolder 设置。

创建 Web 服务的 Web 引用时,会将新的文件夹类型 ProjectItem 添加到项目的 ProjectItems 集合中。 这一新 ProjectItem 包含在其 ProjectItems 属性中组成 Web 引用规范的各个项。 下表描述了 Web 引用规范中包含的四种类型的项。

用途
映射文件 (引用 .map) 此 XML 文件将 Url 映射到本地缓存的文件位置。 它列出了 Web 服务的发现文件和服务协定文件。
服务协定文件 ( .wsdl) 这些 SOAP 文件指定 Web 服务的接口。 "Web 引用" 文件夹中可能有多个协定文件。
XML 架构定义文件 ( .xsd) 这些文件包含 Web 服务的 XML 架构定义。 "Web 引用" 文件夹中可能有多个架构文件。
发现文件 ( .disco 或 .vsdisco) 此 XML 文件包含指向描述 Web 服务的其他资源的链接。

适用于