// protected override CodeMemberMethod GenerateMethod() { HttpOperationBinding httpOperationBinding = (HttpOperationBinding)OperationBinding.Extensions.Find(typeof(HttpOperationBinding)); if (httpOperationBinding == null) { throw OperationBindingSyntaxException(Res.GetString(Res.MissingHttpOperationElement0)); } HttpMethodInfo method = new HttpMethodInfo(); if (hasInputPayload) { method.MimeParameters = ImportMimeParameters(); if (method.MimeParameters == null) { UnsupportedOperationWarning(Res.GetString(Res.NoInputMIMEFormatsWereRecognized0)); return(null); } } else { method.UrlParameters = ImportUrlParameters(); if (method.UrlParameters == null) { UnsupportedOperationWarning(Res.GetString(Res.NoInputHTTPFormatsWereRecognized0)); return(null); } } method.MimeReturn = ImportMimeReturn(); if (method.MimeReturn == null) { UnsupportedOperationWarning(Res.GetString(Res.NoOutputMIMEFormatsWereRecognized0)); return(null); } method.Name = MethodNames.AddUnique(MethodName, method); method.Href = httpOperationBinding.Location; return(GenerateMethod(method)); }
CodeMemberMethod GenerateMethod(HttpMethodInfo method) { MimeParameterCollection parameters = method.MimeParameters != null ? method.MimeParameters : method.UrlParameters; string[] parameterTypeNames = new string[parameters.Count]; string[] parameterNames = new string[parameters.Count]; for (int i = 0; i < parameters.Count; i++) { MimeParameter param = parameters[i]; parameterNames[i] = param.Name; parameterTypeNames[i] = param.TypeName; } CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection(); CodeExpression[] formatterTypes = new CodeExpression[2]; if (method.MimeReturn.ReaderType == null) { formatterTypes[0] = new CodeTypeOfExpression(typeof(NopReturnReader).FullName); } else { formatterTypes[0] = new CodeTypeOfExpression(method.MimeReturn.ReaderType.FullName); } if (method.MimeParameters != null) { formatterTypes[1] = new CodeTypeOfExpression(method.MimeParameters.WriterType.FullName); } else { formatterTypes[1] = new CodeTypeOfExpression(typeof(UrlParameterWriter).FullName); } WebCodeGenerator.AddCustomAttribute(metadata, typeof(HttpMethodAttribute), formatterTypes, new string[0], new CodeExpression[0]); CodeMemberMethod mainCodeMethod = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, method.Name, new CodeFlags[parameterTypeNames.Length], parameterTypeNames, parameterNames, method.MimeReturn.TypeName, metadata, CodeFlags.IsPublic | (Style == ServiceDescriptionImportStyle.Client ? 0 : CodeFlags.IsAbstract)); AppendMetadata(method.MimeReturn.Attributes, mainCodeMethod.ReturnTypeCustomAttributes); mainCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true)); for (int i = 0; i < parameters.Count; i++) { AppendMetadata(parameters[i].Attributes, mainCodeMethod.Parameters[i].CustomAttributes); } if (Style == ServiceDescriptionImportStyle.Client) { bool oldAsync = (ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateOldAsync) != 0; bool newAsync = (ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateNewAsync) != 0 && ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareEvents) && ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareDelegates); CodeExpression[] invokeParams = new CodeExpression[3]; CreateInvokeParams(invokeParams, method, parameterNames); CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Invoke", invokeParams); if (method.MimeReturn.ReaderType != null) { mainCodeMethod.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(method.MimeReturn.TypeName, invoke))); } else { mainCodeMethod.Statements.Add(new CodeExpressionStatement(invoke)); } metadata = new CodeAttributeDeclarationCollection(); string[] asyncParameterTypeNames = new string[parameterTypeNames.Length + 2]; parameterTypeNames.CopyTo(asyncParameterTypeNames, 0); asyncParameterTypeNames[parameterTypeNames.Length] = typeof(AsyncCallback).FullName; asyncParameterTypeNames[parameterTypeNames.Length + 1] = typeof(object).FullName; string[] asyncParameterNames = new string[parameterNames.Length + 2]; parameterNames.CopyTo(asyncParameterNames, 0); asyncParameterNames[parameterNames.Length] = "callback"; asyncParameterNames[parameterNames.Length + 1] = "asyncState"; if (oldAsync) { CodeMemberMethod beginCodeMethod = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, "Begin" + method.Name, new CodeFlags[asyncParameterTypeNames.Length], asyncParameterTypeNames, asyncParameterNames, typeof(IAsyncResult).FullName, metadata, CodeFlags.IsPublic); beginCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true)); invokeParams = new CodeExpression[5]; CreateInvokeParams(invokeParams, method, parameterNames); invokeParams[3] = new CodeArgumentReferenceExpression("callback"); invokeParams[4] = new CodeArgumentReferenceExpression("asyncState"); invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "BeginInvoke", invokeParams); beginCodeMethod.Statements.Add(new CodeMethodReturnStatement(invoke)); CodeMemberMethod endCodeMethod = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, "End" + method.Name, new CodeFlags[1], new string[] { typeof(IAsyncResult).FullName }, new string[] { "asyncResult" }, method.MimeReturn.TypeName, metadata, CodeFlags.IsPublic); endCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true)); CodeExpression expr = new CodeArgumentReferenceExpression("asyncResult"); invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "EndInvoke", new CodeExpression[] { expr }); if (method.MimeReturn.ReaderType != null) { endCodeMethod.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(method.MimeReturn.TypeName, invoke))); } else { endCodeMethod.Statements.Add(new CodeExpressionStatement(invoke)); } } if (newAsync) { metadata = new CodeAttributeDeclarationCollection(); string uniqueMethodName = method.Name; string methodKey = MethodSignature(uniqueMethodName, method.MimeReturn.TypeName, new CodeFlags[parameterTypeNames.Length], parameterTypeNames); DelegateInfo delegateInfo = (DelegateInfo)ExportContext[methodKey]; if (delegateInfo == null) { string handlerType = ClassNames.AddUnique(uniqueMethodName + "CompletedEventHandler", uniqueMethodName); string handlerArgs = ClassNames.AddUnique(uniqueMethodName + "CompletedEventArgs", uniqueMethodName); delegateInfo = new DelegateInfo(handlerType, handlerArgs); } string handlerName = MethodNames.AddUnique(uniqueMethodName + "Completed", uniqueMethodName); string asyncName = MethodNames.AddUnique(uniqueMethodName + "Async", uniqueMethodName); string callbackMember = MethodNames.AddUnique(uniqueMethodName + "OperationCompleted", uniqueMethodName); string callbackName = MethodNames.AddUnique("On" + uniqueMethodName + "OperationCompleted", uniqueMethodName); // public event xxxCompletedEventHandler xxxCompleted; WebCodeGenerator.AddEvent(this.CodeTypeDeclaration.Members, delegateInfo.handlerType, handlerName); // private SendOrPostCallback xxxOperationCompleted; WebCodeGenerator.AddCallbackDeclaration(this.CodeTypeDeclaration.Members, callbackMember); // create the pair of xxxAsync methods string userState = UniqueName("userState", parameterNames); CodeMemberMethod asyncCodeMethod = WebCodeGenerator.AddAsyncMethod(this.CodeTypeDeclaration, asyncName, parameterTypeNames, parameterNames, callbackMember, callbackName, userState); // Generate InvokeAsync call invokeParams = new CodeExpression[5]; CreateInvokeParams(invokeParams, method, parameterNames); invokeParams[3] = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), callbackMember); invokeParams[4] = new CodeArgumentReferenceExpression(userState); invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "InvokeAsync", invokeParams); asyncCodeMethod.Statements.Add(invoke); // private void On_xxx_OperationCompleted(object arg) {..} bool methodHasReturn = method.MimeReturn.ReaderType != null; WebCodeGenerator.AddCallbackImplementation(this.CodeTypeDeclaration, callbackName, handlerName, delegateInfo.handlerArgs, methodHasReturn); if (ExportContext[methodKey] == null) { // public delegate void xxxCompletedEventHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs args); WebCodeGenerator.AddDelegate(ExtraCodeClasses, delegateInfo.handlerType, methodHasReturn ? delegateInfo.handlerArgs : typeof(AsyncCompletedEventArgs).FullName); if (methodHasReturn) { ExtraCodeClasses.Add(WebCodeGenerator.CreateArgsClass(delegateInfo.handlerArgs, new string[] { method.MimeReturn.TypeName }, new string[] { "Result" }, ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes))); } ExportContext[methodKey] = delegateInfo; } } } return(mainCodeMethod); }