Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
fix(type scaning): 修复Assembly.GetTypes()报错的问题,发布1.0.0-preview-10041 版本
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderJames committed Jan 13, 2018
1 parent 3593cc0 commit 9db081d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>preview-10027</VersionSuffix>
<VersionSuffix>preview-10041</VersionSuffix>
<Version>$(VersionPrefix)-$(VersionSuffix)</Version>
<AssemblyVersion>$(VersionPrefix).0</AssemblyVersion>
<FileVersion>$(VersionPrefix).0</FileVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Shriek.EntityFrameworkCore/BaseDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private IEnumerable<Type> GetEntityTypeConfigurations()
//if (!entities.Any())
// return new Type[] { };

var types = AppDomain.CurrentDomain.GetExcutingAssemblies().SelectMany(ass => ass.GetTypes())
var types = AppDomain.CurrentDomain.GetAllTypes()
.Where(x =>
{
//获取IEntityTypeConfiguration<>的实现类
Expand Down
6 changes: 4 additions & 2 deletions src/Shriek.ServiceProxy.Abstractions/ApiActionDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public ServiceControllerFeatureProvider(IEnumerable<Type> ServiceTypes)
this.ServiceTypes = ServiceTypes;
}

public void PopulateFeature(IEnumerable<ApplicationPart> parts,
ControllerFeature feature)
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
{
foreach (var type in AppDomain.CurrentDomain.GetExcutingAssemblies().SelectMany(o => o.DefinedTypes))
{
Expand Down
7 changes: 4 additions & 3 deletions src/Shriek.ServiceProxy.Http.Server/ShriekServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ public static class ShriekServerExtensions
{
public static IMvcBuilder AddWebApiProxyServer(this IMvcBuilder mvcBuilder, Action<WebApiProxyOptions> 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 =>
Expand All @@ -39,8 +42,6 @@ public static IMvcCoreBuilder AddWebApiProxyServer(this IMvcCoreBuilder mvcBuild

public static IMvcCoreBuilder AddWebApiProxyServer(this IMvcCoreBuilder mvcBuilder, Action<WebApiProxyOptions> optionAction)
{
AppDomain.CurrentDomain.UpdateExcutingAssemblies();

var option = new WebApiProxyOptions();
optionAction(option);

Expand Down
35 changes: 32 additions & 3 deletions src/Shriek/AppDomainExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static class AppDomainExtensions

private static IEnumerable<Assembly> excutingAssembiles;

private static Type[] typeCache;

private static string GetActualDomainPath(this AppDomain @this)
{
return @this.RelativeSearchPath ?? @this.BaseDirectory;
Expand All @@ -28,7 +30,8 @@ public static IEnumerable<Assembly> 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;
Expand All @@ -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
{
}
}

/// <summary>
/// 获取所有类型
/// </summary>
/// <param name="this">程序域</param>
/// <param name="fromCache">从缓存获取</param>
/// <returns></returns>
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;
}
}
}
39 changes: 38 additions & 1 deletion src/Shriek/Reflection/ReflectionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -504,7 +521,27 @@ private static bool IsExe(string extension)
/// <returns></returns>
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);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Shriek/ShriekExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IShriekBuilder AddShriek(this IServiceCollection services, Action<

builder.Services.AddSingleton(typeof(IMessageSubscriber<DomainNotification>), typeof(EventMessageSubscriber<DomainNotification>));

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)
{
Expand Down

0 comments on commit 9db081d

Please sign in to comment.