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>(); }
    好好学习,天天向上。
  • 相关阅读:
    LIS例题
    基数排序板子
    lower_bound和upper_bound在刷leetcode的时候...
    leetcode1081/316 求字典序最小的包含所有出现字符一次的子序列
    PHP 求多个数组的笛卡尔积,适用于求商品规格组合 【深度优先搜索】【原创】
    PHP 求多个数组的笛卡尔积,适用于求商品规格组合【原创】
    Spring 中注入 properties 中的值
    Java 枚举活用
    Intellij IDEA 快捷键整理(TonyCody)
    WIN API -- 2.Hello World
  • 原文地址:https://www.cnblogs.com/Zhengxue/p/13272428.html
Copyright © 2011-2022 走看看