public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseDeveloperExceptionPage();

            app.UseMetadata(new MetadataOptions
            {
                BeforeInvoke          = BeforeInvoke.LocalOnly,
                Path                  = "_meta",
                IndentJson            = env.IsDevelopment(),
                IncludeNullProperties = true,
                Name                  = "SimpleSoft Middleware ExampleApi",
                Environment           = env.EnvironmentName
            });

            app.UseHealthCheck(new HealthCheckOptions
            {
                BeforeInvoke      = BeforeInvoke.IsLocalOrHasAnyRole("admin"),
                Path              = "_health",
                IndentJson        = env.IsDevelopment(),
                StringEnum        = true,
                ParallelExecution = true
            });

            app.Run(async context =>
            {
                await context.Response.WriteAsync(IndexHtml);
            });
        }
Esempio n. 2
0
 private void OnTriggerExecute(TriggerBase trigger)
 {
     if (Action is null)
     {
         return;
     }
     lock (_localSync)
     {
         _tokenSource = new CancellationTokenSource();
         var token = _tokenSource.Token;
         _triggerTask = Task.Run(() =>
         {
             if (Core.Now - _lastUpdateTime > WaitTime)
             {
                 _lastUpdateTime = Core.Now;
                 BeforeInvoke?.Invoke(this, new TriggeredEventArgs <Action>(trigger, Action));
                 var loadOk = true;
                 var sw     = Stopwatch.StartNew();
                 try
                 {
                     if (CancellationToken.IsCancellationRequested)
                     {
                         return;
                     }
                     Action();
                     if (CancellationToken.IsCancellationRequested)
                     {
                         return;
                     }
                     sw.Stop();
                 }
                 catch (Exception ex)
                 {
                     sw.Stop();
                     loadOk = false;
                     OnException?.Invoke(this, new ExceptionEventArgs(ex, sw.Elapsed.TotalMilliseconds));
                 }
                 if (loadOk)
                 {
                     AfterInvoke?.Invoke(this, new TriggeredEventArgs <Action>(trigger, Action, sw.Elapsed.TotalMilliseconds));
                 }
             }
             else
             {
                 NotInvokedTooRecent?.Invoke(this, new TriggeredEventArgs <Action>(trigger, Action));
             }
         }, token);
         try
         {
             _triggerTask.Wait(token);
         }
         catch
         {
             // ignored
         }
         _triggerTask = null;
         _tokenSource = null;
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Fires the BeforeInvoke event.
 /// </summary>
 /// <param name="e">Event arguments.</param>
 protected internal virtual void OnBeforeInvoke(BeforeInvokeEventArgs e)
 {
     BeforeInvoke?.Invoke(this, e);
 }
Esempio n. 4
0
        protected override object Invoke(MethodInfo targetMethod, object[] args)
        {
            Log.Debug("[MethodProxy.Invoke] " + (new MethodCallInfo()
            {
                Name = targetMethod.Name, Args = args.ToList()
            }).ToString());

            bool          IsIInvokeProxy = _invokeType == InvokeType.Proxy || _invokeType == InvokeType.ProxyFunc;
            IInvokerProxy invoker;

            switch (_invokeType)
            {
            case InvokeType.Proxy:
                invoker = _invokerProxy;
                break;

            case InvokeType.ProxyFunc:
                invoker = GetIInvokerProxyFunc();
                break;

            default:
                invoker = null;
                break;
            }

            if (IsIInvokeProxy || BeforeInvoke != null)
            {
                BeforeInvokeEventArgs beforeArgs = new BeforeInvokeEventArgs()
                {
                    MethodInfo = targetMethod,
                    Args       = args,
                };
                BeforeInvoke?.Invoke(this, beforeArgs);
                invoker?.BeforeInvoke(this, beforeArgs);
                if (beforeArgs.Handled)
                {
                    return(beforeArgs.FakeResult);
                }
            }

            object result;

            if (IsIInvokeProxy)
            {
                result = invoker.Invoke(targetMethod, args);
            }
            else if (_invokeType == InvokeType.Object)
            {
                result = targetMethod.Invoke(_invokeObject, args);
            }
            else
            {
                throw new Exception("InvokeProxy: unknown InvokeType");
            }

            if (result is Task task)
            {
                if (targetMethod.ReturnType.IsGenericType)
                {
                    Type type = targetMethod.ReturnType.GetGenericArguments()[0];
                    if (TypeHelper.IsType(task, typeof(Task <object>)))
                    {
                        result = TaskHelper.ConvertTaskObject((Task <object>)task, type);
                    }
                    else
                    {
                        // e.g. UnwrapPromise<object>
                        var generic_task = TaskHelper.ConvertTaskReturnObject(task);
                        result = TaskHelper.ConvertTaskObject(generic_task, type);
                    }
                }
                else
                {
                    Task.Run(async() => await task);
                    result = Task.CompletedTask;
                }
            }

            TypeHelper.AssertType(result, targetMethod.ReturnType);

            if (IsIInvokeProxy || AfterInvoke != null)
            {
                AfterInvokeEventArgs afterArgs = new AfterInvokeEventArgs()
                {
                    MethodInfo = targetMethod,
                    Args       = args,
                    Result     = result,
                };
                AfterInvoke?.Invoke(this, afterArgs);
                invoker?.AfterInvoke(this, afterArgs);
            }
            return(result);
        }