ServiceDescriptionImporter 类

定义

公开一种为 XML Web services 生成客户端代理类的方法。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 如何使用类生成代理客户端代码,以调用 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. 例如,若要使用公开的 ASP.NET 获取 Web 服务的 WSDL 说明 http://localhost/service.asmx ,只需导航到 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.

ServiceDescriptionImporter类使你可以轻松地将 WSDL 说明中包含的信息导入 System.CodeDom.CodeCompileUnit 对象。The ServiceDescriptionImporter class allows you to easily import the information contained in a WSDL description into a System.CodeDom.CodeCompileUnit object. 通过调整参数的值 Style ,你可以指示一个 ServiceDescriptionImporter 实例生成一个客户端代理类,该类通过以透明方式调用它来提供 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 services 的协议。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 引用的集合,以生成客户端代理或服务器存根。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)

适用于