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
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: }
鍙冲浘鎵€绀虹殑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瀵硅薄鏉ュ疄鐜扮殑銆?
濡傚彸鍥炬墍绀猴紝涓嶅悓绫诲瀷鐨勬湇鍔″櫒鍦ㄦ帴鏀跺埌璇锋眰鐨勬椂鍊欎細鍒涘缓涓€涓師濮嬬殑涓婁笅鏂囷紝鐒跺悗瀹冧細灏嗘搷浣滃師濮嬩笂涓嬫枃鐨勬搷浣滃皝瑁呮垚涓€绯诲垪鏍囧噯鐨勭壒鎬у璞★紙鐗规€х被鍨嬪疄鐜扮粺涓€鐨勬帴鍙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