internal XmlSerializerOperationGenerator(XmlSerializerImportOptions options) { operationGenerator = new OperationGenerator(); this.options = options; this.codeNamespace = GetTargetCodeNamespace(options); partInfoTable = new Dictionary<MessagePartDescription, PartInfo>(); }
// Assumption: gets called exactly once per operation void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { DataContractSerializerOperationBehavior DataContractSerializerOperationBehavior = context.Operation.Behaviors.Find <DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior; DataContractFormatAttribute dataContractFormatAttribute = (DataContractSerializerOperationBehavior == null) ? new DataContractFormatAttribute() : DataContractSerializerOperationBehavior.DataContractFormatAttribute; OperationFormatStyle style = dataContractFormatAttribute.Style; operationGenerator.GenerateOperation(context, ref style, false /*isEncoded*/, new WrappedBodyTypeGenerator(this, context), knownTypes); dataContractFormatAttribute.Style = style; if (dataContractFormatAttribute.Style != TypeLoader.DefaultDataContractFormatAttribute.Style) { context.SyncMethod.CustomAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, dataContractFormatAttribute)); } if (knownTypes != null) { Dictionary <CodeTypeReference, object> operationKnownTypes = new Dictionary <CodeTypeReference, object>(new CodeTypeReferenceComparer()); foreach (MessageDescription message in context.Operation.Messages) { foreach (MessagePartDescription part in message.Body.Parts) { AddKnownTypesForPart(context, part, operationKnownTypes); } foreach (MessageHeaderDescription header in message.Headers) { AddKnownTypesForPart(context, header, operationKnownTypes); } if (OperationFormatter.IsValidReturnValue(message.Body.ReturnValue)) { AddKnownTypesForPart(context, message.Body.ReturnValue, operationKnownTypes); } } } UpdateTargetCompileUnit(context, this.codeCompileUnit); }
internal XmlSerializerOperationGenerator(XmlSerializerImportOptions options) { operationGenerator = new OperationGenerator(); this.options = options; this.codeNamespace = GetTargetCodeNamespace(options); partInfoTable = new Dictionary <MessagePartDescription, PartInfo>(); }
public void AddTypeAttributes(string messageName, string typeNS, CodeAttributeDeclarationCollection typeAttributes, bool isEncoded) { if (!isEncoded) { XmlTypeAttribute attribute = new XmlTypeAttribute { Namespace = typeNS }; typeAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(this.context.Contract.ServiceContractGenerator, attribute)); } }
public void AddTypeAttributes(string messageName, string typeNS, CodeAttributeDeclarationCollection typeAttributes, bool isEncoded) { // we do not need top-level attibutes for the encoded SOAP if (isEncoded) { return; } XmlTypeAttribute xmlType = new XmlTypeAttribute(); xmlType.Namespace = typeNS; typeAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, xmlType)); }
// Assumption: gets called exactly once per operation void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { if (context == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context"); } if (partInfoTable != null && partInfoTable.Count > 0) { Dictionary <XmlMembersMapping, XmlMembersMapping> alreadyExported = new Dictionary <XmlMembersMapping, XmlMembersMapping>(); foreach (MessageDescription message in context.Operation.Messages) { foreach (MessageHeaderDescription header in message.Headers) { GeneratePartType(alreadyExported, header, header.Namespace); } MessageBodyDescription body = message.Body; bool isWrapped = (body.WrapperName != null); if (OperationFormatter.IsValidReturnValue(body.ReturnValue)) { GeneratePartType(alreadyExported, body.ReturnValue, isWrapped ? body.WrapperNamespace : body.ReturnValue.Namespace); } foreach (MessagePartDescription part in body.Parts) { GeneratePartType(alreadyExported, part, isWrapped ? body.WrapperNamespace : part.Namespace); } } } XmlSerializerOperationBehavior xmlSerializerOperationBehavior = context.Operation.Behaviors.Find <XmlSerializerOperationBehavior>() as XmlSerializerOperationBehavior; if (xmlSerializerOperationBehavior == null) { return; } XmlSerializerFormatAttribute xmlSerializerFormatAttribute = (xmlSerializerOperationBehavior == null) ? new XmlSerializerFormatAttribute() : xmlSerializerOperationBehavior.XmlSerializerFormatAttribute; OperationFormatStyle style = xmlSerializerFormatAttribute.Style; operationGenerator.GenerateOperation(context, ref style, xmlSerializerFormatAttribute.IsEncoded, new WrappedBodyTypeGenerator(context), new Dictionary <MessagePartDescription, ICollection <CodeTypeReference> >()); context.ServiceContractGenerator.AddReferencedAssembly(typeof(System.Xml.Serialization.XmlTypeAttribute).Assembly); xmlSerializerFormatAttribute.Style = style; context.SyncMethod.CustomAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, xmlSerializerFormatAttribute)); AddKnownTypes(context.SyncMethod.CustomAttributes, xmlSerializerFormatAttribute.IsEncoded ? SoapExporter.IncludeMetadata : XmlExporter.IncludeMetadata); DataContractSerializerOperationGenerator.UpdateTargetCompileUnit(context, this.options.CodeCompileUnit); }
public void AddMemberAttributes(XmlName messageName, MessagePartDescription part, CodeAttributeDeclarationCollection attributesImported, CodeAttributeDeclarationCollection typeAttributes, CodeAttributeDeclarationCollection fieldAttributes) { CodeAttributeDeclaration dataContractAttributeDecl = null; foreach (CodeAttributeDeclaration attr in typeAttributes) { if (attr.AttributeType.BaseType == dataContractAttributeTypeRef.BaseType) { dataContractAttributeDecl = attr; break; } } if (dataContractAttributeDecl == null) { Fx.Assert(String.Format(CultureInfo.InvariantCulture, "Cannot find DataContract attribute for {0}", messageName)); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(String.Format(CultureInfo.InvariantCulture, "Cannot find DataContract attribute for {0}", messageName))); } bool nsAttrFound = false; foreach (CodeAttributeArgument attrArg in dataContractAttributeDecl.Arguments) { if (attrArg.Name == "Namespace") { nsAttrFound = true; string nsValue = ((CodePrimitiveExpression)attrArg.Value).Value.ToString(); if (nsValue != part.Namespace) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxWrapperTypeHasMultipleNamespaces, messageName))); } } } if (!nsAttrFound) { dataContractAttributeDecl.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(part.Namespace))); } DataMemberAttribute dataMemberAttribute = new DataMemberAttribute(); dataMemberAttribute.Order = memberCount++; dataMemberAttribute.EmitDefaultValue = !IsNonNillableReferenceType(part); fieldAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, dataMemberAttribute)); }
public void AddMemberAttributes(XmlName messageName, MessagePartDescription part, CodeAttributeDeclarationCollection attributesImported, CodeAttributeDeclarationCollection typeAttributes, CodeAttributeDeclarationCollection fieldAttributes) { CodeAttributeDeclaration declaration = null; foreach (CodeAttributeDeclaration declaration2 in typeAttributes) { if (declaration2.AttributeType.BaseType == dataContractAttributeTypeRef.BaseType) { declaration = declaration2; break; } } if (declaration == null) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(string.Format(CultureInfo.InvariantCulture, "Cannot find DataContract attribute for {0}", new object[] { messageName }))); } bool flag = false; foreach (CodeAttributeArgument argument in declaration.Arguments) { if (argument.Name == "Namespace") { flag = true; if (((CodePrimitiveExpression)argument.Value).Value.ToString() != part.Namespace) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxWrapperTypeHasMultipleNamespaces", new object[] { messageName }))); } } } if (!flag) { declaration.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(part.Namespace))); } DataMemberAttribute attribute = new DataMemberAttribute { Order = this.memberCount++, EmitDefaultValue = !this.IsNonNillableReferenceType(part) }; fieldAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(this.context.Contract.ServiceContractGenerator, attribute)); }
public void AddTypeAttributes(string messageName, string typeNS, CodeAttributeDeclarationCollection typeAttributes, bool isEncoded) { typeAttributes.Add(OperationGenerator.GenerateAttributeDeclaration(this.context.Contract.ServiceContractGenerator, new DataContractAttribute())); this.memberCount = 0; }
public DataContractSerializerOperationGenerator(CodeCompileUnit codeCompileUnit) { this.codeCompileUnit = codeCompileUnit; this.operationGenerator = new OperationGenerator(); }