Пример #1
0
        public async Task <HttpResponse> Execute(HttpRequest request)
        {
            HttpContext context = new HttpContext(request);

            try
            {
                Entry searchEntry = new Entry()
                {
                    Method = request.Method,
                    Route  = request.BaseUrl
                };

                IMiddleware middleware = FilterMiddlewares(searchEntry);
                if (middleware == null)
                {
                    context.Response.StatusCode = HttpStatusCode.NotFound;
                }
                else
                {
                    await middleware.Run(context);
                }
            }
            catch (Exception e)
            {
#if !DEBUG
                context.Response.StatusCode = HttpStatusCode.InternalServerError;
                context.Response.Send("Internal Server Error: " + e.Message);
#else
                throw new Exception("Error during MiddlewareManager Execution.", e);
#endif
            }

            return(context.Response);
        }
Пример #2
0
 /// <summary>
 /// 创建一个线程并运行 <see cref="Run" />
 /// </summary>
 public async Task StartAsync()
 {
     await Task.Run(() =>
     {
         IApiClient apiClient = Provider.GetRequiredService <IApiClient>();
         apiClient.Connect();
         apiClient.On <GenericEventArgs>((sender, args) =>
         {
             using IServiceScope scope = Provider.CreateScope();
             foreach (Type type in Middlewares)
             {
                 IMiddleware middleware = ActivatorUtilities.CreateInstance(Provider, type) as IMiddleware;
                 if (!middleware.Run(sender, args))
                 {
                     break;
                 }
             }
         });
         apiClient.Listen();
     });
 }
Пример #3
0
 private async Task <PipelineResponse <TOut> > RunInner(Func <Task <PipelineResponse <TOut> > > func)
 {
     return(await _middleware.Run(func));
 }