ServiceDescriptionImporter 類別

定義

公開如何產生 XML Web Service 用戶端 Proxy 類別的方法。Exposes a means of generating client proxy classes for XML Web services.

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

範例

下列範例說明如何使用ServiceDescriptionImporter類別來產生 proxy 用戶端程式代碼, 以呼叫 WSDL 檔案所描述的 XML Web Service。The following example illustrates the use of the ServiceDescriptionImporter class to generate proxy client code that calls an XML Web service described by a WSDL file.

#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 Service 的介面通常是由 Web 服務描述語言 (WSDL) 檔案所描述。The interface to an XML Web service is typically described by a Web Services Description Language (WSDL) file. 例如, 若要使用公開于的http://localhost/service.asmxASP.NET 來取得 Web 服務的 WSDL 描述, 只要流覽至。 http://localhost/service.asmx?WSDLFor example, to obtain a WSDL description of a Web service using ASP.NET exposed at http://localhost/service.asmx, simply navigate to http://localhost/service.asmx?WSDL.

類別可讓您輕鬆地將 WSDL 描述System.CodeDom.CodeCompileUnit中包含的資訊匯入物件中。 ServiceDescriptionImporterThe ServiceDescriptionImporter class allows you to easily import the information contained in a WSDL description into a System.CodeDom.CodeCompileUnit object. 藉由調整Style參數的值, 您可以ServiceDescriptionImporter指示實例產生用戶端 proxy 類別, 藉由明確地呼叫它來提供 Web 服務的功能, 或產生抽象類別,封裝 Web 服務的功能而不加以執行。By adjusting the value of the Style parameter, you can instruct a ServiceDescriptionImporter instance either to generate a client proxy class that provides the functionality of the Web service by transparently calling it or to generate an abstract class that encapsulates the functionality of the Web service without implementing it.

然後, 可以直接呼叫CodeCompileUnit所產生物件中的程式碼, 或以您選擇的語言匯出。The code in the resulting CodeCompileUnit object can then either be called directly or exported in the language of your choice.

建構函式

ServiceDescriptionImporter()

初始化 ServiceDescriptionImporter 類別的新執行個體。Initializes a new instance of the ServiceDescriptionImporter class.

屬性

CodeGenerationOptions

取得或設定產生程式碼的各種選項。Gets or sets various options for code generation.

CodeGenerator

取得或設定由服務描述匯入工具使用的程式碼產生器。Gets or sets the code generator used by the service description importer.

ProtocolName

取得或設定通訊協定,用於存取描述的 XML Web Service。Gets or sets the protocol used to access the described XML Web services.

Schemas

取得由 XmlSchemas 屬性使用的 ServiceDescriptionsGets the XmlSchemas used by the ServiceDescriptions property.

ServiceDescriptions

取得要匯入的 ServiceDescription 執行個體集合。Gets the collection of ServiceDescription instances to be imported.

Style

取得或設定值,決定匯入 ServiceDescriptions 值時產生的程式碼樣式 (用戶端或伺服器)。Gets or sets a value that determines the style of code (client or server) that is generated when the ServiceDescriptions values are imported.

方法

AddServiceDescription(ServiceDescription, String, String)

將指定的 ServiceDescription 加入要匯出的 ServiceDescriptions 值集合。Adds the specified ServiceDescription to the collection of ServiceDescriptions values to be imported.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

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

編譯 Web 參考的集合,以產生用戶端 Proxy 或伺服器 Stub。Compiles a collection of Web references to produce a client proxy or a server stub.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

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

匯入指定的 ServiceDescriptions 值,其產生如 Style 屬性所指定的程式碼。Imports the specified ServiceDescriptions values, that generates code as specified by the Style property.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於