예제 #1
0
        private void RequestHandler(RelayedHttpListenerContext context)
        {
            try
            {
                this.logger?.LogMessage(LogLevel.Verbose, $"Request: {context.Request.HttpMethod} {context.Request.Url.AbsolutePath}");
                PluginMethod method = this.FindMethod(context.Request.Url.AbsolutePath, context.Request.HttpMethod);
                if (method == null)
                {
                    string message = $"Method not found. Uri = {context.Request.Url.AbsoluteUri}, Method = {context.Request.HttpMethod}";

                    this.logger?.LogMessage(LogLevel.Warning, message);

                    context.Response.StatusCode        = HttpStatusCode.NotFound;
                    context.Response.StatusDescription = message;
                    return;
                }

                try
                {
                    this.logger?.LogMessage(LogLevel.Verbose, $"Execute: {method}");
                    method.Invoke(context);
                }
                catch (Exception e)
                {
                    context.Response.StatusCode = HttpStatusCode.InternalServerError;
                    this.logger?.LogException(e);
                }
            }
            finally
            {
                context.Response.Close();
            }
        }
예제 #2
0
        internal static AgentPlugin Create(AgentPluginMetadata metadata, IAgentPlugin plugin, ILogger logger)
        {
            logger.LogMessage(LogLevel.Verbose, $"Loading: {metadata.RootPath}");
            MethodInfo[] methodInfos = plugin.GetType().GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod | BindingFlags.Instance);
            if (methodInfos == null || methodInfos.Length == 0)
            {
                return(null);
            }

            ImmutableDictionary <string, ImmutableDictionary <string, PluginMethod> > methodByVerb = methodInfos
                                                                                                     .Select(m => PluginMethod.Create(plugin, m))
                                                                                                     .Where(m => m != null)
                                                                                                     .GroupBy(m => m.HttpMethod)
                                                                                                     .ToImmutableDictionary(g => g.Key, g => g.ToImmutableDictionary(m => m.Name));

            if (methodByVerb.Count == 0)
            {
                logger.LogMessage(LogLevel.Warning, "No valid methods found.");
                return(null);
            }

            foreach (var mbv in methodByVerb)
            {
                foreach (var method in mbv.Value)
                {
                    logger.LogMessage(LogLevel.Verbose, $"Exporting: {mbv.Key} {method.Key}");
                }
            }

            return(new AgentPlugin(metadata, methodByVerb));
        }