国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

ASP.NET Core Middleware的實現(xiàn)方法詳解

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:34:43
文檔

ASP.NET Core Middleware的實現(xiàn)方法詳解

ASP.NET Core Middleware的實現(xiàn)方法詳解:概念 ASP.NET Core Middleware是在應(yīng)用程序處理管道pipeline中用于處理請求和操作響應(yīng)的組件。 每個組件: 在pipeline中判斷是否將請求傳遞給下一個組件 在處理管道的下個組件執(zhí)行之前和之后執(zhí)行一些工作, HttpContxt對象能跨域請求、響應(yīng)的執(zhí)行
推薦度:
導(dǎo)讀ASP.NET Core Middleware的實現(xiàn)方法詳解:概念 ASP.NET Core Middleware是在應(yīng)用程序處理管道pipeline中用于處理請求和操作響應(yīng)的組件。 每個組件: 在pipeline中判斷是否將請求傳遞給下一個組件 在處理管道的下個組件執(zhí)行之前和之后執(zhí)行一些工作, HttpContxt對象能跨域請求、響應(yīng)的執(zhí)行
//-----------------節(jié)選自 Microsoft.AspNetCore.Builder.UseMiddlewareExtensions------------------
 /// <summary>
 /// Adds a middleware type to the application's request pipeline.
 /// </summary>
 /// <typeparam name="TMiddleware">The middleware type.</typeparam>
 /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
 /// <param name="args">The arguments to pass to the middleware type instance's constructor.</param>
 /// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
 public static IApplicationBuilder UseMiddleware<TMiddleware>(this IApplicationBuilder app, params object[] args)
 {
 return app.UseMiddleware(typeof(TMiddleware), args);
 }
 /// <summary>
 /// Adds a middleware type to the application's request pipeline.
 /// </summary>
 /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
 /// <param name="middleware">The middleware type.</param>
 /// <param name="args">The arguments to pass to the middleware type instance's constructor.</param>
 /// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
 public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app, Type middleware, params object[] args)
 {
 if (typeof(IMiddleware).GetTypeInfo().IsAssignableFrom(middleware.GetTypeInfo()))
 {
 // IMiddleware doesn't support passing args directly since it's
 // activated from the container
 if (args.Length > 0)
 {
 throw new NotSupportedException(Resources.FormatException_UseMiddlewareExplicitArgumentsNotSupported(typeof(IMiddleware)));
 }
 
 return UseMiddlewareInterface(app, middleware);
 }
 
 var applicationServices = app.ApplicationServices;
 return app.Use(next =>
 {
 var methods = middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public); // 執(zhí)行委托名稱被限制為Invoke/InvokeAsync
 var invokeMethods = methods.Where(m =>
 string.Equals(m.Name, InvokeMethodName, StringComparison.Ordinal)
 || string.Equals(m.Name, InvokeAsyncMethodName, StringComparison.Ordinal)
 ).ToArray();
 
 if (invokeMethods.Length > 1)
 {
 throw new InvalidOperationException(Resources.FormatException_UseMiddleMutlipleInvokes(InvokeMethodName, InvokeAsyncMethodName));
 }
 
 if (invokeMethods.Length == 0)
 {
 throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNoInvokeMethod(InvokeMethodName, InvokeAsyncMethodName, middleware));
 }
 
 var methodInfo = invokeMethods[0];
 if (!typeof(Task).IsAssignableFrom(methodInfo.ReturnType))
 {
 throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNonTaskReturnType(InvokeMethodName, InvokeAsyncMethodName, nameof(Task)));
 }
 
 var parameters = methodInfo.GetParameters();
 if (parameters.Length == 0 || parameters[0].ParameterType != typeof(HttpContext))
 {
 throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNoParameters(InvokeMethodName, InvokeAsyncMethodName, nameof(HttpContext)));
 }
 
 var ctorArgs = new object[args.Length + 1];
 ctorArgs[0] = next;
 Array.Copy(args, 0, ctorArgs, 1, args.Length); // 通過反射形成中間件實例的時候,構(gòu)造函數(shù)第一個參數(shù)被指定為 下一個中間件的執(zhí)行委托 var instance = ActivatorUtilities.CreateInstance(app.ApplicationServices, middleware, ctorArgs);
 if (parameters.Length == 1)
 {
 return (RequestDelegate)methodInfo.CreateDelegate(typeof(RequestDelegate), instance);
 }
 // 當(dāng)前執(zhí)行委托除了可指定HttpContext參數(shù)以外, 還可以注入更多的依賴參數(shù) 
 var factory = Compile<object>(methodInfo, parameters);
 
 return context => 
 {
 var serviceProvider = context.RequestServices ?? applicationServices;
 if (serviceProvider == null)
 {
 throw new InvalidOperationException(Resources.FormatException_UseMiddlewareIServiceProviderNotAvailable(nameof(IServiceProvider)));
 }
 
 return factory(instance, context, serviceProvider);
 };
 });
 }
 
