From f62af07381b633d8e7ddf5787d9048fbbf4e3c85 Mon Sep 17 00:00:00 2001
From: Bond-009 <bond.009@outlook.com>
Date: Sat, 20 Apr 2019 12:18:44 +0200
Subject: [PATCH 1/2] Handle exception when loading unsupported assembly

Fixes #1256
---
 Emby.Server.Implementations/ApplicationHost.cs | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs
index 82042f5ca7..0295f10981 100644
--- a/Emby.Server.Implementations/ApplicationHost.cs
+++ b/Emby.Server.Implementations/ApplicationHost.cs
@@ -1167,7 +1167,7 @@ namespace Emby.Server.Implementations
             }
             catch (Exception ex)
             {
-                Logger.LogError(ex, "Error loading plugin {pluginName}", plugin.GetType().FullName);
+                Logger.LogError(ex, "Error loading plugin {PluginName}", plugin.GetType().FullName);
                 return null;
             }
 
@@ -1348,8 +1348,19 @@ namespace Emby.Server.Implementations
             {
                 foreach (var file in Directory.EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.AllDirectories))
                 {
-                    Logger.LogInformation("Loading assembly {Path}", file);
-                    yield return Assembly.LoadFrom(file);
+                    Assembly plugAss;
+                    try
+                    {
+                        plugAss = Assembly.LoadFrom(file);
+                    }
+                    catch (TypeLoadException ex)
+                    {
+                        Logger.LogError(ex, "Failed to load assembly {Path}", file);
+                        continue;
+                    }
+
+                    Logger.LogInformation("Loaded assembly {Assembly} from {Path}", plugAss.FullName, file);
+                    yield return plugAss;
                 }
             }
 

From 6973182ade7af9173abaf835608327be30b6b162 Mon Sep 17 00:00:00 2001
From: Bond-009 <bond.009@outlook.com>
Date: Sat, 20 Apr 2019 14:02:00 +0200
Subject: [PATCH 2/2] Fix more possible exceptions

---
 .../ApplicationHost.cs                        | 32 ++++++++++++++++---
 1 file changed, 27 insertions(+), 5 deletions(-)

diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs
index 0295f10981..5d5a63a635 100644
--- a/Emby.Server.Implementations/ApplicationHost.cs
+++ b/Emby.Server.Implementations/ApplicationHost.cs
@@ -1181,10 +1181,32 @@ namespace Emby.Server.Implementations
         {
             Logger.LogInformation("Loading assemblies");
 
-            AllConcreteTypes = GetComposablePartAssemblies()
-                .SelectMany(x => x.ExportedTypes)
-                .Where(type => type.IsClass && !type.IsAbstract && !type.IsInterface && !type.IsGenericType)
-                .ToArray();
+            AllConcreteTypes = GetTypes(GetComposablePartAssemblies()).ToArray();
+        }
+
+        private IEnumerable<Type> GetTypes(IEnumerable<Assembly> assemblies)
+        {
+            foreach (var ass in assemblies)
+            {
+                Type[] exportedTypes;
+                try
+                {
+                    exportedTypes = ass.GetExportedTypes();
+                }
+                catch (TypeLoadException ex)
+                {
+                    Logger.LogError(ex, "Error getting exported types from {Assembly}", ass.FullName);
+                    continue;
+                }
+
+                foreach (Type type in exportedTypes)
+                {
+                    if (type.IsClass && !type.IsAbstract && !type.IsInterface && !type.IsGenericType)
+                    {
+                        yield return type;
+                    }
+                }
+            }
         }
 
         private CertificateInfo CertificateInfo { get; set; }
@@ -1353,7 +1375,7 @@ namespace Emby.Server.Implementations
                     {
                         plugAss = Assembly.LoadFrom(file);
                     }
-                    catch (TypeLoadException ex)
+                    catch (FileLoadException ex)
                     {
                         Logger.LogError(ex, "Failed to load assembly {Path}", file);
                         continue;