Asp.NetコアのEndPointエンド解釈の原則をルーティング

I.背景

この記事書くつもりでIdentityserver4 記事を、彼らは上で自分自身を発見したEndPoint -ルーティングエンドポイントので放棄する最初のもの、非常に理解していないIdentityServer4 研究と執筆を、彼らはこのことについて、今日持っていた理由EndPoint の記事(エンドポイントルーティング)。

それともいつものように、GoogleとBaiduの検索エンジンの関連情報へのアクセスを、だけでなく、私が読んオープンAsp.netコア3.1ソースコードのコンピュータの電源を開いて、そして最終的に私の練習中とするテストのEndPoint 異なった理解を持っている、と言ってここでは、Microsoftの設計モデルAsp.netコア3.xのフレームワークのためのパイプラインでより多くの賞賛にあります。

私は、次の質問を提起したいと思います:

1. Webアプリケーションのアドレスにアクセスすると、Asp.Netコアが実行する方法であるController にActionそれ?2. Endpoint 通常の経路とし、関係の種類がありますか?3 UseRouing() 、UseAuthorization()UserEndpoints() それはどのようなこれら三つのミドルウェアとの関係?4.利用がどのようにEndpoint 独自のミドルウェア、およびエンドポイントのアプリケーションのシナリオを書くために(限られた時間のために、次回の株式併合)

第二に、ソースコードの疑問を読みます

Startup コード

で、私たちは最初に見てStartup、次のようにコードの簡易版では、コードは次のとおりです。

public void ConfigureServices(IServiceCollection services){        services.AddControllers();}public void Configure(IApplicationBuilder app, IWebHostEnvironment env){        app.UseRouting();        app.UseAuthorization();        app.UseEndpoints(endpoints =>        {              endpoints.MapControllers();        });}

プログラムの起動フェーズ:

ステップ1:実行services.AddControllers()Controllerを容器に登録されたコアサービス

第二段階:app.UseRoutingの実装では、()になるEndpointRoutingMiddlewareHTTPパイプラインへの登録ミドルウェア

第三段階:app.UseAuthorizationの実装では、()であろうAuthorizationMiddlewareHTTPパイプラインに登録ミドルウェア

第四段階:行うapp.UseEndpoints(encpoints => endpoints.MapControllers() )二つの主要な機能を有する:コールendpoints.MapControllers()全ての番組の定義されたセットにするControllerActionいずれかを変換するEndPointルーティング構成オブジェクトミドルウェア内RouteOptionsEndpointMiddleware中央HTTPパイプラインへの登録

app.UseRouting() 次のようにソースコードは次のとおりです。

public static IApplicationBuilder UseRouting(this IApplicationBuilder builder){       if (builder == null)       {             throw new ArgumentNullException(nameof(builder));       }       VerifyRoutingServicesAreRegistered(builder);       var endpointRouteBuilder = new DefaultEndpointRouteBuilder(builder);       builder.Properties[EndpointRouteBuilder] = endpointRouteBuilder;       return builder.UseMiddleware<EndpointRoutingMiddleware>(endpointRouteBuilder); }

EndpointRoutingMiddleware 次のようにミドルウェアのコードは次のとおりです。

