ServiceContractGenerator 类

定义

ServiceContractGenerator 类型从 ServiceEndpoint 说明对象生成服务协定代码和绑定配置。

public ref class ServiceContractGenerator
public class ServiceContractGenerator
type ServiceContractGenerator = class
Public Class ServiceContractGenerator
继承
ServiceContractGenerator

示例

下面的示例演示如何使用 ServiceContractGenerator 将下载的元数据转换为代码。

  static void GenerateCSCodeForService(EndpointAddress metadataAddress, string outputFile)
  {
MetadataExchangeClient mexClient = new MetadataExchangeClient(metadataAddress);
mexClient.ResolveMetadataReferences = true;
MetadataSet metaDocs = mexClient.GetMetadata();

      WsdlImporter importer = new WsdlImporter(metaDocs);
ServiceContractGenerator generator = new ServiceContractGenerator();

// Add our custom DCAnnotationSurrogate
// to write XSD annotations into the comments.
object dataContractImporter;
XsdDataContractImporter xsdDCImporter;
if (!importer.State.TryGetValue(typeof(XsdDataContractImporter), out dataContractImporter))
{
  Console.WriteLine("Couldn't find the XsdDataContractImporter! Adding custom importer.");
  xsdDCImporter = new XsdDataContractImporter();
  xsdDCImporter.Options = new ImportOptions();
  importer.State.Add(typeof(XsdDataContractImporter), xsdDCImporter);
}
else
{
  xsdDCImporter = (XsdDataContractImporter)dataContractImporter;
  if (xsdDCImporter.Options == null)
  {
    Console.WriteLine("There were no ImportOptions on the importer.");
    xsdDCImporter.Options = new ImportOptions();
  }
}
xsdDCImporter.Options.DataContractSurrogate = new DCAnnotationSurrogate();

// Uncomment the following code if you are going to do your work programmatically rather than add
// the WsdlDocumentationImporters through a configuration file.
/*
// The following code inserts a custom WsdlImporter without removing the other
// importers already in the collection.
System.Collections.Generic.IEnumerable<IWsdlImportExtension> exts = importer.WsdlImportExtensions;
System.Collections.Generic.List<IWsdlImportExtension> newExts
  = new System.Collections.Generic.List<IWsdlImportExtension>();
foreach (IWsdlImportExtension ext in exts)
{
  Console.WriteLine("Default WSDL import extensions: {0}", ext.GetType().Name);
  newExts.Add(ext);
}
newExts.Add(new WsdlDocumentationImporter());
System.Collections.Generic.IEnumerable<IPolicyImportExtension> polExts = importer.PolicyImportExtensions;
importer = new WsdlImporter(metaDocs, polExts, newExts);
*/

System.Collections.ObjectModel.Collection<ContractDescription> contracts
  = importer.ImportAllContracts();
importer.ImportAllEndpoints();
      foreach (ContractDescription contract in contracts)
      {
          generator.GenerateServiceContractType(contract);
      }
if (generator.Errors.Count != 0)
  throw new Exception("There were errors during code compilation.");

// Write the code dom
System.CodeDom.Compiler.CodeGeneratorOptions options
  = new System.CodeDom.Compiler.CodeGeneratorOptions();
      options.BracingStyle = "C";
      System.CodeDom.Compiler.CodeDomProvider codeDomProvider
  = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("C#");
      System.CodeDom.Compiler.IndentedTextWriter textWriter
  = new System.CodeDom.Compiler.IndentedTextWriter(new System.IO.StreamWriter(outputFile));
      codeDomProvider.GenerateCodeFromCompileUnit(
  generator.TargetCompileUnit, textWriter, options
);
      textWriter.Close();
  }

注解

使用 ServiceContractGenerator 创建工具或修改默认协定生成过程 System.ServiceModel.Description.IWsdlImportExtension

构造函数

名称 说明
ServiceContractGenerator()

使用新ServiceContractGenerator实例初始化类的新实例CodeCompileUnit

ServiceContractGenerator(CodeCompileUnit, Configuration)

使用指定的实例和指定的ServiceContractGeneratorCodeCompileUnit实例初始化类的新实例Configuration

ServiceContractGenerator(CodeCompileUnit)

使用指定的ServiceContractGenerator实例初始化类的新实例CodeCompileUnit

ServiceContractGenerator(Configuration)

使用指定的ServiceContractGenerator实例初始化类的新实例Configuration

属性

名称 说明
Configuration

Configuration获取包含生成的绑定配置的实例。

Errors

获取生成服务协定代码和终结点配置时生成的对象的集合 MetadataConversionError

NamespaceMappings

获取从协定说明命名空间到生成代码时使用的托管命名空间的映射。

Options

获取或设置用于生成服务协定代码的选项。

ReferencedTypes

获取从协定说明到引用的协定类型的映射。

TargetCompileUnit

获取用于生成服务协定代码的目标 CodeCompileUnit 对象。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GenerateBinding(Binding, String, String)

为指定的 Binding 实例生成绑定配置。

GenerateServiceContractType(ContractDescription)

从指定的 ContractDescription 实例生成服务协定类型。

GenerateServiceEndpoint(ServiceEndpoint, ChannelEndpointElement)

从指定的 ServiceEndpoint 实例生成服务协定类型和终结点配置。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于