Exemplo n.º 1
0
        public void Configure(MiddleWare <HttpContext> app)
        {
            //全局异常处理
            app.Add(async(context, next) =>
            {
                try
                {
                    await next();
                }
                catch (Exception ex)
                {
                    context.Response.State       = HttpResponseState.InternalServerError;
                    context.Response.ContentType = "text/html";
                    context.Response.Body        = new MemoryStream();
                    context.Response.Write("<head><meta http-equiv=\"content-type\" content=\"text/html; charset =utf-8\" /></head>");
                    context.Response.Write($"<p>{ex.ToString()}</p>");
                }
            });
            //静态文件处理模型
            app.Add(new StaticsFileMiddleWare());
            //session模块
            app.Add(new SessionMiddleWare());
            //mvc 中间件
            app.Add(new MvcMiddleWare(options => {
                //路由配置
                options.Routes.Add(new Route
                {
                    Name              = "route",
                    Template          = "{controller}/{action}/{id}",
                    DefaultController = "home",
                    DefaultAction     = "action"
                });
                options.Routes.Add(new Route
                {
                    Name              = "route",
                    Template          = "{controller}/{action}",
                    DefaultController = "home",
                    DefaultAction     = "index"
                });
                //各种过滤器
                options.Filters.Add(new ActionFilter());
                options.Filters.Add(new ActionFilter_1());
                options.Filters.Add(new AuthorizationFilter());
                options.Filters.Add(new AuthorizationFilter_1());
                options.Filters.Add(new ExceptionFilter());
                options.Filters.Add(new ExceptionFilter_1());
            }));

            //app.Add(async (context, next) =>
            //{

            //    context.Response.ContentType = "text/html";
            //    context.Response.Write("<head><meta http-equiv=\"content-type\" content=\"text/html; charset =utf-8\" /></head>");
            //    context.Response.Write("<h2>hello world !!</h2>");
            //    context.Response.Write("<h2>你好世界!</h2>");
            //    context.Response.Write($"<h2>sessionId:{context.Request.Cookies["SessionId"]}</h2>");
            //    context.Response.Write("<img src='/bg.jpeg'/>");
            //    //throw new Exception("test excp");
            //});
        }
Exemplo n.º 2
0
 public Client(IConfigure configure)
 {
     //初始化中间件
     middleWareHandler = new MiddleWareHandler <HttpContext>();
     middleWare        = new MiddleWare <HttpContext>(middleWareHandler);
     configure.Configure(middleWare);
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            MiddleWare <string> middleWare = new MiddleWare <string>();

            middleWare.Add(async(s, next) =>
            {
                Console.WriteLine("--异常处理层--");
                try
                {
                    await next.Invoke();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"----------异常:{ex.Message}\r\n{ex.StackTrace}");
                }
                Console.WriteLine("--异常处理层--");
            });

            middleWare.Add(async(s, next) =>
            {
                Console.WriteLine(" 中间件第一层---开始");

                await next.Invoke();
                Console.WriteLine(" 中间件第一层---结束");
            });
            middleWare.Add(async(s, next) =>
            {
                Console.WriteLine("  中间件第二层---开始");
                await next.Invoke();
                Console.WriteLine("  中间件第二层---结束");
            });
            middleWare.Add(async(s, next) =>
            {
                Console.WriteLine("   中间件第三层---开始");
                await next.Invoke();
                Console.WriteLine("   中间件第三层---结束");
            });

            middleWare.Add(async(s, next) =>
            {
                await Task.Run(() => { Console.WriteLine("    中间件第四层---开始"); });
                await next.Invoke();
                await Task.Run(() => { Console.WriteLine("    中间件第四层---结束"); });
            });
            middleWare.Add(new MyProcess());

            middleWare.Add(async(s, next) =>
            {
                Console.WriteLine($"       暂停 3s");
                Thread.Sleep(3000);
                Console.WriteLine($"       输入的内容是:{s}");

                await Task.Delay(0);
            });

            middleWare.Execute("哈哈哈哈哈");
            //Task.WaitAll(middleWare.Execute("哈哈哈哈哈"));
            Console.ReadLine();
        }
Exemplo n.º 4
0
        static MvcMiddleWare()
        {
            //初始化中间件
            middleWareHandler = new MiddleWareHandler <ActionContext>();
            middleWare        = new MiddleWare <ActionContext>(middleWareHandler);
            Configure(middleWare);
            //载入缓存
            LoadCache();
            //初始化动作过滤器
            InitActionFilter();

            Console.WriteLine("启动成功。。。");
        }
