Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue with Assembly.Location throwing #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

defr0zen
Copy link

@defr0zen defr0zen commented May 3, 2017

Assembly.Location could throw. In my case, I am trying to use RazorEngine in an Azure Function, which loads some native Edge dlls for JavaScript interop. These assemblies are loaded into the domain, however, calling Assembly.Location on them throws an exception.

I was thinking to use a custom resolver, however, this code is also used in the exceptions, and it is not trivial to use the custom resolver in the exception constructors. Hence such quick fix.

Copy link
Collaborator

@matthid matthid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review, one smaller thing to change.

{
return !a.IsDynamic && File.Exists(a.Location) && !a.Location.Contains(CompilerServiceBase.DynamicTemplateNamespace);
}
catch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify which exception is thrown and only catch the specific one (and add a comment)?
Otherwise (if we want to catch all) we need some kind of logging here at the very least.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In AF this part is throwing exception.

@shyamal890
Copy link

Guys any temp workaround till the time this pull is merged into the master?

@matthid
Copy link
Collaborator

matthid commented Feb 3, 2018

@shyamal890 Yes the assembly-resolver can be configured so you can just copy it to your code and configure it.

@shyamal890
Copy link

@matthid I created a new cs file with the following code. However, CompilerServiceBase.DynamicTemplateNamespace gives an error since it's inaccessible due to its protection level.

namespace RazorEngine.Compilation.ReferenceResolver
{
    public class UseCurrentAssembliesReferenceResolver : IReferenceResolver
    {
        /// <summary>
        /// See <see cref="IReferenceResolver.GetReferences"/>
        /// </summary>
        /// <param name="context"></param>
        /// <param name="includeAssemblies"></param>
        /// <returns></returns>
        public IEnumerable<CompilerReference> GetReferences(TypeContext context = null, IEnumerable<CompilerReference> includeAssemblies = null)
        {
            return CompilerServicesUtility
                   .GetLoadedAssemblies()
                   .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<CompilerReference>());
        }

        private static bool IsValidAssembly(System.Reflection.Assembly a)
        {
            try
            {
                return !a.IsDynamic && File.Exists(a.Location) && !a.Location.Contains(CompilerServiceBase.DynamicTemplateNamespace);
            }
            catch
            {
                return false;
            }
        }

    }
}

@matthid
Copy link
Collaborator

matthid commented Feb 5, 2018

@shyamal890

protected internal const string DynamicTemplateNamespace = "CompiledRazorTemplates.Dynamic";

@harmandeol
Copy link

when will this be merged?

@vovikdrg
Copy link

vovikdrg commented May 16, 2018

private static bool IsValidAssembly(System.Reflection.Assembly a)
        {
            //Azure function is failing when try to accessl location of this dll
            var skipByName = new[] {"edge_nativeclr"};
            return !skipByName.All(c => a.FullName.StartsWith(c)) && !a.IsDynamic && File.Exists(a.Location) &&
                   !a.Location.Contains(DynamicTemplateNamespace);
        }

Basiclly this fixes my problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants