ServiceDescriptionImporter 類別

定義

公開如何產生 XML Web Service 用戶端 Proxy 類別的方法。

public ref class ServiceDescriptionImporter
public class ServiceDescriptionImporter
type ServiceDescriptionImporter = class
Public Class ServiceDescriptionImporter
繼承
ServiceDescriptionImporter

範例

下列範例說明 如何使用 ServiceDescriptionImporter 類別來產生 Proxy 用戶端程式代碼,以呼叫 WSDL 檔案所描述的 XML Web 服務。

#using <System.Xml.dll>
#using <System.Web.Services.dll>
#using <System.dll>

using namespace System;
using namespace System::Web::Services::Description;
using namespace System::CodeDom;
using namespace System::CodeDom::Compiler;

int main()
{
   // Get a WSDL file describing a service.
   ServiceDescription^ description = ServiceDescription::Read( "service.wsdl" );

   // Initialize a service description importer.
   ServiceDescriptionImporter^ importer = gcnew ServiceDescriptionImporter;
   importer->ProtocolName = "Soap12"; // Use SOAP 1.2.
   importer->AddServiceDescription( description, nullptr, nullptr );

   // Report on the service descriptions.
   Console::WriteLine( "Importing {0} service descriptions with {1} associated schemas.", importer->ServiceDescriptions->Count, importer->Schemas->Count );

   // Generate a proxy client.
   importer->Style = ServiceDescriptionImportStyle::Client;

   // Generate properties to represent primitive values.
   importer->CodeGenerationOptions = System::Xml::Serialization::CodeGenerationOptions::GenerateProperties;

   // Initialize a Code-DOM tree into which we will import the service.
   CodeNamespace^ nmspace = gcnew CodeNamespace;
   CodeCompileUnit^ unit = gcnew CodeCompileUnit;
   unit->Namespaces->Add( nmspace );
   
   // Import the service into the Code-DOM tree. This creates proxy code
   // that uses the service.
   ServiceDescriptionImportWarnings warning = importer->Import(nmspace,unit);
   if ( warning == (ServiceDescriptionImportWarnings)0 )
   {
      // Generate and print the proxy code in C#.
      CodeDomProvider^ provider = CodeDomProvider::CreateProvider( "CSharp" );
      ICodeGenerator^ generator = provider->CreateGenerator();
      generator->GenerateCodeFromCompileUnit( unit, Console::Out, gcnew CodeGeneratorOptions );
   }
   else
   {
      // Print an error message.
      Console::WriteLine( warning );
   }
}
using System;
using System.Web.Services.Description;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Security.Permissions;

public class Import {

    public static void Main() 
    {
        Run();
    }

    [PermissionSetAttribute(SecurityAction.Demand, Name = "Full Trust")]
    public static void Run()
    {
    // Get a WSDL file describing a service.
    ServiceDescription description = ServiceDescription.Read("service.wsdl");

    // Initialize a service description importer.
    ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
    importer.ProtocolName = "Soap12";  // Use SOAP 1.2.
    importer.AddServiceDescription(description,null,null);

    // Report on the service descriptions.
    Console.WriteLine("Importing {0} service descriptions with {1} associated schemas.",
                      importer.ServiceDescriptions.Count, importer.Schemas.Count);

    // Generate a proxy client.
    importer.Style = ServiceDescriptionImportStyle.Client;

    // Generate properties to represent primitive values.
    importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

    // Initialize a Code-DOM tree into which we will import the service.
    CodeNamespace nmspace = new CodeNamespace();
    CodeCompileUnit unit = new CodeCompileUnit();
    unit.Namespaces.Add(nmspace);

    // Import the service into the Code-DOM tree. This creates proxy code
    // that uses the service.
    ServiceDescriptionImportWarnings warning = importer.Import(nmspace,unit);

    if (warning == 0)
    {
        // Generate and print the proxy code in C#.
        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
        provider.GenerateCodeFromCompileUnit(unit, Console.Out, new CodeGeneratorOptions() );
    }
    else
    {
        // Print an error message.
        Console.WriteLine(warning); 
    }
}
}

備註

XML Web 服務的介面通常是由 Web 服務描述語言 (WSDL) 檔案描述。 例如,若要使用公開于 http://localhost/service.asmx 的 ASP.NET 取得 Web 服務的 WSDL 描述,只需流覽至 http://localhost/service.asmx?WSDL

類別 ServiceDescriptionImporter 可讓您輕鬆地將 WSDL 描述中包含的資訊匯入 System.CodeDom.CodeCompileUnit 物件。 藉由調整 參數的值 Style ,您可以指示 ServiceDescriptionImporter 實例產生用戶端 Proxy 類別,以透過透明方式呼叫 Web 服務來提供 Web 服務的功能,或產生抽象類別來封裝 Web 服務的功能,而不需要實作它。

然後,結果物件中的 CodeCompileUnit 程式碼可以直接呼叫,或以您選擇的語言匯出。

建構函式

ServiceDescriptionImporter()

初始化 ServiceDescriptionImporter 類別的新執行個體。

屬性

CodeGenerationOptions

取得或設定產生程式碼的各種選項。

CodeGenerator

取得或設定由服務描述匯入工具使用的程式碼產生器。

ProtocolName

取得或設定通訊協定,用於存取描述的 XML Web Service。

Schemas

取得由 XmlSchemas 屬性使用的 ServiceDescriptions

ServiceDescriptions

取得要匯入的 ServiceDescription 執行個體集合。

Style

取得或設定值,決定匯入 ServiceDescriptions 值時產生的程式碼樣式 (用戶端或伺服器)。

方法

AddServiceDescription(ServiceDescription, String, String)

將指定的 ServiceDescription 加入要匯出的 ServiceDescriptions 值集合。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GenerateWebReferences(WebReferenceCollection, CodeDomProvider, CodeCompileUnit, WebReferenceOptions)

編譯 Web 參考的集合,以產生用戶端 Proxy 或伺服器 Stub。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Import(CodeNamespace, CodeCompileUnit)

匯入指定的 ServiceDescriptions 值,其產生如 Style 屬性所指定的程式碼。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於