OwinBuilder婧愮爜闃呰
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OwinBuilder婧愮爜闃呰相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
婧愮爜鍙傝Microsoft.Owin.Host.SystemWeb.OwinBuilder
閫氳繃鍓嶆枃鐭ラ亾锛?span lang="en-US">Build鏂规硶灏嗚璋冪敤锛屽叾鍋氱殑绗竴浠朵簨鍎垮氨鏄鎵?span lang="en-US">Startup鏂规硶
銆€銆€銆€銆€internal static OwinAppContext Build() { Action<IAppBuilder> startup = GetAppStartup(); return Build(startup); }
鑰?span lang="en-US">GetAppStartup鏂规硶涓昏瀹屾垚浠庡綋鍓?span lang="en-US">Assembly涓鎵?span lang="en-US">AppStartup鏂规硶锛岃繖涔熸槸涓轰粈涔堢敵鏄?span lang="en-US">Startup锛屼娇鍏惰璋冪敤鏈変袱绉嶆柟娉曪細
1
[assembly: OwinStartup(typeof(XX.Startup))] //鍒╃敤OwinStartupAtrribute鏉ユ寚瀵糞tartup
2
<appSettings> <add key="owin:appStartup" value="StartupDemo.ProductionStartup" /> </appSettings> //鍦╳ebconfig涓畾涔塷win:appStartup閿€煎锛屼互涓嬪皢瀵硅礋璐f悳绱tartup鏂规硶鐨凞efaultLoader鐨勬簮鐮佽繘琛屽垎鏋愶紝鏉ヤ簡瑙e浣曞畾浣峉tartup鏂规硶鐨?/span>
銆€銆€銆€銆€internal static Action<IAppBuilder> GetAppStartup() { string appStartup = ConfigurationManager.AppSettings[Constants.OwinAppStartup]; var loader = new DefaultLoader(new ReferencedAssembliesWrapper()); IList<string> errors = new List<string>(); Action<IAppBuilder> startup = loader.Load(appStartup ?? string.Empty, errors);
if (startup == null) { throw new EntryPointNotFoundException(Resources.Exception_AppLoderFailure + Environment.NewLine + " - " + string.Join(Environment.NewLine + " - ", errors) + (IsAutomaticAppStartupEnabled ? Environment.NewLine + Resources.Exception_HowToDisableAutoAppStartup : string.Empty) + Environment.NewLine + Resources.Exception_HowToSpecifyAppStartup); } return startup; }
涓婇潰婧愮爜灞曠ず浜嗚皟鐢?span lang="en-US">DefaultLoader鐨?span lang="en-US">Load鏂规硶鏉ユ悳绱?span lang="en-US">Startup锛岃€?span lang="en-US">Startup鏄竴涓?span lang="en-US">Action<IAppBuilder>鏂规硶锛屽嵆鎺ュ彈涓€涓疄鐜颁簡IAppBuilder鎺ュ彛鐨勫疄渚嬩綔涓哄弬鏁帮紝杩斿洖鍊间负void鐨?span lang="en-US">Action
銆€銆€銆€銆€public Action<IAppBuilder> Load(string startupName, IList<string> errorDetails) { return LoadImplementation(startupName, errorDetails) ?? _next(startupName, errorDetails); }
Load鏂规硶瀹為檯涓婃槸瀵?span lang="en-US">LoadImplementation鐨勪竴涓皝瑁咃紝濡傛灉瀵绘壘澶辫触鍒欎娇鐢?span lang="en-US">_next杩涜瀵绘壘锛堝疄闄呬笂杩欎細杩斿洖null锛岃繖涓嶆槸閲嶇偣锛?/span>
1 private Action<IAppBuilder> LoadImplementation(string startupName, IList<string> errorDetails) 2 { 3 Tuple<Type, string> typeAndMethod = null; 4 startupName = startupName ?? string.Empty; 5 // Auto-discovery or Friendly name? 6 if (!startupName.Contains(鈥?/span>,鈥?/span>)) 7 { 8 typeAndMethod = GetDefaultConfiguration(startupName, errorDetails); //閫氬父浼氳繘鍏ヨ繖涓€娴佺▼锛屽鏋渟tartupName涓寘鍚€楀彿锛屽垯瀵瑰簲鍙︿竴绉嶇敵鏄庢柟寮?/span> 9 } 10 11 if (typeAndMethod == null && !string.IsNullOrWhiteSpace(startupName)) //杩欑鐢虫槑鏂瑰紡涓篠tartupName = “startupName锛宎ssemblyName” 12 { 13 typeAndMethod = GetTypeAndMethodNameForConfigurationString(startupName, errorDetails); //瀵箂tartupName鍜宎ssemblyName杩涜鍒嗙锛屽苟鎵惧埌瀵瑰簲鐨刟ssembly鍔犺浇 14 銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€ //鍏朵腑鐨剆tartupName 15 } 16 17 if (typeAndMethod == null) 18 { 19 return null; 20 } 21 22 Type type = typeAndMethod.Item1; 23 // default to the "Configuration" method if only the type name was provided //濡傛灉鍙彁渚涗簡startup鐨則ype锛屽垯榛樿璋冪敤鍏朵腑鐨凜onfiguration鏂规硶 24 string methodName = !string.IsNullOrWhiteSpace(typeAndMethod.Item2) ? typeAndMethod.Item2 : Constants.Configuration; 25 26 Action<IAppBuilder> startup = MakeDelegate(type, methodName, errorDetails); //鐩存帴璋冪敤startup鏂规硶鎴栬€呭仛涓轰竴涓猰iddleware鍘嬪叆List涓紝鍚庢枃浼氳鍒板叿浣撳疄鐜?/span> 27 if (startup == null) 28 { 29 return null; 30 } 31 32 return builder => //鍐嶅startup杩涜涓€娆elegate灏佽锛屼紶鍏ュ弬鏁颁负builder锛屼緵涓婂眰璋冪敤 33 { 34 if (builder == null) 35 { 36 throw new ArgumentNullException("builder"); 37 } 38 39 object value; 40 if (!builder.Properties.TryGetValue(Constants.HostAppName, out value) || 41 String.IsNullOrWhiteSpace(Convert.ToString(value, CultureInfo.InvariantCulture))) 42 { 43 builder.Properties[Constants.HostAppName] = type.FullName; //鑾峰彇骞惰褰旽ostAppName 44 } 45 startup(builder); //寮€濮嬫瀯閫?/span> 46 }; 47 }
鐢变簬鍙傛暟startupName涓烘渶鍒濆畾涔夌殑甯搁噺锛屽叾鍊间负Constants.OwinAppStartup = "owin:AppStartup"锛涙墍浠ュ緢鏄庢樉浼氳皟鐢?span lang="zh-CN">GetDefaultConfiguration(startupName, errorDetails)鏂规硶杩涗竴姝ュ鐞嗐€?/span>
銆€銆€銆€銆€private Tuple<Type, string> GetDefaultConfiguration(string friendlyName, IList<string> errors) { friendlyName = friendlyName ?? string.Empty; bool conflict = false; Tuple<Type, string> result = SearchForStartupAttribute(friendlyName, errors, ref conflict); if (result == null && !conflict && string.IsNullOrEmpty(friendlyName)) { result = SearchForStartupConvention(errors); } return result; }
杩欎釜鏂规硶鍙堟槸瀵?span lang="en-US">SearchForStartupAttribute鐨勪竴涓皝瑁?/span>
鍏堜簡瑙d竴涓婳winStartupAttribute
鐪嬩笂鏂囦娇鐢ㄥ埌鐨勬瀯閫犲嚱鏁?/strong>
銆€銆€銆€銆€public OwinStartupAttribute(Type startupType) : this(string.Empty, startupType, string.Empty) { } 銆€銆€銆€銆€public OwinStartupAttribute(string friendlyName, Type startupType, string methodName) { if (friendlyName == null) { throw new ArgumentNullException("friendlyName"); } if (startupType == null) { throw new ArgumentNullException("startupType"); } if (methodName == null) { throw new ArgumentNullException("methodName"); } FriendlyName = friendlyName; StartupType = startupType; MethodName = methodName; }
杩欓噷榛樿灏?span lang="en-US">FriendlyName鍜?span lang="en-US">MethodName璁剧疆涓虹┖锛屽嵆鍙褰曚簡Startup绫荤殑Type锛屼笅闈㈢殑SearchForStartupAttribute涓昏涔熸槸閫氳繃瀵绘壘OwinStartupAttribute涓殑StartupType 鏉ヨ幏鍙?span lang="en-US">Startup鐨勩€?/span>
1 private Tuple<Type, string> SearchForStartupAttribute(string friendlyName, IList<string> errors, ref bool conflict) 2 { 3 friendlyName = friendlyName ?? string.Empty; 4 bool foundAnyInstances = false; 5 Tuple<Type, string> fullMatch = null; 6 Assembly matchedAssembly = null; 7 foreach (var assembly in _referencedAssemblies) // 閬嶅巻绋嬪簭闆?/span> 8 { 9 object[] attributes; 10 try 11 { 12 attributes = assembly.GetCustomAttributes(inherit: false); // 鑾峰彇绋嬪簭闆嗙殑鎵€鏈夎嚜瀹氫箟Attribute 13 } 14 catch (CustomAttributeFormatException) 15 { 16 continue; 17 } 18 19 foreach (var owinStartupAttribute in attributes.Where(attribute => attribute.GetType().Name.Equals(Constants.OwinStartupAttribute, StringComparison.Ordinal))) // 瀵硅幏鍙栧埌鐨凙ttribute杩涜杩囨护锛屽彧閬嶅巻OwinStartupAttribute锛屽嵆鏄紭鍏堜細 //瀵逛笂鏂囨墍璇寸殑绗竴绉?Startup鐢虫槑杩涜璋冪敤 20 { 21 Type attributeType = owinStartupAttribute.GetType(); //閲囩敤鍙嶅皠鏈哄埗锛屽厛鑾峰彇Type 22 foundAnyInstances = true; 23 24 // Find the StartupType property. 25 PropertyInfo startupTypeProperty = attributeType.GetProperty(Constants.StartupType, typeof(Type)); //瀵绘壘灞炴€у悕鏄疭tartupType锛屽睘鎬х被鍨嬫槸Type鐨勫睘鎬?/span> 26 if (startupTypeProperty == null) //瀵绘壘澶辫触锛岃褰曢敊璇?/span> 27 { 28 errors.Add(string.Format(CultureInfo.CurrentCulture, LoaderResources.StartupTypePropertyMissing, 29 attributeType.AssemblyQualifiedName, assembly.FullName)); 30 continue; 31 } 32 33 var startupType = startupTypeProperty.GetValue(owinStartupAttribute, null) as Type; //鑾峰彇StartupType灞炴€х殑鍊硷紝骞惰浆鎹负Type锛屼负鍙嶅皠鍋氬噯澶?/span> 34 if (startupType == null) //鑾峰彇鎴栬€呰浆鎹㈠け璐ワ紝璁板綍閿欒 35 { 36 errors.Add(string.Format(CultureInfo.CurrentCulture, LoaderResources.StartupTypePropertyEmpty, assembly.FullName)); 37 continue; 38 } 39 40 // FriendlyName is an optional property. 41 string friendlyNameValue = string.Empty; //FriendlyName鏄彲閫夐」锛屼綔涓哄Startup绫荤殑鍒О锛屼笉鏄噸鐐?/span> 42 PropertyInfo friendlyNameProperty = attributeType.GetProperty(Constants.FriendlyName, typeof(string)); 43 if (friendlyNameProperty != null) 44 { 45 friendlyNameValue = friendlyNameProperty.GetValue(owinStartupAttribute, null) as string ?? string.Empty; 46 } 47 48 if (!string.Equals(friendlyName, friendlyNameValue, StringComparison.OrdinalIgnoreCase)) //濡傛灉鏈畾涔塅riendlyName鍒欓粯璁ゆ槸Empty锛屽惁鍒欒褰曢敊璇?/span> 49 { 50 errors.Add(string.Format(CultureInfo.CurrentCulture, LoaderResources.FriendlyNameMismatch, 51 friendlyNameValue, friendlyName, assembly.FullName)); 52 continue; 53 } 54 55 // MethodName is an optional property. 56 string methodName = string.Empty; 鍚岀悊MethodName涔熸槸鍙€夐」锛屽鏋滀负瀹氫箟榛樿鏄疎mpty 57 PropertyInfo methodNameProperty = attributeType.GetProperty(Constants.MethodName, typeof(string)); 58 if (methodNameProperty != null) 59 { 60 methodName = methodNameProperty.GetValue(owinStartupAttribute, null) as string ?? string.Empty; 61 } 62 63 if (fullMatch != null) //琛ㄦ槑宸茬粡瀵绘壘鍒颁竴涓猄tartup绫伙紝鍒欏啿绐佷簡锛岃鏄庢湁閲嶅鐢虫槑Startup绫?/span> 64 { 65 conflict = true; 66 errors.Add(string.Format(CultureInfo.CurrentCulture, 67 LoaderResources.Exception_AttributeNameConflict, 68 matchedAssembly.GetName().Name, fullMatch.Item1, assembly.GetName().Name, startupType, friendlyName)); 69 } 70 else //灏氭湭瀵绘壘鍒癝tartup绫伙紝灏哠tartupType鍜孧ethodName瀛樹负浜屽厓缁勶紝璁板綍绋嬪簭闆?/span> 71 { 72 fullMatch = new Tuple<Type, string>(startupType, methodName); 73 matchedAssembly = assembly; 74 } 75 } 76 } 77 78 if (!foundAnyInstances) //鏈鎵惧埌鐢虫槑Startup鐨勭▼搴忛泦锛岃褰曢敊璇?/span> 79 { 80 errors.Add(LoaderResources.NoOwinStartupAttribute); 81 } 82 if (conflict) //濡傛灉鏈夊啿绐侊紝杩斿洖null 83 { 84 return null; 85 } 86 return fullMatch; //杩斿洖缁撴灉 87 }
鍓嶆枃璁插埌MakeDelegate(Type type, string methodName, IList<string> errors)涓昏浣滅敤鏄皢瀵绘壘鍒扮殑startup鏂规硶浣滀负涓€涓?span lang="en-US">middleware鍘嬪叆List涓紝鐪嬪叾婧愮爜
1 private Action<IAppBuilder> MakeDelegate(Type type, string methodName, IList<string> errors) 2 { 3 MethodInfo partialMatch = null; 4 foreach (var methodInfo in type.GetMethods()) 5 { 6 if (!methodInfo.Name.Equals(methodName)) 7 { 8 continue; 9 } 10 11 // void Configuration(IAppBuilder app) //妫€娴婼tartup绫讳腑鐨凜onfiguration鏂规硶鐨勫弬鏁板拰杩斿洖鍊硷紝杩欑涓洪粯璁ょ殑鏂规硶锛屼篃鏄柊寤篗VC鏃堕粯璁ょ殑鏂规硶 12 if (Matches(methodInfo, false, typeof(IAppBuilder))) //鏂规硶鏃犺繑鍥炲€硷紙void锛夛紝鍙傛暟涓猴紙IAppBuilder锛?/span> 13 { 14 object instance = methodInfo.IsStatic ? null : _activator(type); //濡傛灉涓洪潤鎬佹柟娉曪紝鍒欎笉闇€瑕佸疄渚嬶紝鍚﹀垯瀹炰緥鍖栦竴涓猄tartup瀵硅薄 15 return builder => methodInfo.Invoke(instance, new[] { builder }); //杩斿洖涓€涓狶ambda褰㈠紡鐨刣elegate锛屽疄闄呬笂灏辨槸璋冪敤Startup鐨凜onfiguration(IAppBuilder)鏂规硶 16 } 17 18 // object Configuration(IDictionary<string, object> appProperties) //鍙︿竴绉岰onfiguration鏂规硶锛屽弬鏁颁负Environment锛岃繑鍥瀘bject 19 if (Matches(methodInfo, true, typeof(IDictionary<string, object>))) 20 { 21 object instance = methodInfo.IsStatic ? null : _activator(type); //鐢变簬浼犲叆鍙傛暟涓篋ictionary锛屾墍浠ュ皢杩欎釜Configuration鏂规硶鍘嬪叆middleware鐨凩ist涓?/span> 22 return builder => builder.Use(new Func<object, object>(_ => methodInfo.Invoke(instance, new object[] { builder.Properties }))); 23 } //builder.Use浼犲叆鍙傛暟鏄竴涓狥unc<object,object>鐨刣elegate锛屽疄闄呬笂灏辨槸涓€涓猰iddleware锛屼笉杩囧洜涓哄湪鍒濆鍖栭樁娈碉紝鎵€浠ヤ笉闇€瑕佽繘鍏ヤ笅涓€涓猻tage 24 25 // object Configuration() //鏃犲弬鏁帮紝杩斿洖object 26 if (Matches(methodInfo, true)) 27 { 28 object instance = methodInfo.IsStatic ? null : _activator(type); 29 return builder => builder.Use(new Func<object, object>(_ => methodInfo.Invoke(instance, new object[0]))); 30 } 31 32 partialMatch = partialMatch ?? methodInfo; //璁板綍鎵惧埌浣嗕笉绗﹀悎涓夌瀹氫箟鐨凜onfiguration鏂规硶 33 } 34 35 if (partialMatch == null) //鏈壘鍒扮殑Configuration锛岃褰曢敊璇?/span> 36 { 37 errors.Add(string.Format(CultureInfo.CurrentCulture, 38 LoaderResources.MethodNotFoundInClass, methodName, type.AssemblyQualifiedName)); 39 } 40 else 鎵惧埌Configuration锛屼絾涓嶇鍚堜笁绉嶅畾涔夛紝璁板綍閿欒 41 { 42 errors.Add(string.Format(CultureInfo.CurrentCulture, LoaderResources.UnexpectedMethodSignature, 43 methodName, type.AssemblyQualifiedName)); 44 } 45 return null; 46 }
鎬荤粨锛?span lang="en-US">OwinBuilder涓昏瀹屾垚瀵?span lang="en-US">Startup绫荤殑瀵绘壘锛屽苟璋冪敤鍏朵腑鐨?span lang="en-US">Configuration鏂规硶锛?span lang="en-US">Configuration鏈変笁绉嶇鍚嶏紙浼犲叆鍙傛暟涓庤繑鍥炵粨鏋滐級锛屽皢鍏跺皝瑁呮垚涓€涓柟娉曡繑鍥炵粰涓婂眰锛屼緵涓婂眰璋冪敤銆傛帴涓嬫潵灏辨槸鏈€閲嶈鐨勫伐浣滐紝璋冪敤Startup涓殑Configuration鍏蜂綋鍋氫簡浠€涔堬紝姣忎釜middleware鏄浣曟敞鍏ュ埌pipeline涓殑锛岃繖灏辨槸AppBuilder涓昏鍋氱殑宸ヤ綔浜嗐€?/span>
以上是关于OwinBuilder婧愮爜闃呰的主要内容,如果未能解决你的问题,请参考以下文章