Exemple #1
0
 private static void IsException(AshxException ex, AshxRouteData ashxRoute, IApiOut aipOut)
 {
     if (ex.ExceptionHandled)
     {
         aipOut?.HttpOutput(ashxRoute).Wait();
     }
     else
     {
         throw ex;
     }
 }
Exemple #2
0
 private static async Task IsTaskException(AshxException ex, AshxRouteData ashxRoute, IApiOut aipOut)
 {
     if (ex.ExceptionHandled)
     {
         if (aipOut == null)
         {
             await Task.CompletedTask;
         }
         else
         {
             await aipOut?.HttpOutput(ashxRoute);//await Task.CompletedTask;
         }
     }
     else
     {
         await Task.FromException(ex);
     }
 }
Exemple #3
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();
            //}
        }
    }