diff --git a/build/version.props b/build/version.props index 52c2114..27b338a 100644 --- a/build/version.props +++ b/build/version.props @@ -1,7 +1,7 @@ 1.0.0 - preview-10027 + preview-10041 $(VersionPrefix)-$(VersionSuffix) $(VersionPrefix).0 $(VersionPrefix).0 diff --git a/src/Shriek.EntityFrameworkCore/BaseDbContext.cs b/src/Shriek.EntityFrameworkCore/BaseDbContext.cs index 1421fbf..a679135 100644 --- a/src/Shriek.EntityFrameworkCore/BaseDbContext.cs +++ b/src/Shriek.EntityFrameworkCore/BaseDbContext.cs @@ -34,7 +34,7 @@ private IEnumerable GetEntityTypeConfigurations() //if (!entities.Any()) // return new Type[] { }; - var types = AppDomain.CurrentDomain.GetExcutingAssemblies().SelectMany(ass => ass.GetTypes()) + var types = AppDomain.CurrentDomain.GetAllTypes() .Where(x => { //获取IEntityTypeConfiguration<>的实现类 diff --git a/src/Shriek.ServiceProxy.Abstractions/ApiActionDescriptor.cs b/src/Shriek.ServiceProxy.Abstractions/ApiActionDescriptor.cs index ca6bb29..051133c 100644 --- a/src/Shriek.ServiceProxy.Abstractions/ApiActionDescriptor.cs +++ b/src/Shriek.ServiceProxy.Abstractions/ApiActionDescriptor.cs @@ -104,10 +104,12 @@ private ApiReturnAttribute[] ApiReturnAttributes get { if (apiReturnAttributes == null || !apiReturnAttributes.Any()) - apiReturnAttributes = AppDomain.CurrentDomain.GetExcutingAssemblies().SelectMany(x => x.GetTypes()) + { + AppDomain.CurrentDomain.UpdateExcutingAssemblies(); + apiReturnAttributes = AppDomain.CurrentDomain.GetAllTypes() .Where(x => x.BaseType == typeof(ApiReturnAttribute)) .Select(x => Activator.CreateInstance(x) as ApiReturnAttribute).ToArray(); - + } return apiReturnAttributes; } } diff --git a/src/Shriek.ServiceProxy.Http.Server/Internal/ServiceControllerFeatureProvider.cs b/src/Shriek.ServiceProxy.Http.Server/Internal/ServiceControllerFeatureProvider.cs index b331ad9..5481d4f 100644 --- a/src/Shriek.ServiceProxy.Http.Server/Internal/ServiceControllerFeatureProvider.cs +++ b/src/Shriek.ServiceProxy.Http.Server/Internal/ServiceControllerFeatureProvider.cs @@ -18,8 +18,7 @@ public ServiceControllerFeatureProvider(IEnumerable ServiceTypes) this.ServiceTypes = ServiceTypes; } - public void PopulateFeature(IEnumerable parts, - ControllerFeature feature) + public void PopulateFeature(IEnumerable parts, ControllerFeature feature) { foreach (var type in AppDomain.CurrentDomain.GetExcutingAssemblies().SelectMany(o => o.DefinedTypes)) { diff --git a/src/Shriek.ServiceProxy.Http.Server/ShriekServerExtensions.cs b/src/Shriek.ServiceProxy.Http.Server/ShriekServerExtensions.cs index 3d32b5c..1a34e90 100644 --- a/src/Shriek.ServiceProxy.Http.Server/ShriekServerExtensions.cs +++ b/src/Shriek.ServiceProxy.Http.Server/ShriekServerExtensions.cs @@ -11,19 +11,22 @@ public static class ShriekServerExtensions { public static IMvcBuilder AddWebApiProxyServer(this IMvcBuilder mvcBuilder, Action optionAction) { + AppDomain.CurrentDomain.UpdateExcutingAssemblies(); mvcBuilder.Services.AddMvcCore().AddWebApiProxyServer(optionAction); return mvcBuilder; } public static IMvcBuilder AddWebApiProxyServer(this IMvcBuilder mvcBuilder) { + AppDomain.CurrentDomain.UpdateExcutingAssemblies(); mvcBuilder.Services.AddMvcCore().AddWebApiProxyServer(); return mvcBuilder; } public static IMvcCoreBuilder AddWebApiProxyServer(this IMvcCoreBuilder mvcBuilder) { - var webApiProxyTypes = AppDomain.CurrentDomain.GetExcutingAssemblies().SelectMany(x => x.GetTypes()) + AppDomain.CurrentDomain.UpdateExcutingAssemblies(); + var webApiProxyTypes = AppDomain.CurrentDomain.GetAllTypes() .Where(x => !x.IsAbstract && typeof(WebApiProxy).IsAssignableFrom(x)); mvcBuilder.AddWebApiProxyServer(opt => @@ -39,8 +42,6 @@ public static IMvcCoreBuilder AddWebApiProxyServer(this IMvcCoreBuilder mvcBuild public static IMvcCoreBuilder AddWebApiProxyServer(this IMvcCoreBuilder mvcBuilder, Action optionAction) { - AppDomain.CurrentDomain.UpdateExcutingAssemblies(); - var option = new WebApiProxyOptions(); optionAction(option); diff --git a/src/Shriek/AppDomainExtensions.cs b/src/Shriek/AppDomainExtensions.cs index f239c23..dbd40ab 100644 --- a/src/Shriek/AppDomainExtensions.cs +++ b/src/Shriek/AppDomainExtensions.cs @@ -12,6 +12,8 @@ public static class AppDomainExtensions private static IEnumerable excutingAssembiles; + private static Type[] typeCache; + private static string GetActualDomainPath(this AppDomain @this) { return @this.RelativeSearchPath ?? @this.BaseDirectory; @@ -28,7 +30,8 @@ public static IEnumerable GetExcutingAssemblies(this AppDomain @this) lock (Locker) { if (excutingAssembiles == null || !excutingAssembiles.Any()) - excutingAssembiles = ReflectionUtil.GetAssemblies(new AssemblyFilter(@this.GetActualDomainPath())); + excutingAssembiles = + ReflectionUtil.GetAssemblies(new AssemblyFilter(@this.GetActualDomainPath())); } return excutingAssembiles; @@ -40,12 +43,38 @@ public static void UpdateExcutingAssemblies(this AppDomain @this) { var assemblies = ReflectionUtil.GetAssemblies(new AssemblyFilter(@this.GetActualDomainPath())); - excutingAssembiles = @this.GetExcutingAssemblies().Union(assemblies) - .Union(new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() }).Distinct(); + excutingAssembiles = @this.GetExcutingAssemblies().Union(assemblies).Union(new[] { Assembly.GetCallingAssembly(), Assembly.GetExecutingAssembly() }).Distinct(); } catch { } } + + /// + /// 获取所有类型 + /// + /// 程序域 + /// 从缓存获取 + /// + public static Type[] GetAllTypes(this AppDomain @this, bool fromCache = true) + { + if (fromCache && (typeCache == null || !typeCache.Any()) || !fromCache) + { + typeCache = @this.GetExcutingAssemblies() + .SelectMany(x => + { + try + { + return x.GetTypes(); + } + catch (ReflectionTypeLoadException ex) + { + return ex.Types.Where(t => t != null); + } + }).ToArray(); + } + + return typeCache; + } } } \ No newline at end of file diff --git a/src/Shriek/Reflection/ReflectionUtil.cs b/src/Shriek/Reflection/ReflectionUtil.cs index 2b51553..8788695 100644 --- a/src/Shriek/Reflection/ReflectionUtil.cs +++ b/src/Shriek/Reflection/ReflectionUtil.cs @@ -118,6 +118,23 @@ public static Assembly GetAssemblyNamed(string assemblyName) { throw; } + catch (ReflectionTypeLoadException ex) + { + var sb = new StringBuilder(); + foreach (var exSub in ex.LoaderExceptions) + { + sb.AppendLine(exSub.Message); + var exFileNotFound = exSub as FileNotFoundException; + if (!string.IsNullOrEmpty(exFileNotFound?.FusionLog)) + { + sb.AppendLine("Fusion Log:"); + sb.AppendLine(exFileNotFound.FusionLog); + } + sb.AppendLine(); + } + var errorMessage = sb.ToString(); + throw new Exception(errorMessage, ex); + } catch (BadImageFormatException) { throw; @@ -504,7 +521,27 @@ private static bool IsExe(string extension) /// private static Assembly LoadAssembly(AssemblyName assemblyName) { - return Assembly.Load(assemblyName); + try + { + return Assembly.Load(assemblyName); + } + catch (ReflectionTypeLoadException ex) + { + var sb = new StringBuilder(); + foreach (var exSub in ex.LoaderExceptions) + { + sb.AppendLine(exSub.Message); + var exFileNotFound = exSub as FileNotFoundException; + if (!string.IsNullOrEmpty(exFileNotFound?.FusionLog)) + { + sb.AppendLine("Fusion Log:"); + sb.AppendLine(exFileNotFound.FusionLog); + } + sb.AppendLine(); + } + var errorMessage = sb.ToString(); + throw new Exception(errorMessage, ex); + } } } } \ No newline at end of file diff --git a/src/Shriek/ShriekExtensions.cs b/src/Shriek/ShriekExtensions.cs index d0647c0..d346c22 100644 --- a/src/Shriek/ShriekExtensions.cs +++ b/src/Shriek/ShriekExtensions.cs @@ -26,7 +26,7 @@ public static IShriekBuilder AddShriek(this IServiceCollection services, Action< builder.Services.AddSingleton(typeof(IMessageSubscriber), typeof(EventMessageSubscriber)); - var messages = AppDomain.CurrentDomain.GetExcutingAssemblies().SelectMany(x => x.GetTypes()).Where(x => x.Assembly != Assembly.GetExecutingAssembly() && typeof(Message).IsAssignableFrom(x)); + var messages = AppDomain.CurrentDomain.GetAllTypes().Where(x => x.Assembly != Assembly.GetExecutingAssembly() && typeof(Message).IsAssignableFrom(x)); foreach (var msg in messages) {