共用方式為


VSProject2.AddWebReference 方法

將 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
    類型:String

    必要項。 這通常是具有 .disco 或 .vsdisco 副檔名的檔名 URL。

傳回值

類型:ProjectItem
傳回代表 Web 參考資料夾的 ProjectItem 物件。

備註

如果 WebReferencesFolder 是 Nothing (nullnull 參考 (即 Visual Basic 中的 Nothing) 參考),便會建立專案 Web 參考資料夾的 ProjectItem,也會設定 WebReferencesFolder

當建立 Web 服務的 Web 參考時,新的資料夾類型的 ProjectItem 會加入至專案的 ProjectItems 集合。 這個新的 ProjectItem 在其 ProjectItems 屬性中包含組成 Web 參考規格的個別項目。 下列資料表將說明包含在 Web 參考規格中的四種項目類型。

項目

用途

對應檔 (Reference.map)

這個 XML 檔將 URL 對應至本機快取的檔案位置。 它會列出 Web 服務的探索檔以及服務合約檔。

服務合約檔 (.wsdl)

這些 SOAP 檔案指定 Web 服務的介面。 Web 參考資料夾中可能有一個以上的合約檔。

XML 結構描述定義檔 (.xsd)

這些檔案包含 Web 服務的 XML 結構描述 (Schema) 定義。 Web 參考資料夾中可能有一個以上的結構描述檔。

探索檔 (.disco 或 .vsdisco)

這個 XML 檔包含說明 Web 服務的其他資源連結。

範例

此範例會將 Web 服務加入至 Visual Basic 或 Visual C# 專案。 執行此範例之前,請先以實際的 URL 取代 bstrUrl 參數 : http://ServerName/Application/myServiceName.asmx。 若要將此範例當做增益集來執行,請參閱 如何:編譯和執行 Automation 物件模型程式碼範例

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

.NET Framework 安全性

請參閱

參考

VSProject2 介面

VSLangProj80 命名空間