internal sealed class EndpointRoutingMiddleware    {        private const string DiagnosticsEndpointMatchedKey = "Microsoft.AspNetCore.Routing.EndpointMatched";        private readonly MatcherFactory _matcherFactory;        private readonly ILogger _logger;        private readonly EndpointDataSource _endpointDataSource;        private readonly DiagnosticListener _diagnosticListener;        private readonly RequestDelegate _next;        private Task<Matcher> _initializationTask;        public EndpointRoutingMiddleware(            MatcherFactory matcherFactory,            ILogger<EndpointRoutingMiddleware> logger,            IEndpointRouteBuilder endpointRouteBuilder,            DiagnosticListener diagnosticListener,            RequestDelegate next)        {            if (endpointRouteBuilder == null)            {                throw new ArgumentNullException(nameof(endpointRouteBuilder));            }            _matcherFactory = matcherFactory ?? throw new ArgumentNullException(nameof(matcherFactory));            _logger = logger ?? throw new ArgumentNullException(nameof(logger));            _diagnosticListener = diagnosticListener ?? throw new ArgumentNullException(nameof(diagnosticListener));            _next = next ?? throw new ArgumentNullException(nameof(next));            _endpointDataSource = new CompositeEndpointDataSource(endpointRouteBuilder.DataSources);        }        public Task Invoke(HttpContext httpContext)        {            // There's already an endpoint, skip maching completely            var endpoint = httpContext.GetEndpoint();            if (endpoint != null)            {                Log.MatchSkipped(_logger, endpoint);                return _next(httpContext);            }            // There's an inherent race condition between waiting for init and accessing the matcher            // this is OK because once `_matcher` is initialized, it will not be set to null again.            var matcherTask = InitializeAsync();            if (!matcherTask.IsCompletedSuccessfully)            {                return AwaitMatcher(this, httpContext, matcherTask);            }            var matchTask = matcherTask.Result.MatchAsync(httpContext);            if (!matchTask.IsCompletedSuccessfully)            {                return AwaitMatch(this, httpContext, matchTask);            }            return SetRoutingAndContinue(httpContext);            // Awaited fallbacks for when the Tasks do not synchronously complete            static async Task AwaitMatcher(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task<Matcher> matcherTask)            {                var matcher = await matcherTask;                await matcher.MatchAsync(httpContext);                await middleware.SetRoutingAndContinue(httpContext);            }            static async Task AwaitMatch(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask)            {                await matchTask;                await middleware.SetRoutingAndContinue(httpContext);            }        }        [MethodImpl(MethodImplOptions.AggressiveInlining)]        private Task SetRoutingAndContinue(HttpContext httpContext)        {            // If there was no mutation of the endpoint then log failure            var endpoint = httpContext.GetEndpoint();            if (endpoint == null)            {                Log.MatchFailure(_logger);            }            else            {                // Raise an event if the route matched                if (_diagnosticListener.IsEnabled() && _diagnosticListener.IsEnabled(DiagnosticsEndpointMatchedKey))                {                    // We're just going to send the HttpContext since it has all of the relevant information                    _diagnosticListener.Write(DiagnosticsEndpointMatchedKey, httpContext);                }                Log.MatchSuccess(_logger, endpoint);            }            return _next(httpContext);        }        // Initialization is async to avoid blocking threads while reflection and things        // of that nature take place.        //        // We've seen cases where startup is very slow if we  allow multiple threads to race        // while initializing the set of endpoints/routes. Doing CPU intensive work is a        // blocking operation if you have a low core count and enough work to do.        private Task<Matcher> InitializeAsync()        {            var initializationTask = _initializationTask;            if (initializationTask != null)            {                return initializationTask;            }            return InitializeCoreAsync();        }        private Task<Matcher> InitializeCoreAsync()        {            var initialization = new TaskCompletionSource<Matcher>(TaskCreationOptions.RunContinuationsAsynchronously);            var initializationTask = Interlocked.CompareExchange(ref _initializationTask, initialization.Task, null);            if (initializationTask != null)            {                // This thread lost the race, join the existing task.                return initializationTask;            }            // This thread won the race, do the initialization.            try            {                var matcher = _matcherFactory.CreateMatcher(_endpointDataSource);                // Now replace the initialization task with one created with the default execution context.                // This is important because capturing the execution context will leak memory in ASP.NET Core.                using (ExecutionContext.SuppressFlow())                {                    _initializationTask = Task.FromResult(matcher);                }                // Complete the task, this will unblock any requests that came in while initializing.                initialization.SetResult(matcher);                return initialization.Task;            }            catch (Exception ex)            {                // Allow initialization to occur again. Since DataSources can change, it's possible                // for the developer to correct the data causing the failure.                _initializationTask = null;                // Complete the task, this will throw for any requests that came in while initializing.                initialization.SetException(ex);                return initialization.Task;            }        }        private static class Log        {            private static readonly Action<ILogger, string, Exception> _matchSuccess = LoggerMessage.Define<string>(                LogLevel.Debug,                new EventId(1, "MatchSuccess"),                "Request matched endpoint '{EndpointName}'");            private static readonly Action<ILogger, Exception> _matchFailure = LoggerMessage.Define(                LogLevel.Debug,                new EventId(2, "MatchFailure"),                "Request did not match any endpoints");            private static readonly Action<ILogger, string, Exception> _matchingSkipped = LoggerMessage.Define<string>(                LogLevel.Debug,                new EventId(3, "MatchingSkipped"),                "Endpoint '{EndpointName}' already set, skipping route matching.");            public static void MatchSuccess(ILogger logger, Endpoint endpoint)            {                _matchSuccess(logger, endpoint.DisplayName, null);            }            public static void MatchFailure(ILogger logger)            {                _matchFailure(logger, null);            }            public static void MatchSkipped(ILogger logger, Endpoint endpoint)            {                _matchingSkipped(logger, endpoint.DisplayName, null);            }        }    }

私たちは、ソースコードからそれを見ることができるEndpointRoutingMiddlewareミドルウェアは最初に作成matcherした後、呼び出しmatcher.MatchAsync(httpContext)エンドポイントを見つけること、そして最後によるhttpContext.GetEndpoint()試合は右に検証されたかどうかEndpointとミドルウェアが続く次を渡ります!

app.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));       }       VerifyRoutingServicesAreRegistered(builder);       VerifyEndpointRoutingMiddlewareIsRegistered(builder, out var endpointRouteBuilder);       configure(endpointRouteBuilder);       // Yes, this mutates an IOptions. We're registering data sources in a global collection which       // can be used for discovery of endpoints or URL generation.       //       // Each middleware gets its own collection of data sources, and all of those data sources also       // get added to a global collection.       var routeOptions = builder.ApplicationServices.GetRequiredService<IOptions<RouteOptions>>();        foreach (var dataSource in endpointRouteBuilder.DataSources)        {              routeOptions.Value.EndpointDataSources.Add(dataSource);        }        return builder.UseMiddleware<EndpointMiddleware>();}internal class DefaultEndpointRouteBuilder : IEndpointRouteBuilder{        public DefaultEndpointRouteBuilder(IApplicationBuilder applicationBuilder)        {            ApplicationBuilder = applicationBuilder ?? throw new ArgumentNullException(nameof(applicationBuilder));            DataSources = new List<EndpointDataSource>();        }        public IApplicationBuilder ApplicationBuilder { get; }        public IApplicationBuilder CreateApplicationBuilder() => ApplicationBuilder.New();        public ICollection<EndpointDataSource> DataSources { get; }        public IServiceProvider ServiceProvider => ApplicationBuilder.ApplicationServices;    }