Exemplo n.º 5
0
        /// <summary>
        /// 初始化动作过滤器
        /// </summary>
        private static void InitActionFilter()
        {
            actionMiddleWareHandler = new MiddleWareHandler <ActionMiddWareContext>();
            actionMiddleWare        = new MiddleWare <ActionMiddWareContext>(actionMiddleWareHandler);
            var filters = Configuration.Filters.Where(f => f.GetType().IsSubclassOf(typeof(ActionFilter))).ToList();

            if (filters.Count > 0)
            {
                foreach (ActionFilter filter in filters)
                {
                    actionMiddleWare.Add(async(context, next) =>
                    {
                        filter.OnExecuting(context.ActionContext);
                        await next();
                        filter.Executed(context.ActionContext);
                    });
                }
            }
            //实际要调用继续往下调用的中间件
            actionMiddleWare.Add(async(context, next) =>
            {
                await context.Action();
            });
        }
        //First Create Class which Return Object
        //  ReVerification Class Name
        public DataTable FetchQCCCustomerAddress(string TradingLicense)
        {
            string Result = "";
            // return TradingLicense + "|" + TradingLicense;
            DataTable _EmployeeInfo = new DataTable();
            QCCCustomer _Obj = new QCCCustomer();
            try
            {
                MiddleWare _Onwire2 = new MiddleWare();
                _EmployeeInfo = _Onwire2.FechQccCustomerAdressWithID(TradingLicense);
            }
            catch (Exception ex)
            {
                string ErrorMessage = Convert.ToString(ex.Message.ToString());
                Common.LogSystemException("ScalesWebService-FetchQCCCustomer", ErrorMessage, ErrorMessage, "IQCCClients");
                //QCCCustomer _Obj = new QCCCustomer();
                _Obj.Email = ex.Message.ToString();

            }
            return _EmployeeInfo;
        }
Exemplo n.º 7
0
        private static void Configure(MiddleWare <ActionContext> app)
        {
            //异常过滤器
            app.Add(async(context, next) =>
            {
                try
                {
                    await next();
                }
                catch (Exception ex)
                {
                    var filters = Configuration.Filters.Where(f => f.GetType().IsSubclassOf(typeof(ExceptionFilter))).ToList();
                    if (filters.Count > 0)
                    {
                        var expcontext        = new ExceptionContext();
                        expcontext.context    = context.context;
                        expcontext.Controller = context.Controller;
                        expcontext.Action     = context.Action;
                        expcontext.Result     = context.Result;
                        expcontext.exception  = ex;
                        foreach (ExceptionFilter filter in filters)
                        {
                            filter.Resole(expcontext);
                            if (expcontext.Result != null)
                            {
                                expcontext.Result.Execute(expcontext);
                                break;
                            }
                        }
                        if (expcontext.Result == null)
                        {
                            throw;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            });

            //授权认证
            app.Add(async(context, next) =>
            {
                var isAuthorized = true;
                var filters      = Configuration.Filters.Where(f => f.GetType().IsSubclassOf(typeof(AuthorizationFilter))).ToList();
                if (filters.Count > 0)
                {
                    foreach (AuthorizationFilter filter in filters)
                    {
                        filter.OnAuthorization(context);
                        if (context.Result != null)
                        {
                            isAuthorized = false;
                            context.Result.Execute(context);
                            break;
                        }
                    }
                    if (isAuthorized)
                    {
                        await next();
                    }
                }
                else
                {
                    await next();
                }
            });

            //行为过滤器
            app.Add(async(context, next) =>
            {
                await actionMiddleWareHandler.Execute(new ActionMiddWareContext {
                    Action = next, ActionContext = context
                });
            });

            //核心逻辑
            app.Add(async(context, next) =>
            {
                Console.WriteLine("core code");

                var factory        = ControllerFactoryProvider.GetControllerFactory();
                var controller     = factory.Create(context.Controller);
                controller.context = context.context;
                var param          = GetParameters(context.Action.GetParameters(), context);
                var result         = context.Action.Invoke(controller, param.Length > 0? param:null);
                //有返回值时
                if (result != null)
                {
                    //是 actionresult类型时的处理
                    if (result is IActionResult)
                    {
                        ((IActionResult)result).Execute(context);
                    }
                    //非 actionresult类型时当作对象返回json
                    else
                    {
                        new ContentResult(result.ToString()).Execute(context);
                    }
                }
            });
        }