//-------------------節(jié)選自 Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder-------------------
private readonly IList<Func<RequestDelegate, RequestDelegate>> _components = new List<Func<RequestDelegate, RequestDelegate>>();
 
publicIApplicationBuilder Use(Func<RequestDelegate,RequestDelegate>middleware)
{
 this._components.Add(middleware);
 return this;
}
 
 public RequestDelegate Build()
 {
 RequestDelegate app = context =>
 {
 context.Response.StatusCode = 404;
 return Task.CompletedTask;
 };
 
 foreach (var component in _components.Reverse())
 {
 app = component(app);
 }
 
 return app;
 }

通過以上代碼我們可以看出:

  • 注冊中間件的過程實際上,是給一個 Type= List<Func<RequestDelegate, RequestDelegate>> 的容器依次添加元素的過程;
  • 容器中每個元素對應(yīng)每個中間件的行為委托Func<RequestDelegate, RequestDelegate> , 這個行為委托包含2個關(guān)鍵行為:輸入下一個中間件的執(zhí)行委托next:RequestDelegate, 完成當(dāng)前中間件的Invoke函數(shù): RequestDelegate;
  • 通過build方法完成前后中間件的鏈?zhǔn)絺髦店P(guān)系
  • 分析源碼:回答上面的問題:

    1. 使用反射構(gòu)造中間件的時候,第一個參數(shù)Array[0] 是下一個中間件的執(zhí)行委托
    2. 當(dāng)前中間件執(zhí)行委托 函數(shù)名稱被限制為: Invoke/InvokeAsync, 函數(shù)支持傳入除HttpContext之外的參數(shù)
    3. 按照代碼順序添加進(jìn)入 _components容器, 通過后一個中間件的執(zhí)行委托 -----(指向)----> 前一個中間件的輸入執(zhí)行委托建立鏈?zhǔn)疥P(guān)系。

    附:非標(biāo)準(zhǔn)中間件的用法

    短路中間件、 分叉中間件

    整個處理管道的形成,存在一些管道分叉或者臨時插入中間件的行為,一些重要方法可供使用

  • Use方法是一個注冊中間件的簡便寫法
  • Run方法是一個約定,一些中間件使用Run方法來完成管道的結(jié)尾
  • Map擴(kuò)展方法:請求滿足指定路徑,將會執(zhí)行分叉管道,強(qiáng)調(diào)滿足 path
  • MapWhen方法:HttpContext滿足條件,將會執(zhí)行分叉管道:
  • UseWhen方法:選擇性的注入中間件 
  • 總結(jié)

    聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    ASP.NET Core Middleware的實現(xiàn)方法詳解

    ASP.NET Core Middleware的實現(xiàn)方法詳解:概念 ASP.NET Core Middleware是在應(yīng)用程序處理管道pipeline中用于處理請求和操作響應(yīng)的組件。 每個組件: 在pipeline中判斷是否將請求傳遞給下一個組件 在處理管道的下個組件執(zhí)行之前和之后執(zhí)行一些工作, HttpContxt對象能跨域請求、響應(yīng)的執(zhí)行
    推薦度:
    標(biāo)簽: 方法 詳解 core
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 日韩电影一区二区 | 护士精品一区二区三区 | 天天爱夜夜操 | 欧美精品色精品一区二区三区 | 欧美亚洲网 | 亚洲综合日韩在线亚洲欧美专区 | 国产一区二区久久精品 | 青青国产成人久久91 | 国产一级久久久久久毛片 | 国产欧美日韩一区二区三区 | 国产精品亚洲精品不卡 | 国产精品美女久久久久 | 久久亚洲精品中文字幕60分钟 | 另类一区 | 九九国产精品九九 | 午夜视频在线播放 | 欧美日韩国产专区 | 俄罗斯女人禽交zozo | 国产成人久久精品二区三区 | 国产一区二区三区欧美 | 久久精品亚洲一区二区 | 日本vs欧美一区二区三区 | 欧美国产亚洲一区二区三区 | 亚洲好骚综合 | 国产欧美精品区一区二区三区 | 2020年国产高中毛片在线视频 | 国产成人免费高清激情明星 | 国产精品久久久久久一级毛片 | 久久国产一级毛片一区二区 | 欧美精品一区二区三区久久 | 国产激情在线观看 | 女人18毛片a级毛片一区②区 | 国产成人一区二区三区免费观看 | 久久亚洲精品国产精品婷婷 | 欧美在线观看视频免费 | 国产一级久久久久久毛片 | 精品国产乱码久久久久久浪潮 | 精品一区二区三区18 | 国产视频最新 | 欧美精品一区二区三区视频 | 国产一级特黄aaaa大片野外 |