Beispiel #1
0
 /// <summary>
 /// 在请求正常完成结束时触发
 /// </summary>
 /// <param name="ashx">获取接口信息</param>
 /// <returns></returns>
 protected virtual void OnResult(Ashx ashx)
 {
 }
Beispiel #2
0
        //AshxRouteData IHttpApi.RouteData => throw new NotImplementedException();

        //string IHttpApi.ApiKey => throw new NotImplementedException();


        /// <summary>
        /// 同步请求创建(开始)
        /// </summary>
        /// <param name="_objs">源数据</param>
        void IHttpApi.Request(object[] _objs)
        {
            Ashx ashx = this.RouteData.GetAshx;

            try
            {
                Action action = () => { ashx.Action.VoidExecute(this, _objs); }; //AshxExtension.Invoke(ashx.Method, this);

                action();

                OnResult(ashx);
            }
            catch (ThreadAbortException)
            {
                // This type of exception occurs as a result of Response.Redirect(), but we special-case so that
                // the filters don't see this as an error.
                throw;
            }
            catch (Exception ex)
            {
                AshxException exception = new(ashx, ex, _objs) { ExceptionHandled = true };
                AshxException(exception);
                IsException(exception);
            }
            //context.ApplicationInstance.CompleteRequest();
        }

        async Task IHttpAsynApi.TaskRequest(object[] _objs)
        {
            Ashx ashx = this.RouteData.GetAshx;

            try
            {
                if (ashx.IsOnAshxEvent)
                {
                    //OnAshxEvent onAshxEvent = ashx.Action.Execute(this, _objs) as OnAshxEvent;

                    OnAshxEvent func() => ashx.Action.Execute(this, _objs) as OnAshxEvent;

                    OnAshxEvent onAshxEvent = func();
                    await RequestAsyncEvent(onAshxEvent);
                }
                else
                {
                    //Func<Task> func = () => { return ashx.Action.Execute(this, _objs) as Task; };

                    async Task func() => await(Task) ashx.Action.Execute(this, _objs); //ThreadLocal AsyncLocal

                    await func();                                                      //AshxExtension.Invoke(ashx.Method, this, _objs as object[]) as Task; //
                }

                OnResult(ashx);
            }
            //catch (ThreadAbortException)
            //{
            //    // This type of exception occurs as a result of Response.Redirect(), but we special-case so that
            //    // the filters don't see this as an error.
            //    throw;
            //}
            //catch (AshxException ex)
            //{
            //    AshxException(new AshxException(ashx, ex) { ExceptionHandled = true });
            //}
            catch (Exception ex)
            {
                AshxException exception = new(ashx, ex, _objs) { ExceptionHandled = true };
                AshxException(exception);
                await IsTaskException(exception);
            }
        }
Beispiel #3
0
 /// <summary>
 /// 当链接真实有效时被执行,默认返回成功。(该方法是用于给使用者重写的)
 /// </summary>
 /// <param name="ashx">当前可以被调起的接口信息</param>
 /// <returns>当前请求会根据返回状态决定是否继续执行接口方法</returns>
 protected virtual bool Initialize(Ashx ashx)
 {
     return(true);
 }
Beispiel #4
0
 /// <summary>
 /// 隐性实现
 /// </summary>
 /// <param name="ashx"></param>
 /// <returns></returns>
 bool IHttpApi.Initialize(Ashx ashx)
 {
     return(Initialize(ashx));
 }
