VSProject2.AddWebReference 方法 (String)

向项目添加对 Web 服务的引用。将向项目的“Web 引用”文件夹添加新的 Web 服务引用子文件夹。该新文件夹包含与该 Web 服务相关的若干其他项目项。该方法返回与新的“Web 服务”文件夹关联的 ProjectItem 对象。

命名空间:  VSLangProj80
程序集:  VSLangProj80(在 VSLangProj80.dll 中)

语法

声明
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

参数

  • bstrUrl
    类型:System.String
    必选。通常,这是带有 .disco 或 .vsdisco 扩展名的文件名 URL。

返回值

类型:EnvDTE.ProjectItem
返回 ProjectItem 对象,该对象是新的“Web 引用”文件夹。

备注

如果 WebReferencesFolder 为 Nothing(一个 nullnull 引用(在 Visual Basic 中为 Nothing) 引用),则创建项目的“Web 引用”文件夹的 ProjectItem,并设置 WebReferencesFolder

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

用途

映射文件 (Reference.map)

该 XML 文件将 URL 映射到本地缓存文件位置。它列出 Web 服务的发现文件和服务合同文件。

服务合同文件 (.wsdl)

这些 SOAP 文件指定 Web 服务的接口。“Web 引用”文件夹中可能存在不止一个合同文件。

XML 架构定义文件 (.xsd)

这些文件包含 Web 服务的 XML 架构定义。“Web 引用”文件夹中可能存在不止一个架构文件。

发现文件(.disco 或 .vsdisco)

此 XML 文件包含指向描述 Web 服务的其他资源的链接。

示例

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

[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 安全性

请参见

参考

VSProject2 接口

AddWebReference 重载

VSLangProj80 命名空间