static CodeAttributeDeclaration CreateAttrDecl(OperationContractGenerationContext context, TransactionFlowAttribute attr) { CodeAttributeDeclaration attrDecl = new CodeAttributeDeclaration(context.Contract.ServiceContractGenerator.GetCodeTypeReference(typeof(TransactionFlowAttribute))); attrDecl.Arguments.Add(new CodeAttributeArgument(ServiceContractGenerator.GetEnumReference <TransactionFlowOption>(attr.Transactions))); return(attrDecl); }
private static CodeAttributeDeclaration CreateAttrDecl(OperationContractGenerationContext context, FaultDescription fault) { CodeTypeReference type = (fault.DetailType != null) ? context.Contract.ServiceContractGenerator.GetCodeTypeReference(fault.DetailType) : fault.DetailTypeReference; if ((type == null) || (type == voidTypeReference)) { return(null); } CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(context.ServiceContractGenerator.GetCodeTypeReference(typeof(FaultContractAttribute))); declaration.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(type))); if (fault.Action != null) { declaration.Arguments.Add(new CodeAttributeArgument("Action", new CodePrimitiveExpression(fault.Action))); } if (fault.HasProtectionLevel) { declaration.Arguments.Add(new CodeAttributeArgument("ProtectionLevel", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(ProtectionLevel)), fault.ProtectionLevel.ToString()))); } if (!XmlName.IsNullOrEmpty(fault.ElementName)) { declaration.Arguments.Add(new CodeAttributeArgument("Name", new CodePrimitiveExpression(fault.ElementName.EncodedName))); } if (fault.Namespace != context.Contract.Contract.Namespace) { declaration.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(fault.Namespace))); } return(declaration); }
static CodeAttributeDeclaration CreateAttrDecl(OperationContractGenerationContext context, FaultDescription fault) { CodeTypeReference exceptionTypeReference = fault.DetailType != null?context.Contract.ServiceContractGenerator.GetCodeTypeReference(fault.DetailType) : fault.DetailTypeReference; if (exceptionTypeReference == null || exceptionTypeReference == voidTypeReference) { return(null); } CodeAttributeDeclaration faultContractAttr = new CodeAttributeDeclaration(context.ServiceContractGenerator.GetCodeTypeReference(typeof(FaultContractAttribute))); faultContractAttr.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(exceptionTypeReference))); if (fault.Action != null) { faultContractAttr.Arguments.Add(new CodeAttributeArgument("Action", new CodePrimitiveExpression(fault.Action))); } if (fault.HasProtectionLevel) { faultContractAttr.Arguments.Add(new CodeAttributeArgument("ProtectionLevel", new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(ProtectionLevel)), fault.ProtectionLevel.ToString()))); } // override name with encoded value specified in wsdl; this only works beacuse // our Encoding algorithm will leave alredy encoded names untouched if (!XmlName.IsNullOrEmpty(fault.ElementName)) { faultContractAttr.Arguments.Add(new CodeAttributeArgument("Name", new CodePrimitiveExpression(fault.ElementName.EncodedName))); } if (fault.Namespace != context.Contract.Contract.Namespace) { faultContractAttr.Arguments.Add(new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(fault.Namespace))); } return(faultContractAttr); }
// 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); }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { DataContractSerializerOperationBehavior behavior = context.Operation.Behaviors.Find <DataContractSerializerOperationBehavior>(); DataContractFormatAttribute attribute = (behavior == null) ? new DataContractFormatAttribute() : behavior.DataContractFormatAttribute; OperationFormatStyle style = attribute.Style; this.operationGenerator.GenerateOperation(context, ref style, false, new WrappedBodyTypeGenerator(this, context), this.knownTypes); attribute.Style = style; if (attribute.Style != TypeLoader.DefaultDataContractFormatAttribute.Style) { context.SyncMethod.CustomAttributes.Add(System.ServiceModel.Description.OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, attribute)); } if (this.knownTypes != null) { Dictionary <CodeTypeReference, object> operationKnownTypes = new Dictionary <CodeTypeReference, object>(new CodeTypeReferenceComparer()); foreach (MessageDescription description in context.Operation.Messages) { foreach (MessagePartDescription description2 in description.Body.Parts) { this.AddKnownTypesForPart(context, description2, operationKnownTypes); } foreach (MessageHeaderDescription description3 in description.Headers) { this.AddKnownTypesForPart(context, description3, operationKnownTypes); } if (OperationFormatter.IsValidReturnValue(description.Body.ReturnValue)) { this.AddKnownTypesForPart(context, description.Body.ReturnValue, operationKnownTypes); } } } UpdateTargetCompileUnit(context, this.codeCompileUnit); }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { System.ServiceModel.TransactionFlowAttribute attr = context.Operation.Behaviors.Find <System.ServiceModel.TransactionFlowAttribute>(); if (attr != null && attr.Transactions != TransactionFlowOption.NotAllowed) { CodeMemberMethod methodDecl = context.SyncMethod ?? context.BeginMethod; methodDecl.CustomAttributes.Add(CreateAttrDecl(context, attr)); } }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { TransactionFlowAttribute attr = context.Operation.Behaviors.Find <TransactionFlowAttribute>(); if ((attr != null) && (attr.Transactions != TransactionFlowOption.NotAllowed)) { CodeMemberMethod method = context.SyncMethod ?? context.BeginMethod; method.CustomAttributes.Add(CreateAttrDecl(context, attr)); } }
private void AddOperationContractAttributes(OperationContractGenerationContext context) { if (context.SyncMethod != null) { context.SyncMethod.CustomAttributes.Add(this.CreateOperationContractAttributeDeclaration(context.Operation, false)); } if (context.BeginMethod != null) { context.BeginMethod.CustomAttributes.Add(this.CreateOperationContractAttributeDeclaration(context.Operation, true)); } }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { CodeMemberMethod methodDecl = context.SyncMethod ?? context.BeginMethod; foreach (FaultDescription fault in context.Operation.Faults) { CodeAttributeDeclaration faultAttr = CreateAttrDecl(context, fault); if (faultAttr != null) { methodDecl.CustomAttributes.Add(faultAttr); } } }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { CodeMemberMethod method = context.SyncMethod ?? context.BeginMethod; foreach (FaultDescription description in context.Operation.Faults) { CodeAttributeDeclaration declaration = CreateAttrDecl(context, description); if (declaration != null) { method.CustomAttributes.Add(declaration); } } }
internal static void UpdateTargetCompileUnit(OperationContractGenerationContext context, CodeCompileUnit codeCompileUnit) { CodeCompileUnit targetCompileUnit = context.ServiceContractGenerator.TargetCompileUnit; if (!object.ReferenceEquals(targetCompileUnit, codeCompileUnit)) { foreach (CodeNamespace namespace2 in codeCompileUnit.Namespaces) { if (!targetCompileUnit.Namespaces.Contains(namespace2)) { targetCompileUnit.Namespaces.Add(namespace2); } } foreach (string str in codeCompileUnit.ReferencedAssemblies) { if (!targetCompileUnit.ReferencedAssemblies.Contains(str)) { targetCompileUnit.ReferencedAssemblies.Add(str); } } foreach (CodeAttributeDeclaration declaration in codeCompileUnit.AssemblyCustomAttributes) { if (!targetCompileUnit.AssemblyCustomAttributes.Contains(declaration)) { targetCompileUnit.AssemblyCustomAttributes.Add(declaration); } } foreach (CodeDirective directive in codeCompileUnit.StartDirectives) { if (!targetCompileUnit.StartDirectives.Contains(directive)) { targetCompileUnit.StartDirectives.Add(directive); } } foreach (CodeDirective directive2 in codeCompileUnit.EndDirectives) { if (!targetCompileUnit.EndDirectives.Contains(directive2)) { targetCompileUnit.EndDirectives.Add(directive2); } } foreach (DictionaryEntry entry in codeCompileUnit.UserData) { targetCompileUnit.UserData[entry.Key] = entry.Value; } } }
internal static void UpdateTargetCompileUnit(OperationContractGenerationContext context, CodeCompileUnit codeCompileUnit) { CodeCompileUnit targetCompileUnit = context.ServiceContractGenerator.TargetCompileUnit; if (!Object.ReferenceEquals(targetCompileUnit, codeCompileUnit)) { foreach (CodeNamespace codeNamespace in codeCompileUnit.Namespaces) { if (!targetCompileUnit.Namespaces.Contains(codeNamespace)) { targetCompileUnit.Namespaces.Add(codeNamespace); } } foreach (string referencedAssembly in codeCompileUnit.ReferencedAssemblies) { if (!targetCompileUnit.ReferencedAssemblies.Contains(referencedAssembly)) { targetCompileUnit.ReferencedAssemblies.Add(referencedAssembly); } } foreach (CodeAttributeDeclaration assemblyCustomAttribute in codeCompileUnit.AssemblyCustomAttributes) { if (!targetCompileUnit.AssemblyCustomAttributes.Contains(assemblyCustomAttribute)) { targetCompileUnit.AssemblyCustomAttributes.Add(assemblyCustomAttribute); } } foreach (CodeDirective startDirective in codeCompileUnit.StartDirectives) { if (!targetCompileUnit.StartDirectives.Contains(startDirective)) { targetCompileUnit.StartDirectives.Add(startDirective); } } foreach (CodeDirective endDirective in codeCompileUnit.EndDirectives) { if (!targetCompileUnit.EndDirectives.Contains(endDirective)) { targetCompileUnit.EndDirectives.Add(endDirective); } } foreach (DictionaryEntry userData in codeCompileUnit.UserData) { targetCompileUnit.UserData[userData.Key] = userData.Value; } } }
// 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); }
void AddKnownTypesForPart(OperationContractGenerationContext context, MessagePartDescription part, Dictionary <CodeTypeReference, object> operationKnownTypes) { ICollection <CodeTypeReference> knownTypesForPart; if (knownTypes.TryGetValue(part, out knownTypesForPart)) { foreach (CodeTypeReference knownTypeReference in knownTypesForPart) { object value; if (!operationKnownTypes.TryGetValue(knownTypeReference, out value)) { operationKnownTypes.Add(knownTypeReference, null); CodeAttributeDeclaration knownTypeAttribute = new CodeAttributeDeclaration(typeof(ServiceKnownTypeAttribute).FullName); knownTypeAttribute.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(knownTypeReference))); context.SyncMethod.CustomAttributes.Add(knownTypeAttribute); } } } }
private void AddKnownTypesForPart(OperationContractGenerationContext context, MessagePartDescription part, Dictionary <CodeTypeReference, object> operationKnownTypes) { ICollection <CodeTypeReference> is2; if (this.knownTypes.TryGetValue(part, out is2)) { foreach (CodeTypeReference reference in is2) { object obj2; if (!operationKnownTypes.TryGetValue(reference, out obj2)) { operationKnownTypes.Add(reference, null); CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(ServiceKnownTypeAttribute).FullName); declaration.Arguments.Add(new CodeAttributeArgument(new CodeTypeOfExpression(reference))); context.SyncMethod.CustomAttributes.Add(declaration); } } } }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { if (context == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context"); } if ((this.partInfoTable != null) && (this.partInfoTable.Count > 0)) { Dictionary <XmlMembersMapping, XmlMembersMapping> alreadyExported = new Dictionary <XmlMembersMapping, XmlMembersMapping>(); foreach (MessageDescription description in context.Operation.Messages) { foreach (MessageHeaderDescription description2 in description.Headers) { this.GeneratePartType(alreadyExported, description2); } if (OperationFormatter.IsValidReturnValue(description.Body.ReturnValue)) { this.GeneratePartType(alreadyExported, description.Body.ReturnValue); } foreach (MessagePartDescription description3 in description.Body.Parts) { this.GeneratePartType(alreadyExported, description3); } } } XmlSerializerOperationBehavior behavior = context.Operation.Behaviors.Find <XmlSerializerOperationBehavior>(); if (behavior != null) { XmlSerializerFormatAttribute attribute = (behavior == null) ? new XmlSerializerFormatAttribute() : behavior.XmlSerializerFormatAttribute; OperationFormatStyle style = attribute.Style; this.operationGenerator.GenerateOperation(context, ref style, attribute.IsEncoded, new WrappedBodyTypeGenerator(context), new Dictionary <MessagePartDescription, ICollection <CodeTypeReference> >()); context.ServiceContractGenerator.AddReferencedAssembly(typeof(XmlTypeAttribute).Assembly); attribute.Style = style; context.SyncMethod.CustomAttributes.Add(System.ServiceModel.Description.OperationGenerator.GenerateAttributeDeclaration(context.Contract.ServiceContractGenerator, attribute)); this.AddKnownTypes(context.SyncMethod.CustomAttributes, attribute.IsEncoded ? this.SoapExporter.IncludeMetadata : this.XmlExporter.IncludeMetadata); DataContractSerializerOperationGenerator.UpdateTargetCompileUnit(context, this.options.CodeCompileUnit); } }
public void GenerateOperation(OperationContractGenerationContext context) { throw new NotImplementedException(); }
/// <summary>Generates the TAP implementation for a single operation.</summary> /// <param name="context">Information about the operation.</param> public void GenerateOperation(OperationContractGenerationContext context) { if (context.IsAsync) { string contractName = context.Contract.ContractType.Name; string clientTypeName = TaskAsyncWsdlImportExtension.DeriveClientTypeName(contractName); // Get the class to contain the new method. CodeTypeDeclaration clientClass = TaskAsyncWsdlImportExtension.FindClientType(clientTypeName, context.ServiceContractGenerator.TargetCompileUnit.Namespaces); // First, set up the new method, with attributes, name, parameters, and return type. CodeMemberMethod newTaskBasedMethod = new CodeMemberMethod() { Attributes = MemberAttributes.Final | MemberAttributes.Public, Name = context.SyncMethod.Name + "Async" }; newTaskBasedMethod.Parameters.AddRange(context.SyncMethod.Parameters); bool returnsVoid = context.SyncMethod.ReturnType == null || context.SyncMethod.ReturnType.BaseType == "System.Void"; if (returnsVoid) { newTaskBasedMethod.ReturnType = new CodeTypeReference(typeof(Task)); } else { var returnType = new CodeTypeReference(typeof(Task<>)); returnType.TypeArguments.Add(context.EndMethod.ReturnType); newTaskBasedMethod.ReturnType = returnType; } // Second, create the Task.Factory.FromAsync or Task<TResult>.Factory.FromAsync invoker. CodePropertyReferenceExpression getTaskFactory = new CodePropertyReferenceExpression(); getTaskFactory.PropertyName = "Factory"; CodeMethodInvokeExpression invokeFromAsync = new CodeMethodInvokeExpression(); if (returnsVoid) { getTaskFactory.TargetObject = new CodeTypeReferenceExpression(typeof(Task)); } else { var taskOfReturnType = new CodeTypeReference(typeof(Task<>)); taskOfReturnType.TypeArguments.Add(context.SyncMethod.ReturnType); getTaskFactory.TargetObject = new CodeTypeReferenceExpression(taskOfReturnType); } invokeFromAsync.Method = new CodeMethodReferenceExpression(getTaskFactory, "FromAsync"); newTaskBasedMethod.Statements.Add(new CodeMethodReturnStatement(invokeFromAsync)); // Create the end delegate for the FromAsync call. var endDelegate = new CodeDelegateCreateExpression(); endDelegate.MethodName = context.EndMethod.Name; endDelegate.TargetObject = new CodeCastExpression(contractName, new CodeThisReferenceExpression()); if (returnsVoid) { endDelegate.DelegateType = new CodeTypeReference(typeof(Action<IAsyncResult>)); } else { endDelegate.DelegateType = new CodeTypeReference(typeof(Func<,>)); endDelegate.DelegateType.TypeArguments.Add(typeof(IAsyncResult)); endDelegate.DelegateType.TypeArguments.Add(context.SyncMethod.ReturnType); } // If there are <= 3 parameters to the APM's Begin method, use a delegate-based // overload, as that's what TPL provides overloads for built-in. If not, // use an overload that accepts an IAsyncResult as the first parameter. if (context.SyncMethod.Parameters.Count <= 3) { // Create the begin delegate for the FromAsync call // FromAsync(beginDelegate, endDelegate, null); var beginDelegate = new CodeDelegateCreateExpression(); beginDelegate.MethodName = context.BeginMethod.Name; beginDelegate.TargetObject = new CodeCastExpression(contractName, new CodeThisReferenceExpression()); switch (context.SyncMethod.Parameters.Count) { case 0: beginDelegate.DelegateType = new CodeTypeReference(typeof(Func<,,>)); break; case 1: beginDelegate.DelegateType = new CodeTypeReference(typeof(Func<,,,>)); break; case 2: beginDelegate.DelegateType = new CodeTypeReference(typeof(Func<,,,,>)); break; case 3: beginDelegate.DelegateType = new CodeTypeReference(typeof(Func<,,,,,>)); break; } beginDelegate.DelegateType.TypeArguments.AddRange(context.SyncMethod.Parameters.Cast<CodeParameterDeclarationExpression>().Select(p => p.Type).ToArray()); beginDelegate.DelegateType.TypeArguments.Add(typeof(AsyncCallback)); beginDelegate.DelegateType.TypeArguments.Add(typeof(Object)); beginDelegate.DelegateType.TypeArguments.Add(typeof(IAsyncResult)); invokeFromAsync.Parameters.Add(beginDelegate); invokeFromAsync.Parameters.Add(endDelegate); invokeFromAsync.Parameters.AddRange((from parameter in context.SyncMethod.Parameters.Cast<CodeParameterDeclarationExpression>() select new CodeVariableReferenceExpression(parameter.Name)).ToArray()); invokeFromAsync.Parameters.Add(new CodePrimitiveExpression(null)); } else // > 3 parameters, so use the IAsyncResult overload { // FromAsync(BeginMethod(inputParams, ..., asyncCallback, state), endDelegate) var invokeBeginExpression = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(new CodeCastExpression(contractName, new CodeThisReferenceExpression()), context.BeginMethod.Name)); invokeBeginExpression.Parameters.AddRange((from parameter in context.SyncMethod.Parameters.Cast<CodeParameterDeclarationExpression>() select new CodeVariableReferenceExpression(parameter.Name)).ToArray()); invokeBeginExpression.Parameters.Add(new CodePrimitiveExpression(null)); // AsyncCallback invokeBeginExpression.Parameters.Add(new CodePrimitiveExpression(null)); // state invokeFromAsync.Parameters.Add(invokeBeginExpression); invokeFromAsync.Parameters.Add(endDelegate); } // Finally, add the new method to the class clientClass.Members.Add(newTaskBasedMethod); } }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { XmlCommentsImporter.AddXmlComment(context.SyncMethod, documentation, XmlCommentsImporter.options); }
public void GenerateOperation(OperationContractGenerationContext context) { context.SyncMethod.Comments.AddRange(FormatComments(text)); Debug.WriteLine("In generate operation."); }
public WrappedBodyTypeGenerator(DataContractSerializerOperationGenerator dataContractSerializerOperationGenerator, OperationContractGenerationContext context) { this.context = context; this.dataContractSerializerOperationGenerator = dataContractSerializerOperationGenerator; }
// calls a specific set of operation-level extensions static void CallOperationExtensions(IEnumerable <IOperationContractGenerationExtension> extensions, OperationContractGenerationContext context) { foreach (IOperationContractGenerationExtension extension in extensions) { extension.GenerateOperation(context); } }
void Visit(OperationDescription operationDescription) { bool isCallback = operationDescription.IsServerInitiated(); CodeTypeDeclaration declaringType = isCallback ? context.DuplexCallbackType : context.ContractType; UniqueCodeIdentifierScope memberScope = isCallback ? this.callbackMemberScope : this.contractMemberScope; Fx.Assert(declaringType != null, "missing callback type"); string syncMethodName = memberScope.AddUnique(operationDescription.CodeName, Strings.DefaultOperationName); CodeMemberMethod syncMethod = new CodeMemberMethod(); syncMethod.Name = syncMethodName; declaringType.Members.Add(syncMethod); OperationContractGenerationContext operationContext; CodeMemberMethod beginMethod = null; CodeMemberMethod endMethod = null; if (asyncMethods) { beginMethod = new CodeMemberMethod(); beginMethod.Name = ServiceReflector.BeginMethodNamePrefix + syncMethodName; beginMethod.Parameters.Add(new CodeParameterDeclarationExpression(context.ServiceContractGenerator.GetCodeTypeReference(typeof(AsyncCallback)), Strings.AsyncCallbackArgName)); beginMethod.Parameters.Add(new CodeParameterDeclarationExpression(context.ServiceContractGenerator.GetCodeTypeReference(typeof(object)), Strings.AsyncStateArgName)); beginMethod.ReturnType = context.ServiceContractGenerator.GetCodeTypeReference(typeof(IAsyncResult)); declaringType.Members.Add(beginMethod); endMethod = new CodeMemberMethod(); endMethod.Name = ServiceReflector.EndMethodNamePrefix + syncMethodName; endMethod.Parameters.Add(new CodeParameterDeclarationExpression(context.ServiceContractGenerator.GetCodeTypeReference(typeof(IAsyncResult)), Strings.AsyncResultArgName)); declaringType.Members.Add(endMethod); operationContext = new OperationContractGenerationContext(parent, context, operationDescription, declaringType, syncMethod, beginMethod, endMethod); } else { operationContext = new OperationContractGenerationContext(parent, context, operationDescription, declaringType, syncMethod); } if (taskMethod) { if (isCallback) { if (beginMethod == null) { operationContext = new OperationContractGenerationContext(parent, context, operationDescription, declaringType, syncMethod); } else { operationContext = new OperationContractGenerationContext(parent, context, operationDescription, declaringType, syncMethod, beginMethod, endMethod); } } else { CodeMemberMethod taskBasedAsyncMethod = new CodeMemberMethod { Name = syncMethodName + ServiceReflector.AsyncMethodNameSuffix }; declaringType.Members.Add(taskBasedAsyncMethod); if (beginMethod == null) { operationContext = new OperationContractGenerationContext(parent, context, operationDescription, declaringType, syncMethod, taskBasedAsyncMethod); } else { operationContext = new OperationContractGenerationContext(parent, context, operationDescription, declaringType, syncMethod, beginMethod, endMethod, taskBasedAsyncMethod); } } } operationContext.DeclaringTypeReference = operationDescription.IsServerInitiated() ? context.DuplexCallbackTypeReference : context.ContractTypeReference; context.Operations.Add(operationContext); AddOperationContractAttributes(operationContext); }
// IOperationContractGenerationContext public void GenerateOperation (OperationContractGenerationContext context) { this.context = context; ml_context.Operations.Add (this); }
public WrappedBodyTypeGenerator(OperationContractGenerationContext context) { this.context = context; }
// 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); }