ASP.NET Core绠¢亾娣卞害鍓栨瀽锛?锛夛細绠¢亾鏄浣曞鐞咹TTP璇锋眰鐨勶紵

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET Core绠¢亾娣卞害鍓栨瀽锛?锛夛細绠¢亾鏄浣曞鐞咹TTP璇锋眰鐨勶紵相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

鎴戜滑鐭ラ亾ASP.NET Core璇锋眰澶勭悊绠¢亾鐢变竴涓湇鍔″櫒鍜屼竴缁勬湁搴忕殑涓棿浠剁粍鎴愶紝鎵€浠ヤ粠鎬讳綋璁捐鏉ヨ鏄潪甯哥畝鍗曠殑锛屼絾鏄氨鍏蜂綋鐨勫疄鐜版潵璇达紝鐢变簬鍏朵腑娑夊強寰堝瀵硅薄鐨勪氦浜掞紝鎴戞兂寰堝皯浜鸿兘澶熷湴鎶婂畠寮勬竻妤氥€備负浜嗚璇昏€呮湅鍙嬩滑鑳藉鏇村姞瀹规槗鍦扮悊瑙g閬撳鐞咹TTP璇锋眰鐨勬€讳綋娴佺▼锛屾垜浠牴鎹湡瀹炵閬撶殑瀹炵幇鍘熺悊鍐嶉€犱簡涓€涓?#8220;妯℃嫙绠¢亾”骞跺湪姝ょ閬撲笂寮€鍙戜簡涓€涓?a href="https://www.cnblogs.com/artech/p/mini-asp-net-core-pipeline.html">鍙戝竷鍥剧墖鐨勫簲鐢?/a>锛岃繖绡囨枃绔犳棬鍦ㄤ负浣犺杩扮閬撴槸濡備綍澶勭悊HTTP璇锋眰鐨?/p>

鐩綍
涓€銆丠ttpApplication
    FeatureCollection
    HostingApplication
浜屻€丠ttpContext
    DefaultHttpContext
    HostingApplication
    灏忕粨
涓夈€佹湇鍔″櫒
    HttpListenerServer
    ServerFactory
    灏忕粨

涓€銆丠ttpApplication

鎶€鏈垎浜? title=

ASP.NET Core璇锋眰澶勭悊绠¢亾鐢变竴涓湇鍔″櫒鍜屼竴缁勬湁搴忔帓鍒楃殑涓棿浠剁粍鍚堣€屾垚銆傛垜浠彲浠ュ湪杩欏熀纭€涓婁綔杩涗竴姝ヤ釜鎶借薄锛屽皢鍚庤€呮娊璞℃垚涓€涓狧ttpApplication瀵硅薄锛岄偅涔堣绠¢亾灏辨垚浜嗕竴涓猄erver鍜孒ttpApplication鐨勭患鍚堜綋锛堝鍥?鎵€绀猴級銆係erver浼氬皢鎺ユ敹鍒扮殑HTTP璇锋眰杞彂缁橦ttpApplication瀵硅薄锛屽悗鑰呬細閽堝褰撳墠璇锋眰鍒涘缓涓€涓笂涓嬫枃锛屽苟鍦ㄦ涓婁笅鏂囦腑澶勭悊璇锋眰锛岃姹傚鐞嗗畬鎴愬苟瀹屾垚鍝嶅簲涔嬪悗HttpApplication浼氬姝や笂涓嬫枃瀹炴柦鍥炴敹閲婃斁澶勭悊銆?

鎴戜滑閫氳繃鍏锋湁濡備笅瀹氫箟鐨処HttpApplication<TContext>鏉ヨ〃绀轰笂杩扮殑杩欎釜HttpApplication锛屾硾鍨嬪弬鏁癟Context浠h〃杩欎釜娉涘寲鐨勪笂涓嬫枃绫诲瀷銆備竴涓狧ttpApplication瀵硅薄鍦ㄦ帴鏀跺埌Server杞彂鐨勮姹備箣鍚庨渶瑕佸畬鎴愪笁椤瑰熀鏈殑鎿嶄綔锛屽嵆鍒涘缓涓婁笅鏂囥€佸湪涓婁笅鏂囦腑澶勭悊璇锋眰浠ュ強璇锋眰澶勭悊瀹屾垚涔嬪悗閲婃斁涓婁笅鏂囷紝杩欎笁涓熀鏈搷浣滄悶濂介€氳繃瀵瑰簲鐨勪笁涓柟娉曟潵瀹屾垚銆?

   1: public interface IHttpApplication<TContext>
   2: {
   3:     TContext CreateContext(IFeatureCollection contextFeatures); 
   4:     Task ProcessRequestAsync(TContext context);
   5:     void DisposeContext(TContext context, Exception exception);
   6: }

