Example #1
0
        public static BridgeService GetService(string virtualPath)
        {
            BridgeService service = null;

            if (s_cache.TryGetValue(virtualPath, out service))
            {
                return(service);
            }
            return(null);
        }
Example #2
0
        public virtual void TransformResponse()
        {
            object           rawResponse = ServiceResponse.Response;
            BridgeMethodInfo methodInfo  = GetMethodInfoForCall(BridgeRequest.Method);

            foreach (Pair pair in methodInfo.ResponseTransforms)
            {
                Type   type;
                string typeStr = (string)pair.First;
                IBridgeResponseTransformer transformer = BridgeService.CreateInstance(typeStr, out type) as IBridgeResponseTransformer;
                if (transformer == null)
                {
                    throw new InvalidOperationException(typeStr + " is not of type IBridgeResponseTransformer");
                }
                transformer.Initialize((BridgeTransformData)pair.Second);
                rawResponse = transformer.Transform(rawResponse);
            }
            BridgeResponse.Response = rawResponse;
        }
        /**
         * public virtual object CallServiceClassMethod(string method, Dictionary<string, object> args, ICredentials credentials, string serviceUrl) {
         *   if (method.Equals("method")) {
         *        ServiceProxy proxy = new ServiceProxy();
         *        proxy.Url = serviceUrl (if Proxy has a Url property)
         *        proxy.Credentials = credentials (if Proxy has a Credentials property)
         *        object arg = args[paramInfo.ServiceName];
         *        ArgType1 arg1;
         *        if (arg is argType1) arg1 = (ArgType1)arg;
         *        return new Proxy().Method1(arg1, arg2, ...);
         */
        private CodeMemberMethod GenerateProxyMethodCode()
        {
            CodeMemberMethod method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Override | MemberAttributes.Public;
            method.Name       = "CallServiceClassMethod";
            method.ReturnType = new CodeTypeReference(typeof(object));
            method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "method"));
            method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Dictionary <String, object>), "args"));
            method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(ICredentials), "credentials"));
            method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "url"));

            // Prevent duplicate methods, as many bridge methods can map to the same server call
            Dictionary <string, NGenWrapper <bool> > methodMap = new Dictionary <string, NGenWrapper <bool> >();
            Type serviceType = BridgeService.GetType(_service.ServiceInfo.ServiceClass);

            foreach (BridgeMethodInfo methodInfo in _service.ServiceInfo.Methods.Values)
            {
                if (methodMap.ContainsKey(methodInfo.ServerName))
                {
                    continue;
                }

                methodMap[methodInfo.ServerName] = true;

                MethodInfo serviceMethodInfo = serviceType.GetMethod(methodInfo.ServerName);
                if (serviceMethodInfo == null)
                {
                    throw new ArgumentException("No such method on service proxy class: " + methodInfo.ServerName);
                }
                ParameterInfo[] paramData = serviceMethodInfo.GetParameters();

                // if (method == "Method1"
                CodeConditionStatement ifStmt = new CodeConditionStatement();
                ifStmt.Condition = new CodeMethodInvokeExpression(new CodePrimitiveExpression(methodInfo.ServerName), "Equals", new CodeArgumentReferenceExpression("method"));
                // <ServiceClass> proxy = new <ServiceClass>()
                ifStmt.TrueStatements.Add(new CodeVariableDeclarationStatement(serviceType, "proxy"));
                ifStmt.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("proxy"), new CodeObjectCreateExpression(serviceType)));

                // check if the proxy class has a Credentials property
                PropertyInfo credProp = serviceType.GetProperty("Credentials");
                if (credProp != null)
                {
                    // proxy.Credentials = credentials;
                    CodeConditionStatement ifCredNotNull = new CodeConditionStatement();
                    ifCredNotNull.Condition = new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("credentials"), CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
                    ifCredNotNull.TrueStatements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("proxy"), "Credentials"), new CodeVariableReferenceExpression("credentials")));
                    ifStmt.TrueStatements.Add(ifCredNotNull);
                }

                // check if the proxy class has a Url property
                PropertyInfo urlProp = serviceType.GetProperty("Url");
                if (urlProp != null)
                {
                    // proxy.Credentials = credentials;
                    CodeConditionStatement ifUrlNotEmpty = new CodeConditionStatement();
                    ifUrlNotEmpty.Condition = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(string)), "IsNullOrEmpty"), new CodeVariableReferenceExpression("url"));
                    ifUrlNotEmpty.FalseStatements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("proxy"), "Url"), new CodeVariableReferenceExpression("url")));
                    ifStmt.TrueStatements.Add(ifUrlNotEmpty);
                }

                // object obj holder for args dictionary lookup
                ifStmt.TrueStatements.Add(new CodeVariableDeclarationStatement(typeof(object), "obj"));

                // <method>(ConvertValue(arg1, argType1...), ConvertValue(arg2, argType1...), ...)
                CodeMethodInvokeExpression methodCallExpr = new CodeMethodInvokeExpression();
                methodCallExpr.Method = new CodeMethodReferenceExpression(new CodeVariableReferenceExpression("proxy"), methodInfo.ServerName);
                for (int i = 0; i < paramData.Length; ++i)
                {
                    Type   argType = paramData[i].ParameterType;
                    string argName = "arg" + i;

                    // ArgTypeN argN
                    ifStmt.TrueStatements.Add(new CodeVariableDeclarationStatement(argType, argName));

                    // if (!Dict<name>.TryGet(argName, argn)) throw new ArgumentException()
                    CodeConditionStatement findArgStmt = new CodeConditionStatement();
                    findArgStmt.Condition = new CodeMethodInvokeExpression(new CodeArgumentReferenceExpression("args"),
                                                                           "TryGetValue",
                                                                           new CodePrimitiveExpression(paramData[i].Name),
                                                                           new CodeDirectionExpression(FieldDirection.Out, new CodeVariableReferenceExpression("obj")));
                    findArgStmt.FalseStatements.Add(new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof(ArgumentException),
                                                                                                                   new CodePrimitiveExpression("Argument not found: " + paramData[i].Name))));
                    ifStmt.TrueStatements.Add(findArgStmt);

                    // argn = (argtype)ConvertToType(obj, argtype, "paramname");
                    ifStmt.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(argName),
                                                                      new CodeCastExpression(argType, new CodeMethodInvokeExpression(new CodeSnippetExpression("BridgeHandler"),
                                                                                                                                     "ConvertToType",
                                                                                                                                     new CodeVariableReferenceExpression("obj"),
                                                                                                                                     new CodeTypeOfExpression(argType)))));

                    // Build up the arg1, arg2, argn for the method call;
                    methodCallExpr.Parameters.Add(new CodeVariableReferenceExpression(argName));
                }

                ifStmt.TrueStatements.Add(new CodeMethodReturnStatement(methodCallExpr));

                method.Statements.Add(ifStmt);
            }
            method.Statements.Add(new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof(ArgumentException),
                                                                                                 new CodePrimitiveExpression("CallServiceClassMethod: Unknown method"))));
            return(method);
        }
Example #4
0
 public static void UpdateCache(string virtualPath, BridgeService service)
 {
     lock (s_cache) {
         s_cache[virtualPath] = service;
     }
 }
 public static void UpdateCache(string virtualPath, BridgeService service) {
     lock (s_cache) {
         s_cache[virtualPath] = service;
     }
 }