zoukankan      html  css  js  c++  java
  • .Net Core 管道中的ConfigureServices 和Configure

    ConfigureServices    就是配置服务器的DI容器

    把需要的中间件等一些东西添加到DI容器   最后都是添加到IServiceCollection里面

    比如

                services.AddIdentityServer()
                    .AddDeveloperSigningCredential()
                    .AddInMemoryApiResources(Config.GetResource())
                    .AddInMemoryClients(Config.GetClients())
                    .AddTestUsers(Config.GetTestUsers())
                    .AddProfileService<ProfileService>()
                    .AddResourceOwnerValidator<LoginValidator>();

    对于.AddProfileService()     已经内置了一个默认实现IProfileService接口的一个类    默认会注入内置的(DefaultProfileServer)

    这样写了后  其实里面的实现就是   遇到IProfileService  实例化成自定义的类ProfileService    不使用内置的

    关于在Configure 中和添加中间件  

    https://www.cnblogs.com/RainingNight/p/middleware-in-asp-net-core.html

    一种是实现IMiddleware接口的中间件    实现了这个接口的中间件 直接通过反射调用InvokeAsync

    另外一种就是采用约定的方式实现中间件  采用这种方式添加的中间件采用了表达式的方式来调用Invoke或者InvokeAsync方法

    找这个中间件里面的方法   只要方法名为invoke或者invokeAsync的 就累加起来  如果数量大于1或者等于0就抛异常    这里就是为什么中间件的方法名一定要是Invoke

     然后在验证这个唯一存在的方法   方法的返回值如果不是Taks 继续抛异常

    继续验证这个方法的参数   如果参数的长度为0或者第1个参数的类型不是HttpContext  又抛异常

    如果当前中间件中的方法只存在一个 并且参数类型为HttpContext  把当前方法包在一个RequestDelegate委托中直接返回

      否则进行其他封装一下在返回

    注册中间件的对象 ApplicationBuild

            RequestDelegate Build();
            IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware);

    里面有两个最重要的方法  

    Use负责注册中间件   把中间件添加到_components对象中      在ApplicationBuild 中 有个_components属性  

    Build构建管道流程  把所有注册的中间件进行反转然后遍历     进行嵌套  这样后面注册的中间件就包到了最内层  执行的时候 需要从最外层开始  这样管道就封装完成

            public RequestDelegate Build()
            {
                RequestDelegate requestDelegate = (RequestDelegate)(context =>
                {
                    context.Response.StatusCode = 404;
                    return Task.CompletedTask;
                });
                foreach (Func<RequestDelegate, RequestDelegate> func in Enumerable.Reverse<Func<RequestDelegate, RequestDelegate>>((IEnumerable<Func<RequestDelegate, RequestDelegate>>)this._components))
                    requestDelegate = func(requestDelegate);
                return requestDelegate;
            }
  • 相关阅读:
    eclipse中的tomcat中修改部署项目的路径
    failed to load the jni shared library
    Can't call commit when autocommit=true问题的解决方法
    eclipse中快捷键的使用
    Eclipse导入的项目中发现包的形式变成了文件夹的形式,需要将文件夹的形式变成包
    '<>' operator is not allowed for source level below 1.7
    Tomcat中的server.xml配置详解
    shell 常用操作
    Mac 截图保存位置设置
    一周小结(2016-05-23~2016-05-27)
  • 原文地址:https://www.cnblogs.com/jiangchengbiao/p/10399591.html
Copyright © 2011-2022 走看看