ServiceDescriptionImporter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
揭示一種為 XML Web 服務產生客戶端代理類別的方法。
public ref class ServiceDescriptionImporter
public class ServiceDescriptionImporter
type ServiceDescriptionImporter = class
Public Class ServiceDescriptionImporter
- 繼承
-
ServiceDescriptionImporter
範例
以下範例說明了使用該 ServiceDescriptionImporter 類別產生代理客戶端程式碼,呼叫由 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 網路服務的介面通常由網路服務描述語言(WSDL)檔案描述。 例如,若要取得使用 http://localhost/service.asmx 暴露的 ASP.NET 的 WSDL 網路服務描述,只需導覽至 http://localhost/service.asmx?WSDL。
這個 ServiceDescriptionImporter 類別讓你能輕鬆地將 WSDL 描述中的資訊匯入物件 System.CodeDom.CodeCompileUnit 中。 透過調整參數值 Style ,你可以指示實 ServiceDescriptionImporter 例產生一個客戶端代理類別,透過透明呼叫來提供 Web 服務的功能,或產生一個抽象類別,封裝 Web 服務的功能而不實作。
所得 CodeCompileUnit 物件中的程式碼可以直接呼叫,或以你選擇的語言匯出。
Important
不要用 ServiceDescriptionImporter 不可靠的輸入。 從不受信任來源匯入 ServiceDescription 可能導致程式碼產生,存取任意 URL 或實例化任意 .NET 類型,進而可能導致安全漏洞。
建構函式
| 名稱 | Description |
|---|---|
| ServiceDescriptionImporter() |
初始化 ServiceDescriptionImporter 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| CodeGenerationOptions |
取得或設定各種程式碼產生選項。 |
| CodeGenerator |
取得或設定服務描述匯入器使用的程式碼產生器。 |
| ProtocolName |
取得或設定用於存取所述 XML 網路服務的協定。 |
| Schemas |
被 XmlSchemas 物業使用 ServiceDescriptions 。 |
| ServiceDescriptions |
取得要匯入的實例集合 ServiceDescription 。 |
| Style |
取得或設定一個值,決定匯入值時 ServiceDescriptions 產生的程式碼風格(客戶端或伺服器)。 |
方法
| 名稱 | Description |
|---|---|
| AddServiceDescription(ServiceDescription, String, String) |
將指定的 ServiceDescription 值加入要匯入的值集合 ServiceDescriptions 中。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GenerateWebReferences(WebReferenceCollection, CodeDomProvider, CodeCompileUnit, WebReferenceOptions) |
編譯一組 Web 參考,產生用戶端代理或伺服器存根。 |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| Import(CodeNamespace, CodeCompileUnit) |
匯入指定的 ServiceDescriptions 值,產生依屬性指定的 Style 程式碼。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |