/*
         *  Line 12:   namespace Hao.Kung.Samples {
         *  Line 13:       using System;
         *  Line 14:       using System.Net;
         *  Line 15:       using System.Web.Services;
         *  Line 16:       using System.Collections;
         *  Line 17:       using System.Xml.Serialization;
         *  Line 18:       using System.Web.Services;
         *  Line 19:
         *  Line 20:
         *  Line 21:       [WebService(Name="http://tempuri.org/")]
         *  Line 22:       [WebServiceBinding(ConformsTo=System.Web.Services.WsiProfiles.BasicProfile1_1)]
         *  Line 23:       public partial class MSNSearch {
         *  Line 24:
         *  Line 25:           public MSNSearch() {
         *  Line 26:               this.VirtualPath = "/atlas/BCL/msn.asbx";
         *                         this.BridgeXml = <contents of MSN.asbx>
         *  Line 27:           }
         *  Line 28:           [System.Web.Script.Services.ScriptService()]
         *  Line 29:           [System.Web.Services.WebMethodAttribute()]
         *  Line 30:           [System.Xml.Serialization.XmlIncludeAttribute(typeof(MSN.SearchRequest))]
         *  Line 31:           public object Search(System.Collections.IDictionary args) {
         *  Line 32:               return this.Invoke(new System.Web.Services.BridgeRequest("Search", args));
         *  Line 33:           }
         *  Line 34:
         */
        public override void GenerateCode(AssemblyBuilder assemBuilder)
        {
            CodeNamespace ns = new CodeNamespace(Service.Namespace);

            ns.Imports.Add(new CodeNamespaceImport("System"));
            ns.Imports.Add(new CodeNamespaceImport("System.Net"));
            ns.Imports.Add(new CodeNamespaceImport("System.Web.Services"));
            ns.Imports.Add(new CodeNamespaceImport("System.Collections"));
            ns.Imports.Add(new CodeNamespaceImport("System.Xml.Serialization"));
            ns.Imports.Add(new CodeNamespaceImport("Microsoft.Web.Preview.Services"));
            ns.Imports.Add(new CodeNamespaceImport("System.Web.Script.Services"));

            CodeCompileUnit unit = new CodeCompileUnit();

            unit.Namespaces.Add(ns);

            CodeTypeDeclaration classType = new CodeTypeDeclaration(Service.Classname);

            classType.BaseTypes.Add(typeof(BridgeHandler));
            classType.IsPartial = true;
            ns.Types.Add(classType);
            classType.CustomAttributes.Add(new CodeAttributeDeclaration("ScriptService"));
            classType.CustomAttributes.Add(new CodeAttributeDeclaration("WebService", new CodeAttributeArgument("Name", new CodePrimitiveExpression("http://tempuri.org/"))));
            classType.CustomAttributes.Add(new CodeAttributeDeclaration("WebServiceBinding", new CodeAttributeArgument("ConformsTo", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(WsiProfiles)), "BasicProfile1_1"))));

            /**
             * public ClassName() {
             *   VirtualPath = <the virtual path>
             * }
             */
            CodeConstructor constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;
            constructor.Statements.Add(new CodeAssignStatement(
                                           new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "VirtualPath"),
                                           new CodePrimitiveExpression(VirtualPath)));
            constructor.Statements.Add(new CodeAssignStatement(
                                           new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "BridgeXml"),
                                           new CodePrimitiveExpression(_bridgeXml)));
            classType.Members.Add(constructor);

            ServiceInfo serviceInfo = Service.ServiceInfo;

            foreach (BridgeMethodInfo methodInfo in serviceInfo.Methods.Values)
            {
                classType.Members.Add(GenerateWebMethodCode(methodInfo.Name, methodInfo.XmlIncludes, methodInfo.GetEnabled, methodInfo.ResponseFormat, false));
            }

            // Skip the proxy method code generation for REST
            if (!string.Equals(serviceInfo.ServiceClass, "Microsoft.Web.Preview.Services.BridgeRestProxy"))
            {
                classType.Members.Add(GenerateProxyMethodCode());
            }

            // BridgeMethod hook
            classType.Members.Add(GenerateWebMethodCode("__invokeBridge", null, false, ResponseFormat.Json, true));

            assemBuilder.AddCodeCompileUnit(this, unit);
            if (!string.IsNullOrEmpty(Service.PartialClassFile))
            {
                using (TextReader reader = OpenReader(Service.PartialClassFile)) {
                    CodeSnippetCompileUnit snippet = new CodeSnippetCompileUnit(reader.ReadToEnd());
                    if (HttpContext.Current != null)
                    {
                        snippet.LinePragma = new CodeLinePragma(HttpContext.Current.Request.MapPath(Service.PartialClassFile), 1);
                    }
                    assemBuilder.AddCodeCompileUnit(this, snippet);
                }
            }
        }
示例#2
0
        private static void BuildMethodInfo(ServiceInfo serviceInfo, XmlNode node)
        {
            XmlNodeList methodNodes = node.SelectNodes("method");

            foreach (XmlNode methodNode in methodNodes)
            {
                string name       = GetAttributeValue(methodNode, "name", true);
                string serverName = GetAttributeValue(methodNode, "serverName", false);
                if (serverName == null)
                {
                    serverName = name;
                }

                string getEnabled = GetAttributeValue(methodNode, "getEnabled", false);
                bool   get        = (getEnabled != null && String.Equals(getEnabled, "true", StringComparison.OrdinalIgnoreCase));

                ResponseFormat mode           = ResponseFormat.Json;
                string         responseFormat = GetAttributeValue(methodNode, "responseFormat", false);
                if (responseFormat != null && String.Equals(responseFormat, "xml", StringComparison.OrdinalIgnoreCase))
                {
                    mode = ResponseFormat.Xml;
                }

                BridgeMethodInfo methodInfo = new BridgeMethodInfo(name, serverName, get, mode);
                XmlNode          inputNode  = GetSingleNode(methodNode, "input", false);
                if (inputNode == null)
                {
                    methodInfo.Parameters = new Dictionary <string, BridgeParameterInfo>(0);
                }
                else
                {
                    methodInfo.Parameters = BuildParams(inputNode);
                }

                XmlNode requests = GetSingleNode(methodNode, "requestChain", false);
                if (requests != null)
                {
                    BuildMethodRequestChain(requests, methodInfo);
                }

                // REVIEW: Only used on Soap stuff currently
                XmlNodeList includeNodes = methodNode.SelectNodes("xmlinclude");
                foreach (XmlNode includeNode in includeNodes)
                {
                    methodInfo.XmlIncludes.Add(GetAttributeValue(includeNode, "type", true));
                }

                XmlNode transformNode = GetSingleNode(methodNode, "transforms", false);
                BuildTransforms(transformNode, methodInfo);

                XmlNode cachingNode = GetSingleNode(methodNode, "caching", false);
                BuildCaches(cachingNode, methodInfo);

                XmlNode authNode = methodNode.SelectSingleNode("authentication");
                if (authNode != null)
                {
                    methodInfo.Credentials = ParseCredentials(authNode);
                }

                serviceInfo.Methods[name] = methodInfo;
            }
        }
 private static ServiceInfo BuildServiceInfo(XmlNode node) {
     ServiceInfo serviceInfo = new ServiceInfo();
     XmlNode proxyNode = GetSingleNode(node, "proxy");
     serviceInfo.ServiceClass = GetAttributeValue(proxyNode, "type", true);
     serviceInfo.ServiceUrl = GetAttributeValue(proxyNode, "serviceUrl", false);
     BuildMethodInfo(serviceInfo, node);
     return serviceInfo;
 }
        private void Initialize(XmlDocument document) {
            XmlNode rootNode = document.SelectSingleNode("/bridge");
            if (rootNode == null)
                throw new InvalidOperationException("No <bridge> node found");

            Namespace = GetAttributeValue(rootNode, "namespace", true);
            Classname = GetAttributeValue(rootNode, "className", true);
            PartialClassFile = GetAttributeValue(rootNode, "partialClassFile", false);
            Language = GetAttributeValue(rootNode, "language", false);
            if (String.IsNullOrEmpty(Language)) {
                Language = "C#";
            }

            ServiceInfo = BuildServiceInfo(rootNode);
        }
        private static void BuildMethodInfo(ServiceInfo serviceInfo, XmlNode node) {
            XmlNodeList methodNodes = node.SelectNodes("method");
            foreach (XmlNode methodNode in methodNodes) {
                string name = GetAttributeValue(methodNode, "name", true);
                string serverName = GetAttributeValue(methodNode, "serverName", false);
                if (serverName == null) serverName = name;

                string getEnabled = GetAttributeValue(methodNode, "getEnabled", false);
                bool get = (getEnabled != null && String.Equals(getEnabled, "true", StringComparison.OrdinalIgnoreCase));

                ResponseFormat mode = ResponseFormat.Json;
                string responseFormat = GetAttributeValue(methodNode, "responseFormat", false);
                if (responseFormat != null && String.Equals(responseFormat, "xml", StringComparison.OrdinalIgnoreCase)) {
                    mode = ResponseFormat.Xml;
                }

                BridgeMethodInfo methodInfo = new BridgeMethodInfo(name, serverName, get, mode);
                XmlNode inputNode = GetSingleNode(methodNode, "input", false);
                if (inputNode == null) {
                    methodInfo.Parameters = new Dictionary<string, BridgeParameterInfo>(0);
                }
                else {
                    methodInfo.Parameters = BuildParams(inputNode);
                }

                XmlNode requests = GetSingleNode(methodNode, "requestChain", false);
                if (requests != null) {
                    BuildMethodRequestChain(requests, methodInfo);
                }

                // REVIEW: Only used on Soap stuff currently
                XmlNodeList includeNodes = methodNode.SelectNodes("xmlinclude");
                foreach (XmlNode includeNode in includeNodes) {
                    methodInfo.XmlIncludes.Add(GetAttributeValue(includeNode, "type", true));
                }

                XmlNode transformNode = GetSingleNode(methodNode, "transforms", false);
                BuildTransforms(transformNode, methodInfo);

                XmlNode cachingNode = GetSingleNode(methodNode, "caching", false);
                BuildCaches(cachingNode, methodInfo);

                XmlNode authNode = methodNode.SelectSingleNode("authentication");
                if (authNode != null) {
                    methodInfo.Credentials = ParseCredentials(authNode);
                }

                serviceInfo.Methods[name] = methodInfo;
            }
        }