// private void On_xxx_OperationCompleted(object arg) {..} internal static void AddCallbackImplementation(CodeTypeDeclaration codeClass, string callbackName, string handlerName, string handlerArgs, bool methodHasOutParameters) { CodeMemberMethod asyncCompleted = WebCodeGenerator.AddMethod(codeClass, callbackName, new CodeFlags[1] { 0 }, new string[] { typeof(object).FullName }, new string[] { "arg" }, typeof(void).FullName, null, 0); CodeEventReferenceExpression member = new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName); CodeBinaryOperatorExpression checkIfNull = new CodeBinaryOperatorExpression(member, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)); CodeStatement[] trueStatements = new CodeStatement[2]; trueStatements[0] = new CodeVariableDeclarationStatement(typeof(InvokeCompletedEventArgs), "invokeArgs", new CodeCastExpression(typeof(InvokeCompletedEventArgs), new CodeArgumentReferenceExpression("arg"))); CodeVariableReferenceExpression invokeArgs = new CodeVariableReferenceExpression("invokeArgs"); CodeObjectCreateExpression create = new CodeObjectCreateExpression(); if (methodHasOutParameters) { create.CreateType = new CodeTypeReference(handlerArgs); create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "Results")); } else { create.CreateType = new CodeTypeReference(typeof(AsyncCompletedEventArgs)); } create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "Error")); create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "Cancelled")); create.Parameters.Add(new CodePropertyReferenceExpression(invokeArgs, "UserState")); trueStatements[1] = new CodeExpressionStatement(new CodeDelegateInvokeExpression(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName), new CodeExpression[] { new CodeThisReferenceExpression(), create })); asyncCompleted.Statements.Add(new CodeConditionStatement(checkIfNull, trueStatements, new CodeStatement[0])); }
internal static CodeMemberMethod AddAsyncMethod(CodeTypeDeclaration codeClass, string methodName, string[] parameterTypeNames, string[] parameterNames, string callbackMember, string callbackName, string userState) { CodeMemberMethod asyncCodeMethod = WebCodeGenerator.AddMethod(codeClass, methodName, new CodeFlags[parameterNames.Length], parameterTypeNames, parameterNames, typeof(void).FullName, null, CodeFlags.IsPublic); asyncCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true)); CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), methodName); for (int i = 0; i < parameterNames.Length; i++) { invoke.Parameters.Add(new CodeArgumentReferenceExpression(parameterNames[i])); } invoke.Parameters.Add(new CodePrimitiveExpression(null)); asyncCodeMethod.Statements.Add(invoke); asyncCodeMethod = WebCodeGenerator.AddMethod(codeClass, methodName, new CodeFlags[parameterNames.Length], parameterTypeNames, parameterNames, typeof(void).FullName, null, CodeFlags.IsPublic); asyncCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true)); asyncCodeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), userState)); CodeFieldReferenceExpression member = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), callbackMember); CodeBinaryOperatorExpression checkIfNull = new CodeBinaryOperatorExpression(member, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)); CodeDelegateCreateExpression createDelegate = new CodeDelegateCreateExpression(); createDelegate.DelegateType = new CodeTypeReference(typeof(SendOrPostCallback)); createDelegate.TargetObject = new CodeThisReferenceExpression(); createDelegate.MethodName = callbackName; CodeStatement[] trueStatements = new CodeStatement[] { new CodeAssignStatement(member, createDelegate) }; asyncCodeMethod.Statements.Add(new CodeConditionStatement(checkIfNull, trueStatements, new CodeStatement[0])); return(asyncCodeMethod); }
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); }
void GenerateClassForBinding() { try { if (bindingCount == 1 && service != null && Style != ServiceDescriptionImportStyle.ServerInterface) { // If there is only one binding, then use the name of the service className = XmlConvert.DecodeName(service.Name); } else { // If multiple bindings, then use the name of the binding className = binding.Name; if (Style == ServiceDescriptionImportStyle.ServerInterface) { // append "I" if we are generating interfaces className = "I" + CodeIdentifier.MakePascal(className); } } className = XmlConvert.DecodeName(className); className = ClassNames.AddUnique(CodeIdentifier.MakeValid(className), null); this.codeClass = BeginClass(); int methodCount = 0; for (int i = 0; i < portType.Operations.Count; i++) { MoveToOperation(portType.Operations[i]); if (!IsOperationFlowSupported(operation.Messages.Flow)) { // switch (operation.Messages.Flow) { case OperationFlow.SolicitResponse: UnsupportedOperationWarning(Res.GetString(Res.SolicitResponseIsNotSupported0)); continue; case OperationFlow.RequestResponse: UnsupportedOperationWarning(Res.GetString(Res.RequestResponseIsNotSupported0)); continue; case OperationFlow.OneWay: UnsupportedOperationWarning(Res.GetString(Res.OneWayIsNotSupported0)); continue; case OperationFlow.Notification: UnsupportedOperationWarning(Res.GetString(Res.NotificationIsNotSupported0)); continue; } } CodeMemberMethod method; try { method = GenerateMethod(); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw new InvalidOperationException(Res.GetString(Res.UnableToImportOperation1, operation.Name), e); } if (method != null) { AddExtensionWarningComments(codeClass.Comments, operationBinding.Extensions); if (operationBinding.Input != null) { AddExtensionWarningComments(codeClass.Comments, operationBinding.Input.Extensions); } if (operationBinding.Output != null) { AddExtensionWarningComments(codeClass.Comments, operationBinding.Output.Extensions); } methodCount++; } } bool newAsync = (ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateNewAsync) != 0 && ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareEvents) && ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareDelegates); if (newAsync && methodCount > 0 && Style == ServiceDescriptionImportStyle.Client) { CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection(); string cancelAsync = "CancelAsync"; string cancelMethodName = MethodNames.AddUnique(cancelAsync, cancelAsync); CodeMemberMethod asyncCancelMethod = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, cancelMethodName, new CodeFlags[1], new string[] { typeof(object).FullName }, new string[] { "userState" }, typeof(void).FullName, metadata, CodeFlags.IsPublic | (cancelAsync != cancelMethodName ? 0 : CodeFlags.IsNew)); asyncCancelMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true)); CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(new CodeBaseReferenceExpression(), cancelAsync); invoke.Parameters.Add(new CodeArgumentReferenceExpression("userState")); asyncCancelMethod.Statements.Add(invoke); } EndClass(); if (portType.Operations.Count == 0) { NoMethodsGeneratedWarning(); } AddExtensionWarningComments(codeClass.Comments, binding.Extensions); if (port != null) { AddExtensionWarningComments(codeClass.Comments, port.Extensions); } codeNamespace.Types.Add(codeClass); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw new InvalidOperationException(Res.GetString(Res.UnableToImportBindingFromNamespace2, binding.Name, binding.ServiceDescription.TargetNamespace), e); } }
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("<remarks/>", true)); for (int i = 0; i < parameters.Count; i++) { AppendMetadata(parameters[i].Attributes, mainCodeMethod.Parameters[i].CustomAttributes); } if (Style == ServiceDescriptionImportStyle.Client) { CodeExpression[] invokeParams = new CodeExpression[3]; CreateInvokeParams(invokeParams, method, parameterNames); CodeExpression invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Invoke", invokeParams); if (method.MimeReturn.ReaderType != null) { invoke = new CodeCastExpression(method.MimeReturn.TypeName, invoke); } if (method.MimeReturn.ReaderType != null) { mainCodeMethod.Statements.Add(new CodeMethodReturnStatement(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"; 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("<remarks/>", 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("<remarks/>", true)); CodeExpression expr = new CodeArgumentReferenceExpression("asyncResult"); invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "EndInvoke", new CodeExpression[] { expr }); if (method.MimeReturn.ReaderType != null) { invoke = new CodeCastExpression(method.MimeReturn.TypeName, invoke); } if (method.MimeReturn.ReaderType != null) { endCodeMethod.Statements.Add(new CodeMethodReturnStatement(invoke)); } else { endCodeMethod.Statements.Add(new CodeExpressionStatement(invoke)); } } return(mainCodeMethod); }
private 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 parameter = parameters[i]; parameterNames[i] = parameter.Name; parameterTypeNames[i] = parameter.TypeName; } CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection(); CodeExpression[] expressionArray = new CodeExpression[2]; if (method.MimeReturn.ReaderType == null) { expressionArray[0] = new CodeTypeOfExpression(typeof(NopReturnReader).FullName); } else { expressionArray[0] = new CodeTypeOfExpression(method.MimeReturn.ReaderType.FullName); } if (method.MimeParameters != null) { expressionArray[1] = new CodeTypeOfExpression(method.MimeParameters.WriterType.FullName); } else { expressionArray[1] = new CodeTypeOfExpression(typeof(UrlParameterWriter).FullName); } WebCodeGenerator.AddCustomAttribute(metadata, typeof(HttpMethodAttribute), expressionArray, new string[0], new CodeExpression[0]); CodeMemberMethod method2 = WebCodeGenerator.AddMethod(base.CodeTypeDeclaration, method.Name, new CodeFlags[parameterTypeNames.Length], parameterTypeNames, parameterNames, method.MimeReturn.TypeName, metadata, CodeFlags.IsPublic | ((base.Style == ServiceDescriptionImportStyle.Client) ? ((CodeFlags)0) : CodeFlags.IsAbstract)); AppendMetadata(method.MimeReturn.Attributes, method2.ReturnTypeCustomAttributes); method2.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true)); for (int j = 0; j < parameters.Count; j++) { AppendMetadata(parameters[j].Attributes, method2.Parameters[j].CustomAttributes); } if (base.Style == ServiceDescriptionImportStyle.Client) { bool flag = (base.ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateOldAsync) != CodeGenerationOptions.None; bool flag2 = (((base.ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateNewAsync) != CodeGenerationOptions.None) && base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareEvents)) && base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareDelegates); CodeExpression[] invokeParams = new CodeExpression[3]; this.CreateInvokeParams(invokeParams, method, parameterNames); CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Invoke", invokeParams); if (method.MimeReturn.ReaderType != null) { method2.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(method.MimeReturn.TypeName, expression))); } else { method2.Statements.Add(new CodeExpressionStatement(expression)); } metadata = new CodeAttributeDeclarationCollection(); string[] array = new string[parameterTypeNames.Length + 2]; parameterTypeNames.CopyTo(array, 0); array[parameterTypeNames.Length] = typeof(AsyncCallback).FullName; array[parameterTypeNames.Length + 1] = typeof(object).FullName; string[] strArray4 = new string[parameterNames.Length + 2]; parameterNames.CopyTo(strArray4, 0); strArray4[parameterNames.Length] = "callback"; strArray4[parameterNames.Length + 1] = "asyncState"; if (flag) { CodeMemberMethod method3 = WebCodeGenerator.AddMethod(base.CodeTypeDeclaration, "Begin" + method.Name, new CodeFlags[array.Length], array, strArray4, typeof(IAsyncResult).FullName, metadata, CodeFlags.IsPublic); method3.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true)); invokeParams = new CodeExpression[5]; this.CreateInvokeParams(invokeParams, method, parameterNames); invokeParams[3] = new CodeArgumentReferenceExpression("callback"); invokeParams[4] = new CodeArgumentReferenceExpression("asyncState"); expression = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "BeginInvoke", invokeParams); method3.Statements.Add(new CodeMethodReturnStatement(expression)); CodeMemberMethod method4 = WebCodeGenerator.AddMethod(base.CodeTypeDeclaration, "End" + method.Name, new CodeFlags[1], new string[] { typeof(IAsyncResult).FullName }, new string[] { "asyncResult" }, method.MimeReturn.TypeName, metadata, CodeFlags.IsPublic); method4.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true)); CodeExpression expression2 = new CodeArgumentReferenceExpression("asyncResult"); expression = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "EndInvoke", new CodeExpression[] { expression2 }); if (method.MimeReturn.ReaderType != null) { method4.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(method.MimeReturn.TypeName, expression))); } else { method4.Statements.Add(new CodeExpressionStatement(expression)); } } if (!flag2) { return(method2); } metadata = new CodeAttributeDeclarationCollection(); string name = method.Name; string str2 = ProtocolImporter.MethodSignature(name, method.MimeReturn.TypeName, new CodeFlags[parameterTypeNames.Length], parameterTypeNames); DelegateInfo info = (DelegateInfo)base.ExportContext[str2]; if (info == null) { string handlerType = base.ClassNames.AddUnique(name + "CompletedEventHandler", name); string handlerArgs = base.ClassNames.AddUnique(name + "CompletedEventArgs", name); info = new DelegateInfo(handlerType, handlerArgs); } string handlerName = base.MethodNames.AddUnique(name + "Completed", name); string methodName = base.MethodNames.AddUnique(name + "Async", name); string callbackMember = base.MethodNames.AddUnique(name + "OperationCompleted", name); string callbackName = base.MethodNames.AddUnique("On" + name + "OperationCompleted", name); WebCodeGenerator.AddEvent(base.CodeTypeDeclaration.Members, info.handlerType, handlerName); WebCodeGenerator.AddCallbackDeclaration(base.CodeTypeDeclaration.Members, callbackMember); string userState = ProtocolImporter.UniqueName("userState", parameterNames); CodeMemberMethod method5 = WebCodeGenerator.AddAsyncMethod(base.CodeTypeDeclaration, methodName, parameterTypeNames, parameterNames, callbackMember, callbackName, userState); invokeParams = new CodeExpression[5]; this.CreateInvokeParams(invokeParams, method, parameterNames); invokeParams[3] = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), callbackMember); invokeParams[4] = new CodeArgumentReferenceExpression(userState); expression = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "InvokeAsync", invokeParams); method5.Statements.Add(expression); bool methodHasOutParameters = method.MimeReturn.ReaderType != null; WebCodeGenerator.AddCallbackImplementation(base.CodeTypeDeclaration, callbackName, handlerName, info.handlerArgs, methodHasOutParameters); if (base.ExportContext[str2] != null) { return(method2); } WebCodeGenerator.AddDelegate(base.ExtraCodeClasses, info.handlerType, methodHasOutParameters ? info.handlerArgs : typeof(AsyncCompletedEventArgs).FullName); if (methodHasOutParameters) { base.ExtraCodeClasses.Add(WebCodeGenerator.CreateArgsClass(info.handlerArgs, new string[] { method.MimeReturn.TypeName }, new string[] { "Result" }, base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes))); } base.ExportContext[str2] = info; } return(method2); }
private void GenerateClassForBinding() { try { if (((this.bindingCount == 1) && (this.service != null)) && (this.Style != ServiceDescriptionImportStyle.ServerInterface)) { this.className = XmlConvert.DecodeName(this.service.Name); } else { this.className = this.binding.Name; if (this.Style == ServiceDescriptionImportStyle.ServerInterface) { this.className = "I" + CodeIdentifier.MakePascal(this.className); } } this.className = XmlConvert.DecodeName(this.className); this.className = this.ClassNames.AddUnique(CodeIdentifier.MakeValid(this.className), null); this.codeClass = this.BeginClass(); int num = 0; for (int i = 0; i < this.portType.Operations.Count; i++) { CodeMemberMethod method; this.MoveToOperation(this.portType.Operations[i]); if (!this.IsOperationFlowSupported(this.operation.Messages.Flow)) { switch (this.operation.Messages.Flow) { case OperationFlow.OneWay: { this.UnsupportedOperationWarning(System.Web.Services.Res.GetString("OneWayIsNotSupported0")); continue; } case OperationFlow.Notification: { this.UnsupportedOperationWarning(System.Web.Services.Res.GetString("NotificationIsNotSupported0")); continue; } case OperationFlow.RequestResponse: { this.UnsupportedOperationWarning(System.Web.Services.Res.GetString("RequestResponseIsNotSupported0")); continue; } case OperationFlow.SolicitResponse: { this.UnsupportedOperationWarning(System.Web.Services.Res.GetString("SolicitResponseIsNotSupported0")); continue; } } } try { method = this.GenerateMethod(); } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } throw new InvalidOperationException(System.Web.Services.Res.GetString("UnableToImportOperation1", new object[] { this.operation.Name }), exception); } if (method != null) { this.AddExtensionWarningComments(this.codeClass.Comments, this.operationBinding.Extensions); if (this.operationBinding.Input != null) { this.AddExtensionWarningComments(this.codeClass.Comments, this.operationBinding.Input.Extensions); } if (this.operationBinding.Output != null) { this.AddExtensionWarningComments(this.codeClass.Comments, this.operationBinding.Output.Extensions); } num++; } } if ((((((this.ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateNewAsync) != CodeGenerationOptions.None) && this.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareEvents)) && this.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareDelegates)) && (num > 0)) && (this.Style == ServiceDescriptionImportStyle.Client)) { CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection(); string identifier = "CancelAsync"; string methodName = this.MethodNames.AddUnique(identifier, identifier); CodeMemberMethod method2 = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, methodName, new CodeFlags[1], new string[] { typeof(object).FullName }, new string[] { "userState" }, typeof(void).FullName, metadata, CodeFlags.IsPublic | ((identifier != methodName) ? ((CodeFlags)0) : CodeFlags.IsNew)); method2.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true)); CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(new CodeBaseReferenceExpression(), identifier, new CodeExpression[0]); expression.Parameters.Add(new CodeArgumentReferenceExpression("userState")); method2.Statements.Add(expression); } this.EndClass(); if (this.portType.Operations.Count == 0) { this.NoMethodsGeneratedWarning(); } this.AddExtensionWarningComments(this.codeClass.Comments, this.binding.Extensions); if (this.port != null) { this.AddExtensionWarningComments(this.codeClass.Comments, this.port.Extensions); } this.codeNamespace.Types.Add(this.codeClass); } catch (Exception exception2) { if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException)) { throw; } throw new InvalidOperationException(System.Web.Services.Res.GetString("UnableToImportBindingFromNamespace2", new object[] { this.binding.Name, this.binding.ServiceDescription.TargetNamespace }), exception2); } }