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); }
/// <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 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); } }
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="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 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); }
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); } } } }
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; } } } } }
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); } }
/// <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)); }
/// <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); }
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)); } } } }