Beispiel #5
0
        void IMinHttpApi.Request(AshxRouteData ashxRoute, object[] _objs)
        {
            Ashx ashx = ashxRoute.GetAshx;

            try
            {
                Func <IApiOut> func = () => { return(ashx.Action.Execute(this, _objs) as IApiOut); }; //AshxExtension.Invoke(ashx.Method, this);

                IApiOut aipOut = func();

                aipOut?.HttpOutput(ashxRoute).Wait();

                OnResult(ashxRoute);
            }
            catch (ThreadAbortException)
            {
                // This type of exception occurs as a result of Response.Redirect(), but we special-case so that
                // the filters don't see this as an error.
                throw;
            }
            catch (Exception ex)
            {
                AshxException exception = new(ashx, ex, _objs) { ExceptionHandled = true };
                IApiOut       aipOut    = AshxException(exception);
                IsException(exception, ashxRoute, aipOut);
            }
        }

        async Task IMinHttpAsynApi.TaskRequest(AshxRouteData ashxRoute, object[] _objs)
        {
            Ashx ashx = ashxRoute.GetAshx;

            try
            {
                //Func<Task<IApiOut>> func = () => { return ashx.Action.Execute(this, _objs) as Task<IApiOut>; };
                //IApiOut aipOut = await func();

                //System.Runtime.CompilerServices.AsyncTaskMethodBuilder
                //System.Runtime.CompilerServices.AsyncTaskMethodBuilder<object> asyncTaskMethodBuilder = System.Runtime.CompilerServices.AsyncTaskMethodBuilder<object>.Create();
                //Task task = func(); //AsyncStateMachineBox
                //Utils.Data.DictionaryExtension.ToDictionary(task);
                //Type type = task.GetType();
                //var field = type.GetType().GetField("Result", System.Reflection.BindingFlags.Public);

                async Task <IApiOut> func() => await(Task <IApiOut>) ashx.Action.Execute(this, _objs);

                //{
                //    return await (Task<IApiOut>)ashx.Action.Execute(this, _objs);

                //    //Task <IApiOut> task = ashx.Action.Execute(this, _objs) as Task<IApiOut>;
                //    //return await task;
                //} //Task

                //object _task = func();// dynamic,Task

                //Task.WaitAll<object>((func() as Task<object>));

                //for (int i = 0; i < 1000000; i++)
                //{
                //    IApiOut aipOut1 = _task.GetValue("Result") as IApiOut; //aipOut1.Result as IApiOut;
                //}

                //var _r = _task.GetPropertieFind("Result");
                //for (int i = 0; i < 1000000; i++)
                //{
                //    IApiOut aipOut1 = _task.GetValue(_r) as IApiOut; //aipOut1.Result as IApiOut;
                //}

                //Task<object> s = Task.FromResult<object>(100);

                //for (int i = 0; i < 100000000; i++)
                //{
                //    object aipOut1 = s.Result; //aipOut1.Result as IApiOut;
                //}

                //dynamic d = _task;
                //for (int i = 0; i < 100000000; i++)
                //{
                //    IApiOut aipOut1 = d.Result; //aipOut1.Result as IApiOut;
                //}

                IApiOut aipOut = await func();//_task.GetValue("Result") as IApiOut; //aipOut1.Result as IApiOut;

                await aipOut?.HttpOutput(ashxRoute);

                OnResult(ashxRoute);
            }
            //catch (ThreadAbortException ex)
            //{
            //    // This type of exception occurs as a result of Response.Redirect(), but we special-case so that
            //    // the filters don't see this as an error.
            //    throw;
            //}
            //catch (AshxException ex)
            //{
            //    AshxException(new AshxException(ashx, ex) { ExceptionHandled = true });
            //}
            catch (Exception ex)
            {
                //AshxException(new AshxException(ashx, ex) { ExceptionHandled = true });
                AshxException exception = new(ashx, ex, _objs) { ExceptionHandled = true };
                IApiOut       aipOut    = AshxException(exception);
                await IsTaskException(exception, ashxRoute, aipOut);
            }

            //return await Task<IAipOut>.FromResult<IAipOut>(default);
        }

        void IMinHttpAsynApi.ContinueWith(Task task, AshxRouteData ashxRoute)
        {
            if (task.IsFaulted)
            {
                if (task.Exception.GetBaseException() is AshxException ashxException && !ashxException.ExceptionHandled)
                {
                    throw ashxException;
                }

                //AshxException ashxException = task.Exception.GetBaseException() as AshxException;
                //if (ashxException != null && !ashxException.ExceptionHandled) throw ashxException;
                //AshxException(new AshxException(ashxRoute.GetAshx, new Exception("发生异常,异步处理过程中发生异常。", task.Exception.GetBaseException())) { ExceptionHandled = true });
            }
            else if (!task.IsCompleted)
            {
                throw new Exception("发生异常,异步处理未完成。"); //AshxException(new AshxException(ashxRoute.GetAshx, ) { ExceptionHandled = true });
            }
            //else
            //{
            //    IAipOut aipOut = task.Result;
            //    aipOut?.HttpOutput(ashxRoute.HttpContext).Wait();
            //}
        }
    }