FeatureCollection

鐢ㄤ簬鍒涘缓涓婁笅鏂囩殑CreateContext鏂规硶鍏锋湁涓€涓被鍨嬩负IFeatureCollection鎺ュ彛鐨勫弬鏁般€傞【鍚嶆€濅箟锛岃繖涓帴鍙g敤浜庢弿杩版煇涓璞℃墍鍏锋湁鐨勪竴缁勭壒鎬э紝鎴戜滑鍙互灏嗗畠瑙嗕负涓€涓瓧鍏革紝瀹僔aue浠h〃鐗规€ф弿杩板璞★紝Key鍒欒〃绀鸿瀵硅薄鐨勬敞鍐岀被鍨嬶紙鍙互鏄壒鎬ф弿杩板璞$殑鐪熷疄绫诲瀷鎴栬€呯湡瀹炵被鍨嬬殑鍩虹被鎴栬€呭疄鐜扮殑鎺ュ彛锛夈€傛垜浠彲浠ヨ皟鐢℅et鏂规硶鏍规嵁绫诲瀷寰楀埌璁剧疆鐨勭壒鎬ф弿杩板璞★紝鍚庤€呯殑璁剧疆鍒欓€氳繃Set鏂规硶鏉ュ畬鎴愩€侳eatureCollection绫诲瀷閲囩敤鏈€绠€鍗曠殑鏂瑰紡瀹炵幇浜嗚繖涓帴鍙c€?

   1: public interface IFeatureCollection
   2: {
   3:     TFeature Get<TFeature>();
   4:     void Set<TFeature>(TFeature instance);
   5: }
   6:  
   7: public class FeatureCollection : IFeatureCollection
   8: {
   9:     private ConcurrentDictionary<Type, object> features = new ConcurrentDictionary<Type, object>();
  10:  
  11:     public TFeature Get<TFeature>()
  12:     {
  13:         object feature;
  14:         return features.TryGetValue(typeof(TFeature), out feature) 
  15:             ? (TFeature)feature 
  16:             : default(TFeature);
  17:     }
  18:  
  19:     public void Set<TFeature>(TFeature instance)
  20:     {
  21:         features[typeof(TFeature)] = instance;
  22:     }
  23: }

HostingApplication

绠¢亾妯″紡閲囩敤鐨凥ttpApplication鏄竴涓被鍨嬩负 HostingApplication鐨勫璞°€傚涓嬮潰鐨勪唬鐮佺墖娈垫墍绀猴紝杩欎釜绫诲瀷瀹炵幇浜嗘帴鍙HttpApplication<Context>锛屾硾鍨嬪弬鏁癈ontext鏄竴涓拡瀵瑰綋鍓嶈姹傜殑涓婁笅鏂囧璞°€備竴涓狢ontext鏄涓€涓狧ttpContext鐨勫皝瑁咃紝鍚庤€呮槸鐪熸鎻忚堪褰撳墠HTTP璇锋眰鐨勪笂涓嬫枃銆傞櫎姝や箣澶栵紝Context杩樺叿鏈塖cope鍜孲tartTimestamp涓や釜灞炴€э紝涓よ€呬笌鏃ュ織璁板綍鍜屼簨浠惰拷韪湁鍏筹紝鍓嶈€呰鐢ㄦ潵灏嗛拡瀵瑰悓涓€璇锋眰鐨勫娆℃棩蹇楄褰曞叧鑱斿埌鍚屼竴涓笂涓嬫枃鑼冨洿锛堝嵆Logger鐨凚eginScope鏂规硶鐨勮繑鍥炲€硷級锛屽悗鑰呰〃绀哄紑濮嬪鐞嗚姹傜殑鏃堕棿鎴筹紝濡傛灉鍦ㄥ畬鎴愯姹傚鐞嗙殑鏃跺€欒褰曚笅褰撳墠鐨勬椂闂存埑锛屾垜浠氨鍙互璁$畻鍑烘暣涓姹傚鐞嗘墍鑺辫垂鐨勬椂闂淬€?

   1: public class HostingApplication : IHttpApplication<Context>
   2: {
   3:     //鐪佺暐鎴愬憳瀹氫箟
   4: }
   5:  
   6: public class Context
   7: {
   8:     public HttpContext     HttpContext { get; set; }
   9:     public IDisposable     Scope { get; set; }
  10:     public long            StartTimestamp { get; set; }
  11: }