コードは、構築DefaultEndpointRouteBuilder に格納されているビルダオブジェクト、ルーティングエンドポイントEndpointデータがルートセットに記憶されている間ターミネーター;トランザクションのセットのrouteOptions レジスタおよびEndpointMiddleware HTTP中間導管と、  Endpointオブジェクトコード次のように

/// <summary>/// Represents a logical endpoint in an application./// </summary>public class Endpoint{        /// <summary>        /// Creates a new instance of <see cref="Endpoint"/>.        /// </summary>        /// <param name="requestDelegate">The delegate used to process requests for the endpoint.</param>        /// <param name="metadata">        /// The endpoint <see cref="EndpointMetadataCollection"/>. May be null.        /// </param>        /// <param name="displayName">        /// The informational display name of the endpoint. May be null.        /// </param>        public Endpoint(            RequestDelegate requestDelegate,            EndpointMetadataCollection metadata,            string displayName)        {            // All are allowed to be null            RequestDelegate = requestDelegate;            Metadata = metadata ?? EndpointMetadataCollection.Empty;            DisplayName = displayName;        }        /// <summary>        /// Gets the informational display name of this endpoint.        /// </summary>        public string DisplayName { get; }        /// <summary>        /// Gets the collection of metadata associated with this endpoint.        /// </summary>        public EndpointMetadataCollection Metadata { get; }        /// <summary>        /// Gets the delegate used to process requests for the endpoint.        /// </summary>        public RequestDelegate RequestDelegate { get; }        public override string ToString() => DisplayName ?? base.ToString();    }

