Skip to content

Commit 04d7d3e

Browse files
committed
Fix exceptions in .NET Standard projects other than .NET Core
The System.Runtime.Loader is supported only for .NET Core despite having .NET Standard target, and was causing TypeInitializationExceptions. All usage of this class was moved to separate util class so this exception can be caught properly. Also it seems that DependencyContext.Default is returning null for other than .NET Core projects so null checks were added as well. DEVSIX-1726
1 parent 63aea5e commit 04d7d3e

File tree

2 files changed

+101
-16
lines changed

2 files changed

+101
-16
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
3+
This file is part of the iText (R) project.
4+
Copyright (c) 1998-2018 iText Group NV
5+
Authors: Bruno Lowagie, Paulo Soares, et al.
6+
7+
This program is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License version 3
9+
as published by the Free Software Foundation with the addition of the
10+
following permission added to Section 15 as permitted in Section 7(a):
11+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
12+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
13+
OF THIRD PARTY RIGHTS
14+
15+
This program is distributed in the hope that it will be useful, but
16+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17+
or FITNESS FOR A PARTICULAR PURPOSE.
18+
See the GNU Affero General Public License for more details.
19+
You should have received a copy of the GNU Affero General Public License
20+
along with this program; if not, see http://www.gnu.org/licenses or write to
21+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
23+
http://itextpdf.com/terms-of-use/
24+
25+
The interactive user interfaces in modified source and object code versions
26+
of this program must display Appropriate Legal Notices, as required under
27+
Section 5 of the GNU Affero General Public License.
28+
29+
In accordance with Section 7(b) of the GNU Affero General Public License,
30+
a covered work must retain the producer line in every PDF that is created
31+
or manipulated using iText.
32+
33+
You can be released from the requirements of the license by purchasing
34+
a commercial license. Buying such a license is mandatory as soon as you
35+
develop commercial activities involving the iText software without
36+
disclosing the source code of your own applications.
37+
These activities include: offering paid services to customers as an ASP,
38+
serving PDFs on the fly in a web application, shipping iText with a closed
39+
source product.
40+
41+
For more information, please contact iText Software Corp. at this
42+
43+
*/
44+
using System;
45+
using System.Collections.Generic;
46+
using System.Reflection;
47+
using System.Text;
48+
49+
namespace iText.IO.Util
50+
{
51+
#if NETSTANDARD1_6
52+
/// <summary>This file is a helper class for internal usage only.</summary>
53+
/// <remarks>
54+
/// This file is a helper class for internal usage only.
55+
/// Be aware that it's API and functionality may be changed in future.
56+
/// <br/>
57+
/// <br/>
58+
/// Ussage of this class may throw TypeInitializationException
59+
/// in .NET Standard enviroments that doesn't support System.Runtime.Loader
60+
/// </remarks>
61+
public static class AssemblyLoadContextUtil {
62+
63+
public static AssemblyName GetAssemblyName(String path) {
64+
return System.Runtime.Loader.AssemblyLoadContext.GetAssemblyName(path);
65+
}
66+
67+
public static Assembly LoadFromDefaultContextAssemblyPath(String path) {
68+
return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
69+
}
70+
71+
public static void RegisterUnloadingEvent(Action<object> onUnloading) {
72+
RegisterUnloadingEvent(onUnloading, null);
73+
}
74+
75+
public static void RegisterUnloadingEvent(Action<object> onUnloading, Assembly assembly) {
76+
if (assembly == null) {
77+
System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += onUnloading;
78+
} else {
79+
System.Runtime.Loader.AssemblyLoadContext.GetLoadContext(assembly).Unloading += onUnloading;
80+
}
81+
}
82+
}
83+
#endif
84+
}

itext/itext.io/itext/io/util/ResourceUtil.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ source product.
4848
using System.Linq;
4949
using System.Reflection;
5050
#if NETSTANDARD1_6
51-
using System.Runtime.Loader;
5251
using Microsoft.DotNet.PlatformAbstractions;
5352
using Microsoft.Extensions.DependencyModel;
5453
#endif
@@ -134,17 +133,19 @@ public static Stream GetResourceStream(string key, Type definedClassType) {
134133
}
135134
}
136135
#else
137-
string runtimeId = RuntimeEnvironment.GetRuntimeIdentifier();
138-
IEnumerable<AssemblyName> loadedAssemblies = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId).ToList();
139-
foreach (AssemblyName assemblyName in loadedAssemblies) {
140-
if (assemblyName.Name.StartsWith("itext")) {
141-
try {
142-
Assembly assembly = Assembly.Load(assemblyName);
143-
istr = SearchResourceInAssembly(key, assembly);
144-
if (istr != null) {
145-
return istr;
146-
}
147-
} catch { }
136+
if (DependencyContext.Default != null) {
137+
string runtimeId = RuntimeEnvironment.GetRuntimeIdentifier();
138+
IEnumerable<AssemblyName> loadedAssemblies = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId).ToList();
139+
foreach (AssemblyName assemblyName in loadedAssemblies) {
140+
if (assemblyName.Name.StartsWith("itext")) {
141+
try {
142+
Assembly assembly = Assembly.Load(assemblyName);
143+
istr = SearchResourceInAssembly(key, assembly);
144+
if (istr != null) {
145+
return istr;
146+
}
147+
} catch { }
148+
}
148149
}
149150
}
150151
#endif
@@ -170,7 +171,7 @@ private static Stream SearchResourceInAssembly(string key, Object obj) {
170171
#if !NETSTANDARD1_6
171172
istr = Assembly.LoadFrom(dir).GetManifestResourceStream(key);
172173
#else
173-
istr = AssemblyLoadContext.Default.LoadFromAssemblyPath(key).GetManifestResourceStream(key);
174+
istr = AssemblyLoadContextUtil.LoadFromDefaultContextAssemblyPath(key).GetManifestResourceStream(key);
174175
#endif
175176
}
176177
catch
@@ -234,16 +235,16 @@ private static void LoadITextResourceAssemblies() {
234235
}
235236
#else
236237
string runtimeId = RuntimeEnvironment.GetRuntimeIdentifier();
237-
List<AssemblyName> loadedAssemblies = DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId).ToList();
238+
List<AssemblyName> loadedAssemblies = DependencyContext.Default != null ? DependencyContext.Default.GetRuntimeAssemblyNames(runtimeId).ToList() : new List<AssemblyName>();
238239

239240
var referencedPaths = Directory.GetFiles(FileUtil.GetBaseDirectory(), "*.dll");
240241
foreach (String path in referencedPaths)
241242
{
242243
try
243244
{
244-
AssemblyName name = AssemblyLoadContext.GetAssemblyName(path);
245+
AssemblyName name = AssemblyLoadContextUtil.GetAssemblyName(path);
245246
if (iTextResourceAssemblyNames.Contains(name.Name) && !loadedAssemblies.Any(assembly => assembly.Name.Equals(name.Name))) {
246-
Assembly newAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
247+
Assembly newAssembly = AssemblyLoadContextUtil.LoadFromDefaultContextAssemblyPath(path);
247248
loadedAssemblies.Add(newAssembly.GetName());
248249
}
249250
}

0 commit comments

Comments
 (0)