Beispiel #1
0
        /// <summary>
        /// Pulls out all of the exposed methods from the view into a dictionary, indexed by method name. This does
        /// mean that we can't support overloading of a function, each exposed method must be unique.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="methodMap"></param>
        /// <returns></returns>
        private void FindExposedMethods(Type type, Dictionary <string, ExposedMethod> methodMap)
        {
            var reservedMethodNames = new string[] {
                NormaliseString(ViewHeartbeatMethod),
                NormaliseString(GetDeferredResponseMethod),
            };

            foreach (var method in type.GetMethods())
            {
                var webAdminMethodAttribute = method.GetCustomAttributes(inherit: true).OfType <WebAdminMethodAttribute>().SingleOrDefault();
                if (webAdminMethodAttribute != null)
                {
                    var key = NormaliseString(method.Name);

                    if (reservedMethodNames.Contains(key))
                    {
                        throw new InvalidOperationException($"Views cannot expose a method called {method.Name}, it has been reserved for use by the mapper");
                    }
                    if (methodMap.ContainsKey(key))
                    {
                        throw new InvalidOperationException("Function overloading is not supported by the view mapper");
                    }

                    var exposedMethod = new ExposedMethod(webAdminMethodAttribute, method);
                    methodMap.Add(key, exposedMethod);
                }
            }
        }
Beispiel #2
0
 public DeferredMethod(long requestId, MappedView mappedView, ExposedMethod exposedMethod, object[] parameters)
 {
     JobId         = Guid.NewGuid().ToString();
     CreatedUtc    = DateTime.UtcNow;
     RequestId     = requestId;
     MappedView    = mappedView;
     ExposedMethod = exposedMethod;
     Parameters    = parameters;
 }
Beispiel #3
0
        /// <summary>
        /// Builds the parameters for an exposed method from the request passed in.
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private object[] MapParameters(ExposedMethod exposedMethod, RequestReceivedEventArgs args)
        {
            var result = new List <object>();

            var values = new Dictionary <string, string>();

            foreach (var queryStringKey in args.QueryString.AllKeys)
            {
                var name      = NormaliseString(queryStringKey);
                var textValue = args.QueryString[queryStringKey];
                values.Add(name, textValue);
            }
            foreach (var formKey in args.Request.FormValues.AllKeys)
            {
                var name      = NormaliseString(formKey);
                var textValue = args.Request.FormValues[formKey];
                values.Add(name, textValue);
            }

            if (!String.IsNullOrEmpty(exposedMethod.WebAdminMethod.User))
            {
                var userParameter = exposedMethod.WebAdminMethod.User;
                if (values.ContainsKey(userParameter))
                {
                    values.Remove(userParameter);
                }
                values.Add(userParameter, args.UserName);
            }

            foreach (var parameterInfo in exposedMethod.MethodInfo.GetParameters())
            {
                var name = NormaliseString(parameterInfo.Name);

                if (values.ContainsKey(name))
                {
                    result.Add(ParseTextValue(parameterInfo, values[name]));
                }
                else
                {
                    if (parameterInfo.IsOptional)
                    {
                        result.Add(parameterInfo.DefaultValue);
                    }
                    else
                    {
                        throw new InvalidOperationException($"The parameter {parameterInfo.Name} is required but was not supplied");
                    }
                }
            }

            return(result.ToArray());
        }