1 Star 1 Fork 0

defa/aspnetcore_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
defa hello 147584b 7年前
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

aspnetcore_study

1. MiddleWare

Demo1 模拟中间件的实现方式

    class Program
    {
        static void Main(string[] args)
        {
            IApplicationBuilder appBuidler=new ApplicationBuilder(null);
            appBuidler.UseMiddleware<LogMiddleWare>();
            appBuidler.Use(async (context,next)=>{
               await Console.Out.WriteLineAsync("Call before next.");
               await next(context);
               await Console.Out.WriteLineAsync("Call after next.");
            });

            appBuidler.Run(async (context)=>{
               await Console.Out.WriteLineAsync("hello world!");
            });

            var app=appBuidler.Build();

            var response=app.Invoke(new RequestContext(){ Url="http://localhost"});
            response.Wait();
            Console.ReadLine();
        }
    }

    //LogMiddleWare类
    public class LogMiddleWare
    {
        private readonly RequestDelegate _next;
        public LogMiddleWare(RequestDelegate next)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            _next = next;
        }
        
        public async Task Invoke(RequestContext context){
            await Console.Out.WriteLineAsync("before log!");
            await _next(context);
            await Console.Out.WriteLineAsync("after log!");
        }
    }
//RequestContext 类模拟了请求上下文
public class RequestContext
{
   public string Url{ get; set;}
}
//RequestDelegate模拟了这一类请求的委托
public delegate Task RequestDelegate(RequestContext context);
//IApplicationBuilder用来创建注册委托和并创建应用
public interface IApplicationBuilder
{
    IServiceProvider ApplicationServices{get;set;}
    void Add(Func<RequestDelegate,RequestDelegate> middleware);
    RequestDelegate Build();
}
//ApplicationBuilderExtension对IApplication进行了一些方法的扩展,UseMiddleWare,Use,Run等方法
void UseMiddleware<T>(this IApplicationBuilder builder, params object[] args)
void Use(this IApplicationBuilder builder,Func<RequestContext,RequestDelegate,Task> func)
void Run(this IApplicationBuilder builder,Func<RequestContext,Task> last)

Demo2 模拟 WebHostBuilder

static void Main(string[] args)
{
    var host = new WebHostBuilder()
       .UseTestServer()
       .Configure(app =>
       {
           app.UseMiddleware<LogMiddleWare>();
           app.Use(async (context, next) => {
               await Console.Out.WriteLineAsync(context.Url + "Call before next.");
               await next(context);
               await Console.Out.WriteLineAsync(context.Url + "Call after next.");
           });
           app.Run(async (context) => {
               await Console.Out.WriteLineAsync("hello world!");
           });
       })
       .Build()
    host.Run();
    Console.ReadLine();
}

空文件

简介

取消

发行版

暂无发行版

贡献者 (1)

全部

近期动态

7年多前推送了新的提交到 master 分支,00075ae...147584b
7年多前推送了新的提交到 master 分支,7d6b0b9...00075ae
7年多前推送了新的提交到 master 分支,ee8f096...7d6b0b9
7年多前推送了新的提交到 master 分支,48535c6...ee8f096
7年多前推送了新的提交到 master 分支,9d1f2a5...48535c6
加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/defa/aspnetcore_study.git
git@gitee.com:defa/aspnetcore_study.git
defa
aspnetcore_study
aspnetcore_study
master

搜索帮助