ServiceContractGenerator 类
定义
ServiceContractGenerator 类型可以从 ServiceEndpoint 说明对象生成服务协定代码和绑定配置。The ServiceContractGenerator type generates service contract code and binding configurations from ServiceEndpoint description objects.
public ref class ServiceContractGenerator
public class ServiceContractGenerator
type ServiceContractGenerator = class
Public Class ServiceContractGenerator
- 继承
-
ServiceContractGenerator
示例
下面的代码示例演示了如何使用 ServiceContractGenerator 将下载的元数据转换为代码。The following example shows the use of a ServiceContractGenerator to convert downloaded metadata into code.
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 可以创建工具或修改默认协定生成过程。Use the ServiceContractGenerator to create tools or to modify the default contract generation process using an System.ServiceModel.Description.IWsdlImportExtension.
构造函数
| ServiceContractGenerator() |
利用新 ServiceContractGenerator 实例初始化 CodeCompileUnit 类的新实例。Initializes a new instance of the ServiceContractGenerator class with a new CodeCompileUnit instance. |
| ServiceContractGenerator(CodeCompileUnit) |
使用指定的 ServiceContractGenerator 实例初始化 CodeCompileUnit 类的新实例。Initializes a new instance of the ServiceContractGenerator class with the specified CodeCompileUnit instance. |
| ServiceContractGenerator(CodeCompileUnit, Configuration) |
利用指定的 ServiceContractGenerator 实例和 CodeCompileUnit 实例初始化 Configuration 类的新实例。Initializes a new instance of the ServiceContractGenerator class with the specified CodeCompileUnit instance and the specified Configuration instance. |
| ServiceContractGenerator(Configuration) |
使用指定的 ServiceContractGenerator 实例初始化 Configuration 类的新实例。Initializes a new instance of the ServiceContractGenerator class with the specified Configuration instance. |
属性
| Configuration |
获取包含所生成的绑定配置的 Configuration 实例。Gets the Configuration instance that contains the generated binding configurations. |
| Errors |
获取生成服务协定代码和终结点配置时生成的 MetadataConversionError 对象的集合。Gets a collection of MetadataConversionError objects generated when generating service contract code and endpoint configurations. |
| NamespaceMappings |
获取生成代码时使用的从协定说明命名空间到托管命名空间的映射。Gets a mapping from contract description namespaces to managed namespaces that is used when generating code. |
| Options |
获取或设置服务协定代码的生成选项。Gets or sets options for generating service contract code. |
| ReferencedTypes |
获取从协定说明到引用协定类型的映射。Gets a mapping from contract descriptions to referenced contract types. |
| TargetCompileUnit |
为生成服务协定代码获取目标 CodeCompileUnit 对象。Gets the target CodeCompileUnit object for generating service contract code. |
方法
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GenerateBinding(Binding, String, String) |
为指定的 Binding 实例生成绑定配置。Generates a binding configuration for the specified Binding instance. |
| GenerateServiceContractType(ContractDescription) |
从指定的 ContractDescription 实例生成服务协定类型。Generates a service contract type from the specified ContractDescription instance. |
| GenerateServiceEndpoint(ServiceEndpoint, ChannelEndpointElement) |
从指定的 ServiceEndpoint 实例生成服务协定类型和终结点配置。Generates a service contract type and an endpoint configuration from the specified ServiceEndpoint instance. |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |