aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2018-12-27 08:21:05 -0800
committerRob Mensching <rob@robmensching.com>2018-12-27 08:30:39 -0800
commit563857ee4097aead50ff5711c5dc759c0fcc4c5d (patch)
treef6ec7c363fc5502c09ff010d52ac5c19234c4a8c /src
parent97231f3a30002fc9a4ef66dae2f90ceeef92dd07 (diff)
downloadwix-563857ee4097aead50ff5711c5dc759c0fcc4c5d.tar.gz
wix-563857ee4097aead50ff5711c5dc759c0fcc4c5d.tar.bz2
wix-563857ee4097aead50ff5711c5dc759c0fcc4c5d.zip
Simplify and standardize exception handling in extension manager
Diffstat (limited to 'src')
-rw-r--r--src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs80
1 files changed, 35 insertions, 45 deletions
diff --git a/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs b/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs
index 2ecf85ae..d1bbbb4a 100644
--- a/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs
+++ b/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs
@@ -44,33 +44,44 @@ namespace WixToolset.Core.ExtensibilityServices
44 44
45 public void Load(string extensionPath) 45 public void Load(string extensionPath)
46 { 46 {
47 Assembly assembly; 47 try
48
49 // Absolute path to an assembly which means only "load from" will work even though we'd prefer to
50 // use Assembly.Load (see the documentation for Assembly.LoadFrom why).
51 if (Path.IsPathRooted(extensionPath))
52 {
53 assembly = ExtensionManager.ExtensionLoadFrom(extensionPath);
54 }
55 else if (ExtensionManager.TryExtensionLoad(extensionPath, out assembly))
56 { 48 {
57 // Loaded the assembly by name from the probing path. 49 Assembly assembly;
50
51 // Absolute path to an assembly which means only "load from" will work even though we'd prefer to
52 // use Assembly.Load (see the documentation for Assembly.LoadFrom why).
53 if (Path.IsPathRooted(extensionPath))
54 {
55 assembly = Assembly.LoadFrom(extensionPath);
56 }
57 else if (ExtensionManager.TryExtensionLoad(extensionPath, out assembly))
58 {
59 // Loaded the assembly by name from the probing path.
60 }
61 else if (ExtensionManager.TryExtensionLoad(Path.GetFileNameWithoutExtension(extensionPath), out assembly))
62 {
63 // Loaded the assembly by filename alone along the probing path.
64 }
65 else // relative path to an assembly
66 {
67 // We want to use Assembly.Load when we can because it has some benefits over Assembly.LoadFrom
68 // (see the documentation for Assembly.LoadFrom). However, it may fail when the path is a relative
69 // path, so we should try Assembly.LoadFrom one last time. We could have detected a directory
70 // separator character and used Assembly.LoadFrom directly, but dealing with path canonicalization
71 // issues is something we don't want to deal with if we don't have to.
72 assembly = Assembly.LoadFrom(extensionPath);
73 }
74
75 this.Add(assembly);
58 } 76 }
59 else if (ExtensionManager.TryExtensionLoad(Path.GetFileNameWithoutExtension(extensionPath), out assembly)) 77 catch (ReflectionTypeLoadException rtle)
60 { 78 {
61 // Loaded the assembly by filename alone along the probing path. 79 throw new WixException(ErrorMessages.InvalidExtension(extensionPath, String.Join(Environment.NewLine, rtle.LoaderExceptions.Select(le => le.ToString()))));
62 } 80 }
63 else // relative path to an assembly 81 catch (Exception e)
64 { 82 {
65 // We want to use Assembly.Load when we can because it has some benefits over Assembly.LoadFrom 83 throw new WixException(ErrorMessages.InvalidExtension(extensionPath, e.Message), e);
66 // (see the documentation for Assembly.LoadFrom). However, it may fail when the path is a relative
67 // path, so we should try Assembly.LoadFrom one last time. We could have detected a directory
68 // separator character and used Assembly.LoadFrom directly, but dealing with path canonicalization
69 // issues is something we don't want to deal with if we don't have to.
70 assembly = ExtensionManager.ExtensionLoadFrom(extensionPath);
71 } 84 }
72
73 this.Add(assembly);
74 } 85 }
75 86
76 public IEnumerable<T> Create<T>() where T : class 87 public IEnumerable<T> Create<T>() where T : class
@@ -93,18 +104,6 @@ namespace WixToolset.Core.ExtensibilityServices
93 return extensions.Cast<T>().ToList(); 104 return extensions.Cast<T>().ToList();
94 } 105 }
95 106
96 private static Assembly ExtensionLoadFrom(string assemblyName)
97 {
98 try
99 {
100 return Assembly.LoadFrom(assemblyName);
101 }
102 catch (Exception e)
103 {
104 throw new WixException(ErrorMessages.InvalidExtension(assemblyName, e.Message), e);
105 }
106 }
107
108 private static bool TryExtensionLoad(string assemblyName, out Assembly assembly) 107 private static bool TryExtensionLoad(string assemblyName, out Assembly assembly)
109 { 108 {
110 try 109 try
@@ -112,19 +111,10 @@ namespace WixToolset.Core.ExtensibilityServices
112 assembly = Assembly.Load(assemblyName); 111 assembly = Assembly.Load(assemblyName);
113 return true; 112 return true;
114 } 113 }
115 catch (IOException innerE) 114 catch (IOException e) when (e is FileLoadException || e is FileNotFoundException)
116 {
117 if (innerE is FileLoadException || innerE is FileNotFoundException)
118 {
119 assembly = null;
120 return false;
121 }
122
123 throw new WixException(ErrorMessages.InvalidExtension(assemblyName, innerE.Message), innerE);
124 }
125 catch (Exception e)
126 { 115 {
127 throw new WixException(ErrorMessages.InvalidExtension(assemblyName, e.Message), e); 116 assembly = null;
117 return false;
128 } 118 }
129 } 119 }
130 } 120 }