protected override CodeTypeDeclaration BeginClass()
        {
            methodNames.Clear();
            CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();

            if (Style == ServiceDescriptionImportStyle.Client)
            {
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(DebuggerStepThroughAttribute), new CodeExpression[0]);
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(DesignerCategoryAttribute), new CodeExpression[] { new CodePrimitiveExpression("code") });
            }

            Type[] requiredTypes = new Type[] { typeof(SoapDocumentMethodAttribute), typeof(XmlAttributeAttribute), typeof(WebService), typeof(Object), typeof(DebuggerStepThroughAttribute), typeof(DesignerCategoryAttribute) };
            WebCodeGenerator.AddImports(this.CodeNamespace, WebCodeGenerator.GetNamespacesForTypes(requiredTypes));
            CodeTypeDeclaration codeClass = WebCodeGenerator.CreateClass(this.ClassName, BaseClass.FullName,
                                                                         new string[0], metadata, CodeFlags.IsPublic | (Style == ServiceDescriptionImportStyle.Client ? 0 : CodeFlags.IsAbstract));

            codeClass.Comments.Add(new CodeCommentStatement("<remarks/>", true));

            CodeConstructor ctor = WebCodeGenerator.AddConstructor(codeClass, new string[0], new string[0], null, CodeFlags.IsPublic);

            ctor.Comments.Add(new CodeCommentStatement("<remarks/>", true));

            HttpAddressBinding httpAddressBinding = Port == null ? null : (HttpAddressBinding)Port.Extensions.Find(typeof(HttpAddressBinding));
            string             url = (httpAddressBinding != null) ? httpAddressBinding.Location : null;
            ServiceDescription serviceDescription = Binding.ServiceDescription;

            ProtocolImporterUtil.GenerateConstructorStatements(ctor, url, serviceDescription.AppSettingUrlKey, serviceDescription.AppSettingBaseUrl);

            codeClasses.Add(codeClass);
            return(codeClass);
        }
        public override void ImportClass()
        {
            // grab this here so it gets marked "handled" for both client and server
            SoapAddressBinding soapAddress = ImportContext.Port == null ? null : (SoapAddressBinding)ImportContext.Port.Extensions.Find(typeof(SoapAddressBinding));

            if (ImportContext.Style == ServiceDescriptionImportStyle.Client)
            {
                ImportContext.CodeTypeDeclaration.BaseTypes.Add(typeof(SoapHttpClientProtocol).FullName);
                CodeConstructor ctor = WebCodeGenerator.AddConstructor(ImportContext.CodeTypeDeclaration, new string[0], new string[0], null, CodeFlags.IsPublic);
                ctor.Comments.Add(new CodeCommentStatement("<remarks/>", true));
                if (ImportContext is Soap12ProtocolImporter)
                {
                    // add version code
                    CodeTypeReferenceExpression     versionEnumTypeReference  = new CodeTypeReferenceExpression(typeof(SoapProtocolVersion));
                    CodeFieldReferenceExpression    versionEnumFieldReference = new CodeFieldReferenceExpression(versionEnumTypeReference, Enum.Format(typeof(SoapProtocolVersion), SoapProtocolVersion.Soap12, "G"));
                    CodePropertyReferenceExpression versionPropertyReference  = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "SoapVersion");
                    CodeAssignStatement             assignVersionStatement    = new CodeAssignStatement(versionPropertyReference, versionEnumFieldReference);
                    ctor.Statements.Add(assignVersionStatement);
                }
                ServiceDescription serviceDescription = ImportContext.Binding.ServiceDescription;
                string             url     = (soapAddress != null) ? soapAddress.Location : null;
                string             urlKey  = serviceDescription.AppSettingUrlKey;
                string             baseUrl = serviceDescription.AppSettingBaseUrl;
                ProtocolImporterUtil.GenerateConstructorStatements(ctor, url, urlKey, baseUrl);
            }
            else
            {
                ImportContext.CodeTypeDeclaration.BaseTypes.Add(typeof(WebService).FullName);
            }
        }
        // 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]));
        }
