public void RemoveExtension(Extension extension) { string str = string.Empty; using (ReaderWriterLockHelper.CreateWriterLock(this._locker)) { using (Dictionary <string, List <Extension> > .Enumerator enumerator = this.Extensions.GetEnumerator()) { KeyValuePair <string, List <Extension> > current; while (enumerator.MoveNext()) { current = enumerator.Current; if (current.Value.Remove(extension)) { goto Label_0043; } } goto Label_0067; Label_0043: str = current.Key; } } Label_0067: if (!string.IsNullOrEmpty(str)) { this.FireExtensionChanged(str, CollectionChangedAction.Remove, extension); } }
/// <summary> /// 将Assembly添加到ASP.NET页面预编译引用程序集列表。这个方法一般是内部使用。 /// </summary> /// <param name="assembly">程序集对象。</param> public static void AddReferencedAssembly(Assembly assembly) { Assembly assembly2 = null; using (ReaderWriterLockHelper.CreateWriterLock(_refAssembliesLock)) { foreach (Assembly current in _topLevelReferencedAssemblies) { AssemblyName name = current.GetName(); AssemblyName name2 = assembly.GetName(); if (name.Name.Equals(name2.Name)) { if (name2.Version.CompareTo(name.Version) <= 0) { return; } assembly2 = current; } } if (assembly2 != null) { _topLevelReferencedAssemblies.Remove(assembly2); } _topLevelReferencedAssemblies.Add(assembly); } FileLogUtility.Debug(string.Format("Add assembly '{0} to top level referenced assemblies.'", assembly.FullName)); }
/// <summary> /// 将插件本地程序集添加到ASP.NET页面预编译引用程序集列表。这个方法一般是内部使用。 /// </summary> /// <param name="bundleSymbolicName">插件唯一名称。</param> /// <returns>返回插件所有本地程序集。</returns> public virtual ICollection <Assembly> AddReferencedAssemblies(string bundleSymbolicName) { //Check if this bundle still exist or not. BundleData bundleData = BundleRuntime.Instance.GetFirstOrDefaultService <IBundleInstallerService>().GetBundleDataByName(bundleSymbolicName); if (bundleData == null) { FileLogUtility.Debug(string.Format("Bundle '{0}' does not exist when trying to add its assemblies to referenced assemblies.", bundleSymbolicName)); return(new List <Assembly>()); } using (ReaderWriterLockHelper.CreateReaderLock(CacheLock)) { //already registered its assembiles ICollection <Assembly> registeredItems; if (RegisteredBunldeCache.TryGetValue(bundleData, out registeredItems)) { return(registeredItems); } IServiceManager serviceContainer = BundleRuntime.Framework.ServiceContainer; IRuntimeService service = serviceContainer.GetFirstOrDefaultService <IRuntimeService>(); List <Assembly> assemlbies = service.LoadBundleAssembly(bundleSymbolicName); FileLogUtility.Debug(string.Format("Add the assemblies of bundle '{0}' to top level referenced assemblies.", bundleSymbolicName)); assemlbies.ForEach(AddReferencedAssembly); //cache the assemblies using (ReaderWriterLockHelper.CreateWriterLock(CacheLock)) { RegisteredBunldeCache[bundleData] = assemlbies; } return(assemlbies); } }
private Assembly ResolveAssembly(IAssemblyMetadata metadata, Func <AssemblyName, bool> stopWhen, bool tryCache) { string assemblyFullPath = this.GetAssemblyFullPath(metadata); AssemblyEntry entry = this.FindCacheByPath(assemblyFullPath); if (entry == null) { TryStartBundle(this.Framework.GetBundleBySymbolicName(metadata.Owner.SymbolicName)); entry = this.FindCacheByPath(assemblyFullPath); if (entry == null) { entry = new AssemblyEntry { Assembly = Assembly.LoadFile(assemblyFullPath), ProvidedBy = metadata, AssemblyPath = assemblyFullPath }; using (ReaderWriterLockHelper.CreateWriterLock(this._lock)) { if (!this.Cache.ContainsKey(entry.Assembly.FullName)) { this.Cache.Add(entry.Assembly.FullName, entry); } } } } if ((stopWhen != null) && stopWhen(entry.Assembly.GetName())) { return(entry.Assembly); } return(entry.Assembly); }
private void ReleaseExtension(IBundle bundle) { Func <Extension, bool> comparer = null; Func <ExtensionPoint, bool> func2 = null; Dictionary <string, List <Extension> > dictionary = new Dictionary <string, List <Extension> >(); List <ExtensionPoint> list = new List <ExtensionPoint>(); using (ReaderWriterLockHelper.CreateWriterLock(this._locker)) { foreach (KeyValuePair <string, List <Extension> > pair in this.Extensions) { if (comparer == null) { comparer = item => item.Owner == bundle; } dictionary[pair.Key] = ListUtility.RemoveAll <Extension>(pair.Value, comparer); } if (func2 == null) { func2 = item => item.Owner == bundle; } list = ListUtility.RemoveAll <ExtensionPoint>(this.ExtensionPoints, func2); } foreach (KeyValuePair <string, List <Extension> > pair2 in dictionary) { this.FireExtensionsChanged(pair2.Key, CollectionChangedAction.Remove, pair2.Value); } foreach (ExtensionPoint point in list) { this.FireExtensionPointChanged(CollectionChangedAction.Remove, point); } }
private AssemblyEntry FindCacheByPath(string path) { AssemblyEntry entry3; using (ReaderWriterLockHelper.CreateReaderLock(this._lock)) { AssemblyEntry entry = null; using (Dictionary <string, AssemblyEntry> .ValueCollection.Enumerator enumerator = this.Cache.Values.GetEnumerator()) { AssemblyEntry current; while (enumerator.MoveNext()) { current = enumerator.Current; if ((current != null) && (current.AssemblyPath == path)) { goto Label_0046; } } goto Label_0058; Label_0046: entry = current; } Label_0058: entry3 = entry; } return(entry3); }
/// <summary> /// 将程序集从ASP.NET页面预编译引用程序集列表中删除。 /// </summary> /// <param name="assembly">程序集对象。</param> public static void RemoveReferencedAssemlby(Assembly assembly) { using (ReaderWriterLockHelper.CreateWriterLock(_refAssembliesLock)) { _topLevelReferencedAssemblies.Remove(assembly); } FileLogUtility.Debug(string.Format("Remove assembly '{0} from top level referenced assemblies.'", assembly.FullName)); }
public List <ExtensionPoint> GetExtensionPoints() { using (ReaderWriterLockHelper.CreateReaderLock(this._locker)) { List <ExtensionPoint> list = new List <ExtensionPoint>(); list.AddRange(this.ExtensionPoints); return(list); } }
public void Stop() { AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(this.CurrentDomain_AssemblyResolve); AppDomain.CurrentDomain.AssemblyLoad -= new AssemblyLoadEventHandler(this.CurrentDomain_AssemblyLoad); using (ReaderWriterLockHelper.CreateWriterLock(this._lock)) { this.Cache.Clear(); } }
public void AddExtension(string extensionPoint, Extension extension) { using (ReaderWriterLockHelper.CreateWriterLock(this._locker)) { if (!this.Extensions.ContainsKey(extensionPoint)) { this.Extensions[extensionPoint] = new List <Extension>(); } this.Extensions[extensionPoint].Add(extension); } this.FireExtensionChanged(extensionPoint, CollectionChangedAction.Add, extension); }
public Assembly ResolveAssembly(IAssemblyMetadata metadata, AssemblyName assemblyName) { using (ReaderWriterLockHelper.CreateReaderLock(this._lock)) { AssemblyEntry entry; if ((assemblyName != null) && this.Cache.TryGetValue(assemblyName.FullName, out entry)) { return(entry.Assembly); } } return(this.ResolveAssembly(metadata, assembly => StringUtility.IsAssemblyFullNameMatch(assembly, assemblyName, true), true)); }
public List <Extension> GetExtensions(string extensionPoint) { using (ReaderWriterLockHelper.CreateReaderLock(this._locker)) { List <Extension> list2; List <Extension> list = new List <Extension>(); if (this.Extensions.TryGetValue(extensionPoint, out list2)) { list.AddRange(list2); } return(list); } }
private void Resolver_BundleUnresolved(object sender, BundleMetadataEventArgs e) { using (ReaderWriterLockHelper.CreateWriterLock(this._lock)) { Dictionary <string, AssemblyEntry> dictionary = new Dictionary <string, AssemblyEntry>(this.Cache); foreach (KeyValuePair <string, AssemblyEntry> pair in dictionary) { if (pair.Value.ProvidedBy.Owner == e.Metadata) { this.Cache.Remove(pair.Key); } } } }
public List <ExtensionPoint> GetExtensionPoints(IBundle bundle) { using (ReaderWriterLockHelper.CreateReaderLock(this._locker)) { List <ExtensionPoint> list = new List <ExtensionPoint>(); foreach (ExtensionPoint point in this.ExtensionPoints) { if (point.Owner == bundle) { list.Add(point); } } return(list); } }
protected bool TryGetCachedType(string className, out Type type) { type = null; if (BundleRuntime.Instance.EnableBundleClassLoaderCache) { using (ReaderWriterLockHelper.CreateReaderLock(_cacheLock)) { if (_classAssemblyNameCache.Contains(className)) { type = _classAssemblyNameCache[className] as Type; return(true); } } } return(false); }
protected void CacheLoadedType(string className, Type type) { if (BundleRuntime.Instance.EnableBundleClassLoaderCache) { using (ReaderWriterLockHelper.CreateWriterLock(_cacheLock)) { if (!_classAssemblyNameCache.Contains(className)) { if (_classAssemblyNameCache.Count >= BundleRuntime.Instance.BundleClassLoaderCacheSize) { FileLogUtility.Inform(string.Format(Messages.RemoveCachedTypeForSize, Bundle.SymbolicName, Bundle.Version, BundleRuntime.Instance.BundleClassLoaderCacheSize)); _classAssemblyNameCache.RemoveAt(0); } _classAssemblyNameCache.Add(className, type); } } } }
public static void RemoveReferencedAssemblies(BundleData bundleData) { using (ReaderWriterLockHelper.CreateReaderLock(_cacheLock)) { if (_registeredBunldeCache.ContainsKey(bundleData)) { IList <Assembly> removedAssemblies = _registeredBunldeCache[bundleData]; using (ReaderWriterLockHelper.CreateWriterLock(_cacheLock)) { _registeredBunldeCache.Remove(bundleData); } ResetTopLevelReferencedAssemblies(removedAssemblies); FileLogUtility.Debug( string.Format("Remove the assemblies of bundle '{0}' from top level referenced assemblies since the bundle is stopping.", bundleData.SymbolicName)); } } }
public static void AddReferencedAssemblies(BundleData bundleData) { using (ReaderWriterLockHelper.CreateReaderLock(_cacheLock)) { if (!_registeredBunldeCache.ContainsKey(bundleData)) { IList <Assembly> list = AddReferencedAssemblies(bundleData.SymbolicName); FileLogUtility.Debug( string.Format("Add the assemblies of bundle '{0}' to top level referenced assemblies since the bundle is active.", bundleData.SymbolicName)); if (list != null && list.Count > 0) { using (ReaderWriterLockHelper.CreateWriterLock(_cacheLock)) { _registeredBunldeCache[bundleData] = list; } } } } }
/// <summary> /// 当启动/停止插件时,需要对TopLevelAssemblies重新处理确保同一个程序集的只有最高版本出现在列表中。 /// 详细请查看“模块层与类加载设计规范的2.7小节”。 /// </summary> /// <param name="removedAssemblies"></param> public static void ResetTopLevelReferencedAssemblies(IList <Assembly> removedAssemblies) { using (ReaderWriterLockHelper.CreateReaderLock(_cacheLock)) { if (removedAssemblies != null) { foreach (Assembly current in removedAssemblies) { RemoveReferencedAssemlby(current); } } List <Assembly> list = new List <Assembly>(); foreach (IList <Assembly> current2 in _registeredBunldeCache.Values) { list.AddRange(current2); } list.Sort(new AssemblyComparer()); list.ForEach(AddReferencedAssembly); } }
private void AddExtensionPoint(IBundle bundle, List <ExtensionPointData> list) { List <ExtensionPoint> list2 = new List <ExtensionPoint>(); Dictionary <string, Extension> dictionary = new Dictionary <string, Extension>(); using (ReaderWriterLockHelper.CreateWriterLock(this._locker)) { foreach (ExtensionPointData data in list) { ExtensionPoint item = new ExtensionPoint(this) { Owner = bundle, Point = data.Point, Schema = data.Schema }; this.ExtensionPoints.Add(item); list2.Add(item); if (data.ChildNodes.Count > 0) { Extension extension = new Extension { Owner = bundle }; extension.Data.AddRange(data.ChildNodes); if (!this.Extensions.ContainsKey(data.Point)) { this.Extensions[data.Point] = new List <Extension>(); } this.Extensions[data.Point].Add(extension); dictionary[data.Point] = extension; } } } foreach (ExtensionPoint point3 in list2) { this.FireExtensionPointChanged(CollectionChangedAction.Add, point3); } foreach (KeyValuePair <string, Extension> pair in dictionary) { this.FireExtensionChanged(pair.Key, CollectionChangedAction.Add, pair.Value); } }
public Assembly ResolveAssembly(AssemblyName assebmlyFullName) { Func <AssemblyName, bool> stopWhen = null; using (ReaderWriterLockHelper.CreateReaderLock(this._lock)) { AssemblyEntry entry; if (this.Cache.TryGetValue(assebmlyFullName.FullName, out entry)) { return(entry.Assembly); } } IAssemblyMetadataNode node = this.Resolver.ResolvedAssemblyMetadataNodes.Find(delegate(IAssemblyMetadataNode resolvedAssemblyNode) { IAssemblyMetadata metadata = resolvedAssemblyNode.Metadata as IAssemblyMetadata; AssertUtility.NotNull(metadata); if (assebmlyFullName.Version == null) { if (!metadata.MultipleVersions) { return(assebmlyFullName.Name.Equals(metadata.AssemblyName.Name)); } } else if (StringUtility.IsAssemblyFullNameMatch(metadata.AssemblyName, assebmlyFullName, true)) { return(true); } return(false); }); if (node == null) { return(null); } if (stopWhen == null) { stopWhen = assembly => StringUtility.IsAssemblyFullNameMatch(assembly, assebmlyFullName, true); } return(this.ResolveAssembly((IAssemblyMetadata)node.Metadata, stopWhen, false)); }
/// <summary> /// 将Assembly添加到ASP.NET页面预编译引用程序集列表。这个方法一般是内部使用。 /// </summary> /// <param name="assembly">程序集对象。</param> public void AddReferencedAssembly(Assembly assembly) { // TODO:在以下场景会出现一些问题,场景描述为:WebHost程序引用了 // 插件Service的程序集,这个程序集在编译时会拷贝到bin目录下。 // // 此时,该程序集在CLR路径中可以加载到,但是这个程序集 // 又被服务插件添加到TopReferencedAssemblies,此时在编译时会提示 // 一个重复程序集异常。 // // 如果,我们在这里面进行判断是否CLR可以加载到时,又会有另一个异常, // 即WebHost的程序集是由CLR加载的,但是服务插件的程序集是由Assembly.LoadFile // 加载的,因此服务插件注册的服务接口是使用Assembly.LoadFile来定义, // 因此在WebHost用CLR加载的同一个程序集的接口去访问服务则无法访问到。 // //Assembly asmInClrLoader = null; //try //{ // asmInClrLoader = Assembly.Load(assembly.FullName); //} //catch { } //if (asmInClrLoader != null) //{ // FileLogUtility.Warn(string.Format("There is a assembly '{0}' can be loaded by CLR loader and it is ignored to add to top level assembly reference.", assembly.FullName)); // return; //} // TODO: 性能优化 using (ReaderWriterLockHelper.CreateWriterLock(RefAssembliesLock)) { if (TopLevelReferencedAssemblies.Any(tempAssembly => tempAssembly.FullName.Equals(assembly.FullName))) { return; } TopLevelReferencedAssemblies.Add(assembly); } FileLogUtility.Debug(string.Format("Add assembly '{0} to top level referenced assemblies.'", assembly.FullName)); }
public ExtensionPoint GetExtensionPoint(string extensionPoint) { extensionPoint = extensionPoint.Trim(); using (ReaderWriterLockHelper.CreateReaderLock(this._locker)) { foreach (ExtensionPoint point in this.ExtensionPoints) { if (point.Point == extensionPoint) { return(point); } } if (this.GetExtensions(extensionPoint).Count > 0) { return(new ExtensionPoint(this) { Point = extensionPoint }); } } return(null); }
/// <summary> /// 将插件本地程序集添加到ASP.NET页面预编译引用程序集列表。这个方法一般是内部使用。 /// </summary> /// <param name="bundleSymbolicName">插件唯一名称。</param> /// <returns>返回插件所有本地程序集。</returns> public static IList <Assembly> AddReferencedAssemblies(string bundleSymbolicName) { BundleData bundleDataByName = BundleRuntime.Instance.GetFirstOrDefaultService <IBundleInstallerService>().GetBundleDataByName(bundleSymbolicName); if (bundleDataByName == null) { FileLogUtility.Debug(string.Format("Bundle '{0}' does not exist when trying to add its assemblies to referenced assemblies.", bundleSymbolicName)); return(new List <Assembly>()); } IList <Assembly> result; using (ReaderWriterLockHelper.CreateReaderLock(_cacheLock)) { IList <Assembly> list; if (_registeredBunldeCache.TryGetValue(bundleDataByName, out list)) { result = list; } else { IServiceManager serviceContainer = BundleRuntime.Instance.Framework.ServiceContainer; IRuntimeService firstOrDefaultService = serviceContainer.GetFirstOrDefaultService <IRuntimeService>(); List <Assembly> list2 = firstOrDefaultService.LoadBundleAssembly(bundleSymbolicName); FileLogUtility.Debug(string.Format("Add the assemblies of bundle '{0}' to top level referenced assemblies.", bundleSymbolicName)); using (ReaderWriterLockHelper.CreateWriterLock(_cacheLock)) { _registeredBunldeCache[bundleDataByName] = list2; } ResetTopLevelReferencedAssemblies(null); result = list2; } } return(result); }
public void TestMethod1() { ReaderWriterLockHelper readerWriterLock = new ReaderWriterLockHelper(); AutoResetEvent autoResetEvent = new AutoResetEvent(false); int threadNumber = 3; int value = 10000; new Thread(() => { while (true) { bool needBreak = false; readerWriterLock.Read(() => { if (value <= 0) { needBreak = true; return; } Console.WriteLine($"线程 {Thread.CurrentThread.Name} 计数 {value}"); }); if (needBreak) { break; } } threadNumber--; if (threadNumber <= 0) { autoResetEvent.Set(); } }) { Name = "读" }.Start(); new Thread(() => { while (true) { bool needBreak = false; readerWriterLock.Write(() => { if (value <= 0) { needBreak = true; return; } value--; Console.WriteLine($"线程 {Thread.CurrentThread.Name} 计数 {value}"); }); if (needBreak) { break; } } threadNumber--; if (threadNumber <= 0) { autoResetEvent.Set(); } }) { Name = "写" }.Start(); new Thread(() => { while (true) { if (!readerWriterLock.TryEnterReadLock()) { continue; } try { if (!readerWriterLock.TryEnterWriteLock(out ReaderWriterLockCookie cookie)) { continue; } try { if (value <= 0) { break; } value--; Console.WriteLine($"线程 {Thread.CurrentThread.Name} 计数 {value}"); } finally { readerWriterLock.ExitWriteLock(ref cookie); } } finally { readerWriterLock.ExitReadLock(); } } threadNumber--; if (threadNumber <= 0) { autoResetEvent.Set(); } }) { Name = "读写" }.Start();
private void BundleEventListener(object sender, BundleStateChangedEventArgs args) { // This is in another domain, thus the HttpContext.Current is always null. // Just comment it. //if (HttpContext.Current == null) //{ // return; //} //check if this bundle still exist. BundleData bundleData = BundleRuntime.Instance.GetFirstOrDefaultService <IBundleInstallerService>() .GetBundleDataByName(args.Bundle.SymbolicName); if (bundleData == null) { return; } bool needLoad = (args.CurrentState == BundleState.Active); if (needLoad) { //already registered its assemblies using (ReaderWriterLockHelper.CreateReaderLock(CacheLock)) { if (RegisteredBunldeCache.ContainsKey(bundleData)) { return; } //register bundle assemblies to BuildManager. ICollection <Assembly> assemblies = AddReferencedAssemblies(bundleData.SymbolicName); FileLogUtility.Debug( string.Format("Add the assemblies of bundle '{0}' to top level referenced assemblies since the bundle is active.", args.Bundle.SymbolicName)); if (assemblies != null && assemblies.Count > 0) { using (ReaderWriterLockHelper.CreateWriterLock(CacheLock)) { RegisteredBunldeCache[bundleData] = assemblies; } } } } else if (args.CurrentState == BundleState.Stopping) { //unregister when stopping. using (ReaderWriterLockHelper.CreateReaderLock(CacheLock)) { if (RegisteredBunldeCache.ContainsKey(bundleData)) { RemoveReferencedAssemblies(RegisteredBunldeCache[bundleData]); //remove from cache. using (ReaderWriterLockHelper.CreateWriterLock(CacheLock)) { RegisteredBunldeCache.Remove(bundleData); } FileLogUtility.Debug( string.Format("Remove the assemblies of bundle '{0}' from top level referenced assemblies since the bundle is stopping.", args.Bundle.SymbolicName)); } } } }