zoukankan      html  css  js  c++  java
  • Nancy异步用法

    个人笔记,记录Nancy异步用法

    基类,所有请求都将首先执行该类,并执行Before事件
    namespace CxyAdvert.Base
    {
        public class BaseNancyModel : Nancy.NancyModule
        {
            public BaseNancyModel()
            {
                Get["/"] = param =>
                {
                    return "ADVERT SYSTEM";
                };
            }
            public BaseNancyModel(string path)
                : base(path)
            {
                Before += async (context, token) =>
                {
                    return await VerifyAuth(context);
                };
    
                //After += async (context, token) =>
                //{
                //    await VerifyAuth(context);
                //};
            }

         /// <summary> /// Before执行的函数,返回null将继续执行路由指定接口,返回Response将结束并返回 /// </summary> /// <param name="nancyContext"></param> /// <returns></returns> private async Task<Response> VerifyAuth(Nancy.NancyContext nancyContext) { try { var httpMethod = nancyContext.Request.Method; if (httpMethod.ToUpper() == "POST") { var requestParms = HttpHelper.GetPostValue(nancyContext.Request.Body); if (requestParms == "") { return Response.AsJson(new ResultMsg() { code = -2, errormsg = "参数值不能为空" }); } else { } } return null; } catch (Exception error) { Logger.Error("BaseNancyModel ERROR:" + error); return Response.AsJson(new ResultMsg() { code = -2, errormsg = error.Message }); } }      public object GetEncryptValue(object msg) { var strJson = JsonConvert.SerializeObject(msg, StringFormat.jsonSerializerSettings); return strJson ; } } }
    
    
    接口类,所有接口类继承基类BaseNancyModel,GetEncryptValue函数实现于基类BaseNancyModel中
    namespace CxyAdvert
    {
        public class AdvertSericeApi : BaseNancyModel
        {
            public AdvertSericeApi() :
                base("/cxy/api/advert/")
            {
                IAdvertSerice service = new AdvertSerice();
    
                Post["getshowadverts", true] = async (param, token) =>
                {
                    return GetEncryptValue(await service.GetShowAdverts(BaseInputParam, ThridUser));
                };
    
                Post["getadverts", true] = async (param, token) =>
                {
                    return GetEncryptValue(await service.GetAdverts(BaseInputParam, ThridUser));
                };
            }
        }
    }
    
    
    


  • 相关阅读:
    Mandala Coloring Book Game ver 1.2
    Racing Game Starter Kit 1.1.0a
    Unity3D射击项目源码
    U3D第一人称解谜游戏完整项目工程源码
    uMMORPG Remastered v2.25
    U3D网球完整项目源码 v1.1
    Unity3D果汁店模拟经营完整项目工程源码
    3D坦克大战游戏项目源码
    周民强实变函数论第3版勘误14个
    竞赛2021年浙江省高等数学(微积分)竞赛数学类与工科类试题2页pdf
  • 原文地址:https://www.cnblogs.com/pudefu/p/7527403.html
Copyright © 2011-2022 走看看