Пример #4
0
        public override void ImportClass()
        {
            SoapAddressBinding binding = (base.ImportContext.Port == null) ? null : ((SoapAddressBinding)base.ImportContext.Port.Extensions.Find(typeof(SoapAddressBinding)));

            if (base.ImportContext.Style == ServiceDescriptionImportStyle.Client)
            {
                base.ImportContext.CodeTypeDeclaration.BaseTypes.Add(typeof(SoapHttpClientProtocol).FullName);
                CodeConstructor ctor = WebCodeGenerator.AddConstructor(base.ImportContext.CodeTypeDeclaration, new string[0], new string[0], null, CodeFlags.IsPublic);
                ctor.Comments.Add(new CodeCommentStatement(Res.GetString("CodeRemarks"), true));
                bool flag = true;
                if (base.ImportContext is Soap12ProtocolImporter)
                {
                    flag = false;
                    CodeTypeReferenceExpression     targetObject = new CodeTypeReferenceExpression(typeof(SoapProtocolVersion));
                    CodeFieldReferenceExpression    right        = new CodeFieldReferenceExpression(targetObject, Enum.Format(typeof(SoapProtocolVersion), SoapProtocolVersion.Soap12, "G"));
                    CodePropertyReferenceExpression left         = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "SoapVersion");
                    CodeAssignStatement             statement    = new CodeAssignStatement(left, right);
                    ctor.Statements.Add(statement);
                }
                ServiceDescription serviceDescription = base.ImportContext.Binding.ServiceDescription;
                string             url = (binding != null) ? binding.Location : null;
                string             appSettingUrlKey  = serviceDescription.AppSettingUrlKey;
                string             appSettingBaseUrl = serviceDescription.AppSettingBaseUrl;
                ProtocolImporterUtil.GenerateConstructorStatements(ctor, url, appSettingUrlKey, appSettingBaseUrl, flag && !base.ImportContext.IsEncodedBinding);
            }
            else if (base.ImportContext.Style == ServiceDescriptionImportStyle.Server)
            {
                base.ImportContext.CodeTypeDeclaration.BaseTypes.Add(typeof(WebService).FullName);
            }
        }
Пример #5
0
        internal static void GetTypeFullNames(IList parameters, string[] typeFullNames, int start, int specifiedCount, CodeDomProvider codeProvider)
        {
            int num = 0;

            for (int i = 0; i < parameters.Count; i++)
            {
                typeFullNames[(i + start) + num] = WebCodeGenerator.FullTypeName(((SoapParameter)parameters[i]).mapping, codeProvider);
                if (((SoapParameter)parameters[i]).mapping.CheckSpecified)
                {
                    num++;
                    typeFullNames[(i + start) + num] = typeof(bool).FullName;
                }
            }
        }
        protected override CodeTypeDeclaration BeginClass()
        {
            base.MethodNames.Clear();
            base.ExtraCodeClasses.Clear();
            CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();

            if (base.Style == ServiceDescriptionImportStyle.Client)
            {
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(DebuggerStepThroughAttribute), new CodeExpression[0]);
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(DesignerCategoryAttribute), new CodeExpression[] { new CodePrimitiveExpression("code") });
            }
            Type[] types = new Type[] { typeof(SoapDocumentMethodAttribute), typeof(XmlAttributeAttribute), typeof(WebService), typeof(object), typeof(DebuggerStepThroughAttribute), typeof(DesignerCategoryAttribute), typeof(TransactionOption) };
            WebCodeGenerator.AddImports(base.CodeNamespace, WebCodeGenerator.GetNamespacesForTypes(types));
            CodeFlags isAbstract = (CodeFlags)0;

            if (base.Style == ServiceDescriptionImportStyle.Server)
            {
                isAbstract = CodeFlags.IsAbstract;
            }
            else if (base.Style == ServiceDescriptionImportStyle.ServerInterface)
            {
                isAbstract = CodeFlags.IsInterface;
            }
            CodeTypeDeclaration codeClass = WebCodeGenerator.CreateClass(base.ClassName, this.BaseClass.FullName, new string[0], metadata, CodeFlags.IsPublic | isAbstract, base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes));

            codeClass.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true));
            CodeConstructor ctor = WebCodeGenerator.AddConstructor(codeClass, new string[0], new string[0], null, CodeFlags.IsPublic);

            ctor.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true));
            HttpAddressBinding binding            = (base.Port == null) ? null : ((HttpAddressBinding)base.Port.Extensions.Find(typeof(HttpAddressBinding)));
            string             url                = (binding != null) ? binding.Location : null;
            ServiceDescription serviceDescription = base.Binding.ServiceDescription;

            ProtocolImporterUtil.GenerateConstructorStatements(ctor, url, serviceDescription.AppSettingUrlKey, serviceDescription.AppSettingBaseUrl, false);
            this.codeClasses.Add(codeClass);
            return(codeClass);
        }
        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);
        }
