public void GenerateOperation(OperationContractGenerationContext context) { var operationContractAttribute = context.SyncMethod.CustomAttributes.SingleOrDefault <CodeAttributeDeclaration>(p => p.Name == typeof(OperationContractAttribute).FullName); if (operationContractAttribute == null) { return; } var replyActionArgument = operationContractAttribute.Arguments.SingleOrDefault <CodeAttributeArgument>(p => p.Name == "ReplyAction"); if (replyActionArgument == null) { return; } if (!(replyActionArgument.Value is CodePrimitiveExpression expression)) { return; } if (!(expression.Value is string expressionText)) { return; } if (expressionText == "*") { operationContractAttribute.Arguments.Remove(replyActionArgument); } }
/// <summary> /// This is where we actually write the attribute out into the generated WSDL code /// </summary> /// <param name="context"></param> public void GenerateOperation(OperationContractGenerationContext context) { context.SyncMethod.Comments.Add(new CodeCommentStatement("Set CyclicReferencesAware Attribute")); CodeAttributeArgument codeAttr = new CodeAttributeArgument(new CodePrimitiveExpression(true)); context.SyncMethod.CustomAttributes.Add(new CodeAttributeDeclaration("CyclicReferencesAware", new CodeAttributeArgument[] { codeAttr })); }
public void GenerateOperation(OperationContractGenerationContext context) { var portType = FindPortType(_wsdlDocuments, context.Contract.Contract.Name, context.Contract.Contract.Namespace); if (portType == null) { throw new Exception($"Port type '{context.Contract.Contract.Namespace}:{context.Contract.Contract.Name}' not found."); } var operation = FindOperationByName(portType.Operations, context.Operation.Name); if (operation == null) { throw new Exception($"Operation '{context.Operation.Name} not found in port type '{portType.ServiceDescription.TargetNamespace}:{portType.ServiceDescription.Name}'."); } foreach (var message in context.Operation.Messages) { var wrapperElement = FindSchemaElementByQualifiedName(portType.ServiceDescription.Types.Schemas, new XmlQualifiedName(message.Body.WrapperName, message.Body.WrapperNamespace)); if (wrapperElement == null) { throw new Exception($"Schema element '{message.Body.WrapperNamespace}:{message.Body.WrapperName}' not found."); } var parameterRegister = CreateParameterRegister(wrapperElement); foreach (var part in message.Body.Parts) { if (!parameterRegister.TryGetValue(part.Name, out var parameterElement)) { throw new Exception(); } if (!_xmlTypeMapping.TryGetValue(parameterElement.ElementSchemaType.TypeCode, out var typeMapping)) { continue; } var methodParameter = FindMethodParameterByName(context.SyncMethod, part.Name); if (methodParameter == null) { throw new Exception(); } if (parameterElement.MinOccurs == 0 && (typeMapping.IsStruct || typeMapping.IsEnum)) { methodParameter.Type = typeMapping.CodeTypeReference.ToNullable(); } else { methodParameter.Type = typeMapping.CodeTypeReference; } } } }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { foreach (KeyValuePair <MessageHeaderDescription, SoapHeaderDirection> pair in headers) { MessageHeaderDescription header = pair.Key; string headerTypeName = (string)ReflectionUtils.GetValue(header, "BaseType"); CodeAttributeArgument arg1 = new CodeAttributeArgument(new CodePrimitiveExpression(header.Name)); CodeAttributeArgument arg2 = new CodeAttributeArgument(new CodeTypeOfExpression(headerTypeName)); CodeAttributeArgument arg3 = new CodeAttributeArgument("Direction", new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(SoapHeaderDirection)), pair.Value.ToString())); CodeAttributeDeclaration attrib = new CodeAttributeDeclaration(new CodeTypeReference(typeof(SoapHeaderAttribute)), arg1, arg2, arg3); context.SyncMethod.CustomAttributes.Add(attrib); } }
public void GenerateOperation(OperationContractGenerationContext context) { foreach (CodeParameterDeclarationExpression parameter in context.SyncMethod.Parameters) { var parameterName = parameter.Name; if (parameterName.IsPascalCase()) { continue; } parameter.Name = parameterName.ToPascalCase(); var messageParameterAttribute = new CodeAttributeDeclaration(new CodeTypeReference("System.ServiceModel.MessageParameterAttribute"), new CodeAttributeArgument("Name", new CodeSnippetExpression("\"" + parameterName + "\""))); parameter.CustomAttributes.Add(messageParameterAttribute); } }
public void GenerateOperation(OperationContractGenerationContext context) { context.SyncMethod.Comments.AddRange(FormatComments(text)); Debug.WriteLine("In generate operation."); }
void IOperationContractGenerationExtension.GenerateOperation(OperationContractGenerationContext context) { XmlCommentsImporter.AddXmlComment(context.SyncMethod, documentation, XmlCommentsImporter.options); }
/// <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); } }
// IOperationContractGenerationContext public void GenerateOperation(OperationContractGenerationContext context) { this.context = context; ml_context.Operations.Add(this); }
// </snippet12> #endregion #region IOperationContractGenerationExtension Members // <snippet14> public void GenerateOperation(OperationContractGenerationContext context) { context.SyncMethod.Comments.AddRange(Formatter.FormatComments(commentText)); Console.WriteLine("In generate operation."); }
private static void CallOperationExtensions(IEnumerableIOperationContractGenerationExtension extensions, OperationContractGenerationContext context) { foreach (IOperationContractGenerationExtension extension in extensions) { extension.GenerateOperation(context); } }
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) { TransactionFlowAttribute attr = context.Operation.Behaviors.FindTransactionFlowAttribute(); if ((attr != null) && (attr.Transactions != TransactionFlowOption.NotAllowed)) { CodeMemberMethod method = context.SyncMethod context.BeginMethod; method.CustomAttributes.Add(CreateAttrDecl(context, attr)); } }
private static CodeAttributeDeclaration CreateAttrDecl(OperationContractGenerationContext context, TransactionFlowAttribute attr) { CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(context.Contract.ServiceContractGenerator.GetCodeTypeReference(typeof(TransactionFlowAttribute))); declaration.Arguments.Add(new CodeAttributeArgument(ServiceContractGenerator.GetEnumReferenceTransactionFlowOption(attr.Transactions))); return declaration; }
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); } } }
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; }