Endpoint 二つの重要なオブジェクトコードタイプ属性がありますEndpointMetadataCollection 種類とRequestDelegate

   EndpointMetadataCollection:記憶Controller とActionを含む要素の関連するセット 特性データ          :アクション、すなわち記憶された手数料を、このアクションコントローラあたりの方法でありますActionAttributeRequestDelegate

そしてバックに行くEndpointMiddleware ミドルウェアおよびコアコード、EndpointMiddleware 大型のコアコードはのエンドポイントの実装であるRequestDelegate 委員会、すなわち 実行。ControllerAction

public Task Invoke(HttpContext httpContext){        var endpoint = httpContext.GetEndpoint();        if (endpoint?.RequestDelegate != null)        {             if (!_routeOptions.SuppressCheckForUnhandledSecurityMetadata)             {                 if (endpoint.Metadata.GetMetadata<IAuthorizeData>() != null &&                        !httpContext.Items.ContainsKey(AuthorizationMiddlewareInvokedKey))                  {                      ThrowMissingAuthMiddlewareException(endpoint);                  }                  if (endpoint.Metadata.GetMetadata<ICorsMetadata>() != null &&                       !httpContext.Items.ContainsKey(CorsMiddlewareInvokedKey))                   {                       ThrowMissingCorsMiddlewareException(endpoint);                   }             }            Log.ExecutingEndpoint(_logger, endpoint);            try            {                 var requestTask = endpoint.RequestDelegate(httpContext);                 if (!requestTask.IsCompletedSuccessfully)                 {                     return AwaitRequestTask(endpoint, requestTask, _logger);                 }            }            catch (Exception exception)            {                 Log.ExecutedEndpoint(_logger, endpoint);                 return Task.FromException(exception);            }            Log.ExecutedEndpoint(_logger, endpoint);            return Task.CompletedTask;        }        return _next(httpContext);        static async Task AwaitRequestTask(Endpoint endpoint, Task requestTask, ILogger logger)         {             try             {                 await requestTask;             }             finally             {                 Log.ExecutedEndpoint(logger, endpoint);             }         }}

疑問が回答します:

1. Webアプリケーションのアドレスにアクセスすると、Asp.Netコアが実行する方法であるController にActionそれ?

A:プログラムが起動コントローラに保存されているアクションマッピングがすべてになりますrouteOptions コレクションの、アクションにマップされたEndpointターミネーターRequestDelegate によって最終的に財産を委託し、UseEndPoints 追加するEndpointMiddleware ミドルウェアながら、実行ミドルウェアをEndpoint ターミネータールートが通じていますRouing試合をルーティングした後。

2.  EndPoint 通常の経路とし、関係の種類がありますか?

Ednpoint ターミネーター経路は方法ルーティングメッセージのすべての要素を含んでいるルートマップ変換後の通常の経路によって委託されるEndpointMetadataCollection とRequestDelegate デリゲート。

UseRouing() 、UseAuthorization()UseEndpoints() それはどのようなこれら三つのミドルウェアとの関係?

UseRouing ミドルウェアは、主一致ターミネータールートを見つけ、一致をルーティングされEndpoint 、UseEndpoints メインのためのミドルウェアUseRouing のルートに操作実行ミドルウェアマッチデリゲート方法。 UseAuthorization ミドルウェアは、目的とする  UseRouing ミドルウェアが傍受許可検証動作を行うルーティングマッチングのために、次の中間体を介して行われるUseEndpoints()、特定の関係を以下のフローチャートに見ることができます。

上記のフローチャートではいくつかの部分、主に強調表示された関係UseRouing、UseAuthorization、UseEndpoint 3つのミドルウェアを省略します。

309元記事公開 ウォンの賞賛119 ビューに46万+を

おすすめ

転載: blog.csdn.net/u011966339/article/details/104747816