Пример #8
0
        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);
        }
Пример #11
0
        void GenerateCode(string typeName, MimeTextMatchCollection matches, CodeGenerationOptions options)
        {
            CodeIdentifiers     members   = new CodeIdentifiers();
            CodeTypeDeclaration codeClass = WebCodeGenerator.AddClass(ImportContext.CodeNamespace, typeName, string.Empty, new string[0], null, CodeFlags.IsPublic,
                                                                      ImportContext.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes));

            string[] fieldTypeNames = new string[matches.Count];
            for (int i = 0; i < matches.Count; i++)
            {
                MimeTextMatch match = matches[i];
                string        name  = members.AddUnique(CodeIdentifier.MakeValid(match.Name.Length == 0 ? methodName + "Match" : match.Name), match);
                CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();
                if (match.Pattern.Length == 0)
                {
                    throw new ArgumentException(Res.GetString(Res.WebTextMatchMissingPattern));
                }

                CodeExpression pattern       = new CodePrimitiveExpression(match.Pattern);
                int            numPropValues = 0;
                if (match.Group != 1)
                {
                    numPropValues++;
                }
                if (match.Capture != 0)
                {
                    numPropValues++;
                }
                if (match.IgnoreCase)
                {
                    numPropValues++;
                }
                if (match.Repeats != 1 && match.Repeats != int.MaxValue)
                {
                    numPropValues++;
                }
                CodeExpression[] propertyValues = new CodeExpression[numPropValues];
                string[]         propertyNames  = new string[propertyValues.Length];
                numPropValues = 0;
                if (match.Group != 1)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.Group);
                    propertyNames[numPropValues]  = "Group";
                    numPropValues++;
                }
                if (match.Capture != 0)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.Capture);
                    propertyNames[numPropValues]  = "Capture";
                    numPropValues++;
                }
                if (match.IgnoreCase)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.IgnoreCase);
                    propertyNames[numPropValues]  = "IgnoreCase";
                    numPropValues++;
                }
                if (match.Repeats != 1 && match.Repeats != int.MaxValue)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.Repeats);
                    propertyNames[numPropValues]  = "MaxRepeats";
                    numPropValues++;
                }
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(MatchAttribute), new CodeExpression[] { pattern }, propertyNames, propertyValues);

                string fieldTypeName;
                if (match.Matches.Count > 0)
                {
                    fieldTypeName     = ImportContext.ClassNames.AddUnique(CodeIdentifier.MakeValid(match.Type.Length == 0 ? name : match.Type), match);
                    fieldTypeNames[i] = fieldTypeName;
                }
                else
                {
                    fieldTypeName = typeof(string).FullName;
                }
                if (match.Repeats != 1)
                {
                    fieldTypeName += "[]";
                }

                CodeTypeMember member = WebCodeGenerator.AddMember(codeClass, fieldTypeName, name, null, metadata, CodeFlags.IsPublic, options);

                if (match.Matches.Count == 0 && match.Type.Length > 0)
                {
                    ImportContext.Warnings |= ServiceDescriptionImportWarnings.OptionalExtensionsIgnored;
                    ProtocolImporter.AddWarningComment(member.Comments, Res.GetString(Res.WebTextMatchIgnoredTypeWarning));
                }
            }

            for (int i = 0; i < fieldTypeNames.Length; i++)
            {
                string fieldTypeName = fieldTypeNames[i];
                if (fieldTypeName != null)
                {
                    GenerateCode(fieldTypeName, matches[i].Matches, options);
                }
            }
        }
