Пример #1
0
        private void Process(IAsyncResult ar)
        {
            var waiter  = (AutoResetEvent)ar.AsyncState;
            var context = _http.EndGetContext(ar);

            waiter.Set();
            var request = new NetApiRequest(context.Request);
            var setting = GetSetting(request.RequestMethod, request.Url.AbsolutePath);

            if (setting == null)
            {
                return;
            }

            var before = new NetApiBefore(request)
            {
                Instance = TypeResolve(setting.ClassType),
                Method   = setting.Method
            };

            try
            {
                ExecuteBefore?.Invoke(this, before);

                if (before.Result == null)
                {
                    ((NetApi)before.Instance).Request = request;
                    before.Result = before.Method.Invoke(before.Instance, null)?.ToString();
                }

                ExecuteAfter?.Invoke(this, new NetApiAfter(request)
                {
                    Instance = before.Instance,
                    Method   = before.Method,
                    Result   = before.Result
                });
            }
            catch (Exception ex)
            {
                var err = new NetApiError(request)
                {
                    Error    = ex,
                    Instance = before.Instance,
                    Method   = before.Method
                };
                ExecuteError?.Invoke(this, err);
                if (err.Result != null)
                {
                    before.Result = err.Result;
                }
            }

            var result = before.Result ?? string.Empty;
            var data   = Encoding.UTF8.GetBytes(result);

            context.Response.ContentType     = "application/json";
            context.Response.ContentLength64 = data.Length;
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.StatusCode      = 200;
            context.Response.OutputStream.Write(data, 0, data.Length);
            context.Response.Close();
        }
Пример #2
0
 internal NetApiBefore(NetApiRequest request)
 {
     Request = request;
 }
Пример #3
0
 internal NetApiError(NetApiRequest request)
 {
     Request = request;
 }
Пример #4
0
 internal NetApiAfter(NetApiRequest request)
 {
     Request = request;
 }