鎶€鏈垎浜? title=鍙冲浘鎵€绀虹殑UML浣撶幇浜嗕笌HttpApplication鐩稿叧鐨勬牳蹇冩帴鍙?绫诲瀷涔嬮棿鐨勫叧绯汇€傛€诲緱鏉ヨ锛岄€氳繃娉涘瀷鎺ュ彛IHttpApplication<TContext>琛ㄧずHttpApplication鏄娉ㄥ唽鐨勪腑闂翠欢鐨勫皝瑁呫€侶ttpApplication鍦ㄤ竴涓嚜琛屽垱寤虹殑涓婁笅鏂囦腑瀹屾垚瀵规湇鍔″櫒鎺ユ敹璇锋眰鐨勫鐞嗭紝鑰屼笂涓嬫枃鏍规嵁琛ㄨ堪鍘熷HTTP涓婁笅鏂囩殑鐗规€ч泦鍚堟潵鍒涘缓锛岃繖涓壒鎬ч泦鍚堥€氳繃鎺ュ彛IFeatureCollection鏉ヨ〃绀猴紝FeatureCollection鏄鎺ュ彛鐨勯粯璁ゅ疄鐜拌€呫€侫SP.NET Core 榛樿浣跨敤鐨凥ttpApplication鏄竴涓狧ostingApplication瀵硅薄锛屽畠鍒涘缓鐨勪笂涓嬫枃绫诲瀷涓篊ontext锛屼竴涓狢ontext瀵硅薄鏄涓€涓狧ttpContext鍜屽叾浠栦笌鏃ュ織鐩稿叧涓婁笅鏂囦俊鎭殑灏佽銆?

浜屻€丠ttpContext

鐢ㄦ潵鎻忚堪褰撳墠HTTP璇锋眰鐨勪笂涓嬫枃鐨凥ttpContext瀵逛簬ASP .NET Core璇锋眰澶勭悊绠¢亾鏉ヨ鏄竴涓潪甯搁噸瑕佺殑瀵硅薄锛屾垜浠笉浠呬粎鍙互鍒╃敤瀹冭幏鍙栧綋鍓嶈姹傜殑鎵€鏈夌粏鑺傦紝杩樺彲浠ョ洿鎺ュ埄鐢ㄥ畠瀹屾垚瀵硅姹傜殑鍝嶅簲銆侶ttpContext鏄竴涓娊璞$被锛屽緢澶氱敤浜庢弿杩板綋鍓岺TTP璇锋眰鐨勪笂涓嬫枃淇℃伅鐨勫睘鎬ц瀹氫箟鍏朵腑锛屽浜庤繖涓ā鎷熺閬撴潵璇达紝鎴戜滑浠呬粎淇濈暀浜嗕袱涓牳蹇冪殑灞炴€э紝鍗宠〃绀鸿姹傚拰鍝嶅簲鐨凴equst鍜孯esponse灞炴€с€?

   1: public abstract class HttpContext
   2: {
   3:     public abstract HttpRequest     Request { get; }
   4:     public abstract HttpResponse    Response { get; }
   5: }

