zoukankan      html  css  js  c++  java
  • dotNetCore阅读源码-UseRouting及UseEndpoints内部

    UseRouting内部:

      public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
            {
    
                if (builder == null)
                {
                    throw new ArgumentNullException(nameof(builder));
                }
    
                //在调用UseEndpoint之前验证AddRouting服务是否已完成
          
    //我们使用RoutingMarkerService来确保是否添加了所有服务。
           //AddRouting服务在Program类的ConfigureWebHostDefaults调用后添加。

    VerifyRoutingServicesAreRegistered(builder); //VerifyRoutingServicesAreRegistered 内部 //if (app.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null) //{ // throw new InvalidOperationException(Resources.FormatUnableToFindServices( // nameof(IServiceCollection), // nameof(RoutingServiceCollectionExtensions.AddRouting), // "ConfigureServices(...)")); //} //初始化数据源 var endpointRouteBuilder = new DefaultEndpointRouteBuilder(builder); //DefaultEndpointRouteBuilder构造类内部 //public DefaultEndpointRouteBuilder(IApplicationBuilder applicationBuilder) //{ // ApplicationBuilder = applicationBuilder ?? throw new ArgumentNullException(nameof(applicationBuilder)); // DataSources = new List<EndpointDataSource>(); //} //EndpointRouteBuilder为字符串常量 __EndpointRouteBuilder //// builder.Properties["__EndpointRouteBuilder"] = endpointRouteBuilder; builder.Properties[EndpointRouteBuilder] = endpointRouteBuilder; //操作完成后对此实例引用 return builder.UseMiddleware<EndpointRoutingMiddleware>(endpointRouteBuilder); }

    UseEndpoints内部:

       public static IApplicationBuilder UseEndpoints(this IApplicationBuilder builder, Action<IEndpointRouteBuilder> configure)
            {
                if (builder == null)
                {
                    throw new ArgumentNullException(nameof(builder));
                }
    
                if (configure == null)
                {
                    throw new ArgumentNullException(nameof(configure));
                }
    
                
                //在调用UseEndpoint之前验证AddRouting服务是否已完成
           
    //我们使用RoutingMarkerService来确保是否添加了所有服务。
           //AddRouting服务在Program类的ConfigureWebHostDefaults调用后添加。

    //所以在调用UseEndpoint之前 必须要先调用UseRouting,否则报错。 VerifyRoutingServicesAreRegistered(builder); //VerifyRoutingServicesAreRegistered方法内部 //private static void VerifyRoutingServicesAreRegistered(IApplicationBuilder app) //{ // if (app.ApplicationServices.GetService(typeof(RoutingMarkerService)) == null) // { // throw new InvalidOperationException(Resources.FormatUnableToFindServices( // nameof(IServiceCollection), // nameof(RoutingServiceCollectionExtensions.AddRouting), // "ConfigureServices(...)")); // } //} //验证端点路由中间件是否已注册 VerifyEndpointRoutingMiddlewareIsRegistered(builder, out var endpointRouteBuilder); //VerifyEndpointRoutingMiddlewareIsRegistered方法内部 //private static void VerifyEndpointRoutingMiddlewareIsRegistered(IApplicationBuilder app, out DefaultEndpointRouteBuilder endpointRouteBuilder) //{ // if (!app.Properties.TryGetValue(EndpointRouteBuilder, out var obj)) // { // var message = // $"{nameof(EndpointRoutingMiddleware)} matches endpoints setup by {nameof(EndpointMiddleware)} and so must be added to the request " + // $"execution pipeline before {nameof(EndpointMiddleware)}. " + // $"Please add {nameof(EndpointRoutingMiddleware)} by calling '{nameof(IApplicationBuilder)}.{nameof(UseRouting)}' inside the call " + // $"to 'Configure(...)' in the application startup code."; // throw new InvalidOperationException(message); // } // //如果此处转换失败,程序会出错。 // endpointRouteBuilder = (DefaultEndpointRouteBuilder)obj; // // //此检查处理在两者之间调用Map或其他分叉管道的情况 // if (!object.ReferenceEquals(app, endpointRouteBuilder.ApplicationBuilder)) // { // var message = // $"The {nameof(EndpointRoutingMiddleware)} and {nameof(EndpointMiddleware)} must be added to the same {nameof(IApplicationBuilder)} instance. " + // $"To use Endpoint Routing with 'Map(...)', make sure to call '{nameof(IApplicationBuilder)}.{nameof(UseRouting)}' before " + // $"'{nameof(IApplicationBuilder)}.{nameof(UseEndpoints)}' for each branch of the middleware pipeline."; // throw new InvalidOperationException(message); // } //} configure(endpointRouteBuilder); //我们正在将数据源注册到一个全局集合中 //可用于发现端点或生成URL。 //每个中间件都有自己的数据源集合,所有这些数据源也 //被添加到全局集合中。 var routeOptions = builder.ApplicationServices.GetRequiredService<IOptions<RouteOptions>>(); foreach (var dataSource in endpointRouteBuilder.DataSources) { routeOptions.Value.EndpointDataSources.Add(dataSource); } //操作完成后对此实例引用 return builder.UseMiddleware<EndpointMiddleware>(); }
    好好学习,天天向上。
  • 相关阅读:
    python--模块与包
    内置函数 的总结
    迭代器 生成器 列表推导式 生成器表达式的一些总结
    函数的有用信息 带参数的装饰器 多个装饰器装饰一个函数
    函数名的应用(第一对象) 闭包 装饰器
    动态参数 名称空间 作用域 作用域链 加载顺序 函数的嵌套 global nonlocal 等的用法总结
    函数的初识 函数的返回值 参数
    文件操作 常用操作方法 文件的修改
    遍历字典的集中方法 集合的作用 以及增删查的方法
    计算机硬件的小知识
  • 原文地址:https://www.cnblogs.com/Zhengxue/p/13272428.html
Copyright © 2011-2022 走看看