Пример #12
0
        private void GenerateCode(string typeName, MimeTextMatchCollection matches, CodeGenerationOptions options)
        {
            CodeIdentifiers     identifiers = new CodeIdentifiers();
            CodeTypeDeclaration codeClass   = WebCodeGenerator.AddClass(base.ImportContext.CodeNamespace, typeName, string.Empty, new string[0], null, CodeFlags.IsPublic, base.ImportContext.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes));

            string[] strArray = new string[matches.Count];
            for (int i = 0; i < matches.Count; i++)
            {
                string        fullName;
                MimeTextMatch match      = matches[i];
                string        memberName = identifiers.AddUnique(CodeIdentifier.MakeValid((match.Name.Length == 0) ? (this.methodName + "Match") : match.Name), match);
                CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();
                if (match.Pattern.Length == 0)
                {
                    throw new ArgumentException(Res.GetString("WebTextMatchMissingPattern"));
                }
                CodeExpression expression = new CodePrimitiveExpression(match.Pattern);
                int            index      = 0;
                if (match.Group != 1)
                {
                    index++;
                }
                if (match.Capture != 0)
                {
                    index++;
                }
                if (match.IgnoreCase)
                {
                    index++;
                }
                if ((match.Repeats != 1) && (match.Repeats != 0x7fffffff))
                {
                    index++;
                }
                CodeExpression[] propValues = new CodeExpression[index];
                string[]         propNames  = new string[propValues.Length];
                index = 0;
                if (match.Group != 1)
                {
                    propValues[index] = new CodePrimitiveExpression(match.Group);
                    propNames[index]  = "Group";
                    index++;
                }
                if (match.Capture != 0)
                {
                    propValues[index] = new CodePrimitiveExpression(match.Capture);
                    propNames[index]  = "Capture";
                    index++;
                }
                if (match.IgnoreCase)
                {
                    propValues[index] = new CodePrimitiveExpression(match.IgnoreCase);
                    propNames[index]  = "IgnoreCase";
                    index++;
                }
                if ((match.Repeats != 1) && (match.Repeats != 0x7fffffff))
                {
                    propValues[index] = new CodePrimitiveExpression(match.Repeats);
                    propNames[index]  = "MaxRepeats";
                    index++;
                }
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(MatchAttribute), new CodeExpression[] { expression }, propNames, propValues);
                if (match.Matches.Count > 0)
                {
                    fullName    = base.ImportContext.ClassNames.AddUnique(CodeIdentifier.MakeValid((match.Type.Length == 0) ? memberName : match.Type), match);
                    strArray[i] = fullName;
                }
                else
                {
                    fullName = typeof(string).FullName;
                }
                if (match.Repeats != 1)
                {
                    fullName = fullName + "[]";
                }
                CodeTypeMember member = WebCodeGenerator.AddMember(codeClass, fullName, memberName, null, metadata, CodeFlags.IsPublic, options);
                if ((match.Matches.Count == 0) && (match.Type.Length > 0))
                {
                    HttpProtocolImporter importContext = base.ImportContext;
                    importContext.Warnings |= ServiceDescriptionImportWarnings.OptionalExtensionsIgnored;
                    ProtocolImporter.AddWarningComment(member.Comments, Res.GetString("WebTextMatchIgnoredTypeWarning"));
                }
            }
            for (int j = 0; j < strArray.Length; j++)
            {
                string str3 = strArray[j];
                if (str3 != null)
                {
                    this.GenerateCode(str3, matches[j].Matches, options);
                }
            }
        }
        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);
        }
Пример #14
0
        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);
            }
        }