From fb815e3f1b237a0c7cc56f31f02a7d5f7a273bc5 Mon Sep 17 00:00:00 2001 From: defr0zen Date: Wed, 3 May 2017 21:33:22 +0200 Subject: [PATCH] fixed issue with Assembly.Location throwing --- .../UseCurrentAssembliesReferenceResolver.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/source/RazorEngine.Core/Compilation/ReferenceResolver/UseCurrentAssembliesReferenceResolver.cs b/src/source/RazorEngine.Core/Compilation/ReferenceResolver/UseCurrentAssembliesReferenceResolver.cs index e374618f..0741fa8b 100644 --- a/src/source/RazorEngine.Core/Compilation/ReferenceResolver/UseCurrentAssembliesReferenceResolver.cs +++ b/src/source/RazorEngine.Core/Compilation/ReferenceResolver/UseCurrentAssembliesReferenceResolver.cs @@ -22,10 +22,23 @@ public IEnumerable GetReferences(TypeContext context = null, { return CompilerServicesUtility .GetLoadedAssemblies() - .Where(a => !a.IsDynamic && File.Exists(a.Location) && !a.Location.Contains(CompilerServiceBase.DynamicTemplateNamespace)) + .Where(IsValidAssembly) .GroupBy(a => a.GetName().Name).Select(grp => grp.First(y => y.GetName().Version == grp.Max(x => x.GetName().Version))) // only select distinct assemblies based on FullName to avoid loading duplicate assemblies .Select(a => CompilerReference.From(a)) .Concat(includeAssemblies ?? Enumerable.Empty()); } + + private static bool IsValidAssembly(System.Reflection.Assembly a) + { + try + { + return !a.IsDynamic && File.Exists(a.Location) && !a.Location.Contains(CompilerServiceBase.DynamicTemplateNamespace); + } + catch + { + return false; + } + } + } }