琛ㄧず璇锋眰鍜屽搷搴旂殑HttpRequest鍜孒ttpResponse鍚屾牱鏄袱涓娊璞$被锛岀畝鍗曡捣瑙侊紝鎴戜滑浠呬粎淇濈暀灏戞暟鍑犱釜涓庢紨绀哄疄渚嬬浉鍏崇殑灞炴€ф垚鍛樸€傚涓嬮潰鐨勪唬鐮佺墖娈垫墍绀猴紝浠呬粎涓篐ttpRequest淇濈暀浜嗚〃绀哄綋鍓嶈姹傚湴鍧€鐨刄rl灞炴€с€傚浜嶩ttpResponse鏉ヨ锛屾垜浠繚鐣欎簡涓変釜鍒嗗埆琛ㄧず杈撳嚭娴侊紙OutputStream锛夈€佸獟浣撶被鍨嬶紙ContentType锛夊拰鍝嶅簲鐘舵€佺爜锛圫tatusCode锛夈€?

   1: public abstract class HttpRequest
   2: {
   3:     public abstract Uri Url { get; }
   4: }
   5:  
   6: public abstract class HttpResponse
   7: {
   8:     public abstract Stream     OutputStream { get; }
   9:     public abstract string     ContentType { get; set; }
  10:     public abstract int        StatusCode { get; set; }
  11:  
  12:     public void WriteFile(string fileName, string contentType)
  13:     {
  14:         if (File.Exists(fileName))
  15:         {
  16:             byte[] content         = File.ReadAllBytes(fileName);
  17:             this.ContentType       = contentType;
  18:             this.OutputStream.Write(content, 0, content.Length);
  19:         }
  20:         this.StatusCode = 404;
  21:     }
  22: }

DefaultHttpContext

ASP.NET Core榛樿浣跨敤鐨凥ttpContext鏄竴涓被鍨嬩负DefaultHttpContext瀵硅薄锛屽湪浠嬬粛DefaultContext鐨勫疄鐜板師鐞嗕箣鍓嶏紝鎴戜滑蹇呴』浜嗚В杩欎釜涓€涓簨瀹烇細璇锋眰鐨勬帴鏀惰€呭拰鏈€缁堝搷搴旇€呮槸鏈嶅姟鍣紝涓€鑸潵璇存湇鍔″櫒鎺ユ敹鍒拌姹備箣鍚庝細鍒涘缓鑷繁鐨勪笂涓嬫枃鏉ユ弿杩板綋鍓嶈姹傦紝閽堝璇锋眰鐨勭浉搴斾篃閫氳繃杩欎釜鍘熷涓婁笅鏂囨潵瀹屾垚銆傚湪搴旂敤涓笉浠呯粺涓€浣跨敤杩欎釜DefaultHttpContext瀵硅薄鏉ヨ幏鍙栬姹備俊鎭紝鍚屾椂杩樺埄鐢ㄥ畠鏉ュ畬鎴愬璇锋眰鐨勫搷搴旓紝鎵€浠ュ畠蹇呯劧涓庢湇鍔″櫒鍒涘缓鐨勫師濮嬩笂涓嬫枃瀛樺湪鏌愪釜鍏宠仈锛岃繖绉嶅叧鑱旀槸閫氳繃涓婇潰鎴戜滑鎻愬埌杩囩殑杩欎釜FeatureCollection瀵硅薄鏉ュ疄鐜扮殑銆?

鎶€鏈垎浜? title=

濡傚彸鍥炬墍绀猴紝涓嶅悓绫诲瀷鐨勬湇鍔″櫒鍦ㄦ帴鏀跺埌璇锋眰鐨勬椂鍊欎細鍒涘缓涓€涓師濮嬬殑涓婁笅鏂囷紝鐒跺悗瀹冧細灏嗘搷浣滃師濮嬩笂涓嬫枃鐨勬搷浣滃皝瑁呮垚涓€绯诲垪鏍囧噯鐨勭壒鎬у璞★紙鐗规€х被鍨嬪疄鐜扮粺涓€鐨勬帴鍙o級銆傝繖浜涚壒鎬у璞℃渶缁堟湇鍔″櫒琚粍瑁呮垚涓€涓狥eatureCollection瀵硅薄锛屽簲鐢ㄧ▼搴忎腑浣跨敤鐨凞efaultHttpContext灏辨槸鏍规嵁瀹冨垱寤虹殑銆傚綋鎴戜滑璋冪敤DefaultHttpContext鐩稿簲鐨勫睘鎬у拰鏂规硶鏃讹紝鍦ㄥ畠鐨勫唴閮ㄥ疄闄呬笂鍊熷姪灏佽鐨勭壒鎬у璞″幓鎿嶄綔鍘熷鐨勪笂涓嬫枃銆?

