コード例 #1
0
        void ProcessRequest(ServiceContext context)
        {
            if(bridges.Count() == 0)
            {
                context.Respond(NoExposedMethods());
                Log(LogLevel.Error, "There are no methods exposed by this service!");
                return;
            }

            if(context.RequireAuthorization && !AuthorizeRequest(context.Request))
            {
                context.Respond(Unauthorized());
                Log(LogLevel.Warning, "Unauthorized request");
            }
            else
            {
                switch(context.ResponseType)
                {
                    case ResponseType.Describe:
                        context.Respond(Describe());
                        Log(LogLevel.Info, "Describing service, {0}ms", context.ProcessingTime);
                        break;

                    case ResponseType.NoGenericSupport:
                        Exception exception = new Exception("Methods with generic parameters are not supported.");
                        context.Respond(CallFailure(exception));
                        Log(LogLevel.Warning, exception.Message);
                        break;

                    case ResponseType.NoMethod:
                        context.Respond(NoMatchingMethod());
                        Log(LogLevel.Warning, "No suitable method found for {0}", context.Request.Url.PathAndQuery);
                        break;

                    case ResponseType.WrongVerb:
                        context.Respond(InvalidVerb());
                        Log(LogLevel.Warning, "Invalid HTTP verb");
                        break;

                    case ResponseType.Invoke:
                        InvokeRequestedMethod(context);
                        break;
                }
            }
        }
コード例 #2
0
        void InvokeRequestedMethod(ServiceContext context)
        {
            try
            {
                var map = context.GetMappedParameters();
                var result = GetType().InvokeMember(
                    name: context.Bridge.MethodInfo.Name,
                    invokeAttr: _bindingFlags,
                    binder: Type.DefaultBinder,
                    target: this,
                    args: map.Values.ToArray(),
                    modifiers: null,
                    culture: null,
                    namedParameters: map.Keys.ToArray()
                );

                context.Respond(result);
                string paramString = string.Join(", ", map.Select(kvp => string.Format("{0}={1}", kvp.Key, kvp.Value)).ToArray());
                Log(LogLevel.Info, "Invoked {0}({1}), {2}ms", context.Bridge.QualifiedName, paramString, context.ProcessingTime);
            }
            catch(ArgumentException ae)
            {
                context.Respond(ParameterFailure(ae));
                Log(LogLevel.Warning, "Parameter value missing or invalid");
            }
            catch(JsonParserException)
            {
                context.Respond(InvalidJsonPosted());
                Log(LogLevel.Warning, "Unable to parse the posted json document.");
            }
            catch(Exception e)
            {
                context.Respond(CallFailure(e.InnerException ?? e));
                Log(LogLevel.Warning, "Failure to execute method");
            }
        }