Joniff / Terratype

Multiple map picker for Umbraco CMS
19 stars 14 forks source link

Dynamic Assemblies causes registration of providers to fail silently #16

Open arknu opened 6 years ago

arknu commented 6 years ago

I was having an issue, where sometimes the maps would fail loading with the exception indicating that the provider couldn't be found.

After a lot of digging, I found the root cause. Trying to register dynamic assemblies throws an exception like this:

2018-04-06 13:23:15,194 [P6412/D7/T8] ERROR Terratype.Frisk.Frisk - Error registering assemblies in AppDomain, asesembly Microsoft.GeneratedCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
System.NotSupportedException: The invoked member is not supported in a dynamic assembly.
   at System.Reflection.Emit.InternalAssemblyBuilder.get_Location()
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Umbraco.Core.Logging.Logger.Info(Type type, String generateMessageFormat, Func`1[] formatItems)
   at Terratype.Frisk.Frisk.<>c.<.cctor>b__4_0()

I had to add a try-catch in the foreach loop i Frisk.cs to even catch this exception. It was failing silently, with the Lazy swallowing the exception (not at all what I expected - a yellow screen of death would have been easier).

My fix is to just not even attempt registering dynamic assemblies:

foreach (Assembly currAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
    try
    {
        if (!currAssembly.IsDynamic)
        {
            filenames.Add(currAssembly.Location.ToLowerInvariant());
            registerAssembly(currAssembly, ref installed);
        }
    }
    catch (Exception ex)
    {
        LogHelper.Error(typeof(Frisk), $"Error registering assemblies in AppDomain, asesembly {currAssembly.FullName}", ex);
    }
}
arknu commented 6 years ago

If registering a dynamic assembly is needed, then there needs to be at least a try/catch to ensure that the method continues running as it should.

I'm working on a PR to implement this suggested change.

arknu commented 6 years ago

PR #17

Joniff commented 6 years ago

Have released version 1.0.18 to fix this issue

arknu commented 6 years ago

Thanks @Joniff. But looking at https://github.com/Joniff/Terratype/blob/master/src/Terratype/Frisk/Frisk.cs I see no changes to suggest that this issues has been fixed.

Joniff commented 6 years ago

I fixed the issue back in March, I've only now got around to release it. It still might change again, as I'm currently in the process of writing a new ListView functionality that requires this new Frisk functionailty.

image