Esempio n. 1
0
        private async Task RequestAsyncEvent(OnAshxEvent onAshxEvent)
        {
            if (onAshxEvent.IsFlush)
            {
                //response.Headers.Add("IsFlush", "true");
                this.Response.ContentType = onAshxEvent.ContentType;
                //this.response.HeaderEncoding = Encoding.UTF8;
                //this.response.Charset = "utf-8";
                await this.Response.FlushAsync();
            }
            //else
            //{
            //    response.Headers.Add("IsFlush", "false");
            //}

            if (StaticData.StaticAshxEvents.TryRemove(onAshxEvent.GuId, out OnAshxEvent onAshxEvent1))
            {
                onAshxEvent1.OnAshx = OnAshxEventState.OnlyID;
                onAshxEvent1.ManualReset.Set();
            }

            StaticData.StaticAshxEvents.TryAdd(onAshxEvent.GuId, onAshxEvent);

            Task task = Task.Factory.StartNew((_event) =>
            {
                OnAshxEvent _onAshxEvent = _event as OnAshxEvent;

                _onAshxEvent.ManualReset = new ManualResetEvent(false);

                if (!_onAshxEvent.ManualReset.WaitOne(_onAshxEvent.DelayTime))
                {
                    _onAshxEvent.OnAshx = OnAshxEventState.Timeout;
                    StaticData.StaticAshxEvents.TryRemove(_onAshxEvent.GuId, out _);
                }

                _onAshxEvent.ActionEvent(_onAshxEvent);
            }, onAshxEvent, TaskCreationOptions.PreferFairness);

            _ = task.ContinueWith((t) =>
            {
                OnAshxEvent _onAshxEvent = t.AsyncState as OnAshxEvent;
                _onAshxEvent.Dispose();
            });

            await task;
        }
Esempio n. 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);
            }
        }