涓€鏃︿簡瑙efaultHttpContext鏄浣曟搷浣滃師濮婬TTP涓婁笅鏂囦箣鍚庯紝瀵逛簬DefaultHttpContext鐨勫畾涔夊氨寰堝ソ鐞嗚В浜嗐€傚涓嬮潰鐨勪唬鐮佺墖鏂墍绀猴紝DefaultHttpContext鍏锋湁涓€涓狪FeatureCollection绫诲瀷鐨勫睘鎬ttpContextFeatures锛屽畠琛ㄧず鐨勬鏄敱鏈嶅姟鍣ㄥ垱寤虹殑鐢ㄤ簬灏佽鍘熷HTTP涓婁笅鏂囩浉鍏崇壒鎬х殑FeatureCollection瀵硅薄銆傞€氳繃鏋勯€犲嚱鏁扮殑瀹氫箟鎴戜滑鐭ラ亾瀵逛簬涓€涓狣efaultHttpContext瀵硅薄鏉ヨ锛岃〃绀鸿姹傚拰鍝嶅簲鐨勫垎鍒槸涓€涓狣efaultHttpRequst鍜孒ttpResponse瀵硅薄銆?

   1: public class DefaultHttpContext : HttpContext
   2: { 
   3:    public IFeatureCollection HttpContextFeatures { get;}
   4:  
   5:     public DefaultHttpContext(IFeatureCollection httpContextFeatures)
   6:     {
   7:         this.HttpContextFeatures = httpContextFeatures;
   8:         this.Request             = new DefaultHttpRequest(this);
   9:         this.Response            = new DefaultHttpResponse(this);
  10:     }   
  11:     public override HttpRequest     Request { get; }
  12:     public override HttpResponse    Response { get; }
  13: }

灏佽鍚勭鍘熷HTTP涓婁笅鏂囩殑鐗规€ц兘澶熺粺涓€琚獶efaultHttpContext鎵€鐢紝瀹冧滑鐨勭被鍨嬮渶瑕佸疄鐜扮粺涓€鐨勬帴鍙o紝鍦ㄨ繖閲屾垜浠畾涔変簡濡備笅涓や釜閽堝璇锋眰鍜屽搷搴旂殑鐗规€ф帴鍙HttpRequestFeature鍜孖HttpResponseFeature锛屽畠浠笌HttpRequest鍜孒ttpResponse鍏锋湁绫讳技鐨勬垚鍛樺畾涔夈€?

   1: public interface IHttpRequestFeature
   2: {
   3:     Uri Url { get; }
   4: }
   5:  
   6: public interface IHttpResponseFeature
   7: {
   8:     Stream     OutputStream { get; }
   9:     string     ContentType { get; set; }
  10:     int        StatusCode { get; set; }
  11: }

瀹為檯涓奃efaultHttpContext瀵硅薄涓〃绀鸿姹傚拰鍝嶅簲鐨凞efaultHttpRequest鍜孌efaultHttpResponse瀵硅薄灏辨槸鍒嗗埆鏍规嵁浠庢彁渚涚殑FeatureCollection涓幏鍙栫殑HttpRequestFeature鍜孒ttpResponseFeature瀵硅薄鍒涘缓鐨勶紝鍏蜂綋鐨勫疄鐜板涓嬮潰鐨勪唬鐮佺墖鏂墍绀恒€?

   1: public class DefaultHttpRequest : HttpRequest
   2: {
娣卞害瀛︿範涓嶯LP

Hashmap涓環ash绠楁硶鍓栨瀽

Spring Boot 2.0娣卞害瀹炶返涔嬫牳蹇冩妧鏈瘒