ServiceDescriptionImporter Class

Exposes a means of generating client proxy classes for XML Web services.

Namespace:  System.Web.Services.Description
Assembly:  System.Web.Services (in System.Web.Services.dll)

[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public class ServiceDescriptionImporter

The interface to an XML Web service is typically described by a Web Services Description Language (WSDL) file. For 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.

The ServiceDescriptionImporter class allows you to easily import the information contained in a WSDL description into a System.CodeDom.CodeCompileUnit object. 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.

The code in the resulting CodeCompileUnit object can then either be called directly or exported in the language of your choice.

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


}
#using <mscorlib.dll>
#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(S"service.wsdl");

    // Initialize a service description importer.
    ServiceDescriptionImporter* importer = new ServiceDescriptionImporter();
    importer->ProtocolName = S"Soap12";  // Use SOAP 1.2.
    importer->AddServiceDescription(description,0,0);

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

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

    // Use types from the System.Data.SQLTypes namespace. 
    importer->CodeGenerationOptions = System::Xml::Serialization::CodeGenerationOptions::UseSqlTypes;

    // 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(S"CSharp");
        ICodeGenerator* generator = provider->CreateGenerator();
        generator->GenerateCodeFromCompileUnit(unit, Console::Out, new CodeGeneratorOptions() );
    }
    else
    {
        // Print an error message.
        Console::WriteLine(warning); 
    }

}

System.Object
  System.Web.Services.Description.ServiceDescriptionImporter

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft